Qt制作Aero特效窗口
转载请注明链接与作者huihui1988
初学QT,边看书边自己做点小东西。最近突然心血来潮,想自己做个小巧点的,界面美观一点的备忘当桌面上。想了半天,发现VISTA/WIN7的Aero效果就不错,况且自己现在就在用WIN7。于是上网找了下QT制作Aero效果的方法。Google之后终于找到了函数和用法。于是做了一个简单的Aero特效窗口
- //qtwin.h
- #ifndef QTWIN_H
- #define QTWIN_H
- #include <QColor>
- #include <QWidget>
- class WindowNotifier;
- class QtWin
- {
- public:
- static bool enableAeroWindow(QWidget *widget, bool enable = true);
- static bool extendFrameIntoClientArea(QWidget *widget,
- int left = -1, int top = -1,
- int right = -1, int bottom = -1);
- static bool isCompositionEnabled();
- static QColor colorizatinColor();
- private:
- static WindowNotifier *windowNotifier();
- };
- #endif // QTWIN_H
- //qtwin.cpp
- #include "qtwin.h"
- #include <QLibrary>
- #include <QApplication>
- #include <QWidget>
- #include <QList>
- #include <QPointer>
- #ifdef Q_WS_WIN
- #include <qt_windows.h>
- // Blur behind data structures
- #define DWM_BB_ENABLE 0x00000001 // fEnable has been specified
- #define DWM_BB_BLURREGION 0x00000002 // hRgnBlur has been specified
- #define DWM_BB_TRANSITIONONMAXIMIZED 0x00000004 // fTransitionOnMaximized has been specified
- #define WM_DWMCOMPOSITIONCHANGED 0x031E // Composition changed window message
- typedef struct _DWM_BLURBEHIND
- {
- DWORD dwFlags;
- BOOL fEnable;
- HRGN hRgnBlur;
- BOOL fTransitionOnMaximized;
- } DWM_BLURBEHIND, *PDWM_BLURBEHIND;
- typedef struct _MARGINS
- {
- int cxLeftWidth;
- int cxRightWidth;
- int cyTopHeight;
- int cyBottomHeight;
- } MARGINS, *PMARGINS;
- typedef HRESULT (WINAPI *PtrDwmIsCompositionEnabled)(BOOL* pfEnabled);
- typedef HRESULT (WINAPI *PtrDwmExtendFrameIntoClientArea)(HWND hWnd, const MARGINS* pMarInset);
- typedef HRESULT (WINAPI *PtrDwmenableAeroWindow)(HWND hWnd, const DWM_BLURBEHIND* pBlurBehind);
- typedef HRESULT (WINAPI *PtrDwmGetColorizationColor)(DWORD *pcrColorization, BOOL *pfOpaqueBlend);
- static PtrDwmIsCompositionEnabled pDwmIsCompositionEnabled= 0;
- static PtrDwmenableAeroWindow pDwmenableAeroWindow = 0;
- static PtrDwmExtendFrameIntoClientArea pDwmExtendFrameIntoClientArea = 0;
- static PtrDwmGetColorizationColor pDwmGetColorizationColor = 0;
- /*
- *同步响应DWM状态消息
- */
- class WindowNotifier : public QWidget
- {
- public:
- WindowNotifier() { winId(); }
- void addWidget(QWidget *widget) { widgets.append(widget); }
- void removeWidget(QWidget *widget) { widgets.removeAll(widget); }
- bool winEvent(MSG *message, long *result);
- private:
- QWidgetList widgets;
- };
- static bool resolveLibs()
- {
- if (!pDwmIsCompositionEnabled) {
- QLibrary dwmLib(QString::fromAscii("dwmapi"));
- pDwmIsCompositionEnabled =(PtrDwmIsCompositionEnabled)dwmLib.resolve("DwmIsCompositionEnabled");
- pDwmExtendFrameIntoClientArea = (PtrDwmExtendFrameIntoClientArea)dwmLib.resolve("DwmExtendFrameIntoClientArea");
- pDwmenableAeroWindow = (PtrDwmenableAeroWindow)dwmLib.resolve("DwmenableAeroWindow");
- pDwmGetColorizationColor = (PtrDwmGetColorizationColor)dwmLib.resolve("DwmGetColorizationColor");
- }
- return pDwmIsCompositionEnabled != 0;
- }
- #endif
- /*!
- * 检查 DWM 是否开启
- *
- */
- bool QtWin::isCompositionEnabled()
- {
- #ifdef Q_WS_WIN
- if (resolveLibs()) {
- HRESULT hr = S_OK;
- BOOL isEnabled = false;
- hr = pDwmIsCompositionEnabled(&isEnabled);
- if (SUCCEEDED(hr))
- return isEnabled;
- }
- #endif
- return false;
- }
- /*!
- * 对一个widget实现Aero效果.
- *
- */
- bool QtWin::enableAeroWindow(QWidget *widget, bool enable)
- {
- Q_ASSERT(widget);
- bool result = false;
- #ifdef Q_WS_WIN
- if (resolveLibs()) {
- DWM_BLURBEHIND bb = {0};
- HRESULT hr = S_OK;
- bb.fEnable = enable;
- bb.dwFlags = DWM_BB_ENABLE;
- bb.hRgnBlur = NULL;
- widget->setAttribute(Qt::WA_TranslucentBackground, enable);
- widget->setAttribute(Qt::WA_NoSystemBackground, enable);
- hr = pDwmenableAeroWindow(widget->winId(), &bb);
- if (SUCCEEDED(hr)) {
- result = true;
- windowNotifier()->addWidget(widget);
- }
- }
- #endif
- return result;
- }
- /*!
- * 设置Aero绘图区
- */
- bool QtWin::extendFrameIntoClientArea(QWidget *widget, int left, int top, int right, int bottom)
- {
- Q_ASSERT(widget);
- Q_UNUSED(left);
- Q_UNUSED(top);
- Q_UNUSED(right);
- Q_UNUSED(bottom);
- bool result = false;
- #ifdef Q_WS_WIN
- if (resolveLibs()) {
- QLibrary dwmLib(QString::fromAscii("dwmapi"));
- HRESULT hr = S_OK;
- MARGINS m = {left, top, right, bottom};
- hr = pDwmExtendFrameIntoClientArea(widget->winId(), &m);
- if (SUCCEEDED(hr)) {
- result = true;
- windowNotifier()->addWidget(widget);
- }
- widget->setAttribute(Qt::WA_TranslucentBackground, result);
- }
- #endif
- return result;
- }
- /*!
- * 返回当前窗口颜色.
- */
- QColor QtWin::colorizatinColor()
- {
- QColor resultColor = QApplication::palette().window().color();
- #ifdef Q_WS_WIN
- if (resolveLibs()) {
- DWORD color = 0;
- BOOL opaque = FALSE;
- QLibrary dwmLib(QString::fromAscii("dwmapi"));
- HRESULT hr = S_OK;
- hr = pDwmGetColorizationColor(&color, &opaque);
- if (SUCCEEDED(hr))
- resultColor = QColor(color);
- }
- #endif
- return resultColor;
- }
- #ifdef Q_WS_WIN
- WindowNotifier *QtWin::windowNotifier()
- {
- static WindowNotifier *windowNotifierInstance = 0;
- if (!windowNotifierInstance)
- windowNotifierInstance = new WindowNotifier;
- return windowNotifierInstance;
- }
- /* 所有窗口响应 DWM 状态变换消息 */
- bool WindowNotifier::winEvent(MSG *message, long *result)
- {
- if (message && message->message == WM_DWMCOMPOSITIONCHANGED) {
- bool compositionEnabled = QtWin::isCompositionEnabled();
- foreach(QWidget * widget, widgets) {
- if (widget) {
- widget->setAttribute(Qt::WA_NoSystemBackground, compositionEnabled);
- }
- widget->update();
- }
- }
- return QWidget::winEvent(message, result);
- }
- #endif
- 使用的时候只需要在生成窗口的main.cpp中#include "qtwin.h",并使用相关函数,以下是main.cpp文件:
- #include <QtGui/QApplication>
- #include "widget.h"
- #include "qtwin.h"
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- Widget window;
- window.setGeometry(1040,0,240,120);
- window.setWindowFlags(Qt::CustomizeWindowHint);
- #ifdef Q_WS_X11
- window.setAttribute(Qt::WA_TranslucentBackground);
- window.setAttribute(Qt::WA_NoSystemBackground, false);
- QPalette pal = window.palette();
- QColor bg = pal.window().color();
- bg.setAlpha(180);
- pal.setColor(QPalette::Window, bg);
- window.setPalette(pal);
- window.ensurePolished(); // workaround Oxygen filling the background
- window.setAttribute(Qt::WA_StyledBackground, false);
- #endif
- if (QtWin::isCompositionEnabled()) {
- QtWin::extendFrameIntoClientArea(&window);
- window.setContentsMargins(0, 0, 0, 0);
- }
- window.show();
- return a.exec();
- }
http://blog.csdn.net/huihui1988/article/details/5601497
Qt制作Aero特效窗口的更多相关文章
- Qt Style Sheets制作UI特效
使用Qt Style Sheets制作UI特效 博客出处:http://developer.nokia.com/community/wiki/%E4%BD%BF%E7%94%A8Qt_Style_S ...
- QT制作一个图片播放器
前言:使用qt制作了一个简单的图片播放器,可以播放gif.png等格式图片 先来看看播放器的功能(当然是很简陋的,没有很深入的设计): 1.点击图片列表中图片进行播放. 2.自动播放,播放的图片的间隔 ...
- QT笔记之不规则窗口的实现
QT实现的不规则窗口,是根据图片的形状显示 1.去标题栏 2.设置窗口背景为透明色 3.最后给窗口设置背景色 注:背景图为镂空的 格式为.png 图片资源下载:http://pan.baidu.com ...
- QT笔记之自定义窗口拖拽移动
1.QT自定义标题栏,拖拽标题栏移动窗口(只能拖拽标题,其他位置无法拖拽) 方法一: 转载:http://blog.sina.com.cn/s/blog_4ba5b45e0102e83h.html . ...
- Qt 制作安装包
Qt 制作在线.离线 安装包 见如下文档
- Qt 鼠标样式特效探索样例(一)——利用时间器调用QWidget.move()函数
Qt 鼠标样式特效探索样例(一) 心血来潮,突然想在Qt里玩一把鼠标样式,想到在浏览网页时,经常看到漂亮的鼠标动画,于是今天摸索着乱写个粗糙的demo,来满足自己的好奇心. 效果图 方案要 ...
- Qt全屏显示窗口、子窗口的相关函数
Qt全屏显示函数 window.showFullScreen() Qt最大化显示函数 window.showMaximized() Qt最小化显示函数 ...
- Qt控制台和带窗口的区别_mickelfeng_新浪博客
Qt控制台和带窗口的区别_mickelfeng_新浪博客 t控制台和带窗口的区别 (2012-04-30 10:50:53) 标签: 杂谈 分类: C/C ...
- 使用qt制作一个简单的计算器
前言:今天使用qt制作了一个很简单的计算器,觉得挺有意思的,所以在这里跟大家分享一下. 这里先跟大家说说使用到的函数: 一.槽连接函数 connect(信号发送者,发送的信号,信号接收者,信号接收者的 ...
随机推荐
- PowerShell_零基础自学课程_3_如何利用Powershell ISE调试PS脚本
微软在推出PS的同时,没有忘记其一贯的作风,什么东东都弄一个IDE环境,这不微软没有忘记给PS也来一个IDE的环境, 通过这个IDE环境,可以建立psl文件,可以调试psl文件. 1.IDE界面 我们 ...
- 【转】Android下编译jni库的二种方法(含示例)
原文网址:http://blog.sina.com.cn/s/blog_3e3fcadd01011384.html 总结如下:两种方法是:1)使用Android源码中的Make系统2)使用NDK(从N ...
- JQuery 图片延迟加载并等比缩放插件
原文地址:http://www.shangxueba.com/jingyan/1909987.html DEMO地址:http://demo.jb51.net/html/jquery_img/jque ...
- hdu 3232 Crossing Rivers(期望 + 数学推导 + 分类讨论,水题不水)
Problem Description You live in a village but work in another village. You decided to follow the s ...
- Spark常用函数讲解之Action操作
摘要: RDD:弹性分布式数据集,是一种特殊集合 ‚ 支持多种来源 ‚ 有容错机制 ‚ 可以被缓存 ‚ 支持并行操作,一个RDD代表一个分区里的数据集RDD有两种操作算子: Trans ...
- LDA-线性判别分析(二)
本来是要调研 Latent Dirichlet Allocation 的那个 LDA 的, 没想到查到很多关于 Linear Discriminant Analysis 这个 LDA 的资料.初步看了 ...
- Linux 内核开发 - 进程空间
1.1 虚拟内存 Linux 的系统.假设每一个任务都独立的占用内存,则实际的物理内存将非常快消耗殆尽.实际上对于前台正在执行的任务来说,所须要要的内存并不多,非常多任务基本不须要执行,也就没有必要一 ...
- IOS总结_无需自己定义UITabbar也可改变UITabbarController的背景和点击和的颜色
在application: application didFinishLaunchingWithOptions: launchOptions 增加以下代码就能够实现对tabbar的颜色的改动 //设定 ...
- Flashback Query(函数示例)
Flashback Query 函数,存储过程,包,触发器等对象Flashback Drop 可以闪回与表相关联的对象, 如果是其他的对象,比如function,procedure,trigger等. ...
- VML :Vector Markup Language
在以前老是浏览器IE<9在不支持SVG情况下,IE一般通过VML来绘制图形,图片,文字等 步骤: 必须在头部添加 <HTML xmlns:v="urn:schemas-micro ...