转载请注明链接与作者huihui1988

初学QT,边看书边自己做点小东西。最近突然心血来潮,想自己做个小巧点的,界面美观一点的备忘当桌面上。想了半天,发现VISTA/WIN7的Aero效果就不错,况且自己现在就在用WIN7。于是上网找了下QT制作Aero效果的方法。Google之后终于找到了函数和用法。于是做了一个简单的Aero特效窗口

 
以下是头文件和实现文件:
  1. //qtwin.h
  2. #ifndef QTWIN_H
  3. #define QTWIN_H
  4. #include <QColor>
  5. #include <QWidget>
  6. class WindowNotifier;
  7. class QtWin
  8. {
  9. public:
  10. static bool enableAeroWindow(QWidget *widget, bool enable = true);
  11. static bool extendFrameIntoClientArea(QWidget *widget,
  12. int left = -1, int top = -1,
  13. int right = -1, int bottom = -1);
  14. static bool isCompositionEnabled();
  15. static QColor colorizatinColor();
  16. private:
  17. static WindowNotifier *windowNotifier();
  18. };
  19. #endif // QTWIN_H
  20. //qtwin.cpp
  21. #include "qtwin.h"
  22. #include <QLibrary>
  23. #include <QApplication>
  24. #include <QWidget>
  25. #include <QList>
  26. #include <QPointer>
  27. #ifdef Q_WS_WIN
  28. #include <qt_windows.h>
  29. // Blur behind data structures
  30. #define DWM_BB_ENABLE                 0x00000001  // fEnable has been specified
  31. #define DWM_BB_BLURREGION             0x00000002  // hRgnBlur has been specified
  32. #define DWM_BB_TRANSITIONONMAXIMIZED  0x00000004  // fTransitionOnMaximized has been specified
  33. #define WM_DWMCOMPOSITIONCHANGED        0x031E    // Composition changed window message
  34. typedef struct _DWM_BLURBEHIND
  35. {
  36. DWORD dwFlags;
  37. BOOL fEnable;
  38. HRGN hRgnBlur;
  39. BOOL fTransitionOnMaximized;
  40. } DWM_BLURBEHIND, *PDWM_BLURBEHIND;
  41. typedef struct _MARGINS
  42. {
  43. int cxLeftWidth;
  44. int cxRightWidth;
  45. int cyTopHeight;
  46. int cyBottomHeight;
  47. } MARGINS, *PMARGINS;
  48. typedef HRESULT (WINAPI *PtrDwmIsCompositionEnabled)(BOOL* pfEnabled);
  49. typedef HRESULT (WINAPI *PtrDwmExtendFrameIntoClientArea)(HWND hWnd, const MARGINS* pMarInset);
  50. typedef HRESULT (WINAPI *PtrDwmenableAeroWindow)(HWND hWnd, const DWM_BLURBEHIND* pBlurBehind);
  51. typedef HRESULT (WINAPI *PtrDwmGetColorizationColor)(DWORD *pcrColorization, BOOL *pfOpaqueBlend);
  52. static PtrDwmIsCompositionEnabled pDwmIsCompositionEnabled= 0;
  53. static PtrDwmenableAeroWindow pDwmenableAeroWindow = 0;
  54. static PtrDwmExtendFrameIntoClientArea pDwmExtendFrameIntoClientArea = 0;
  55. static PtrDwmGetColorizationColor pDwmGetColorizationColor = 0;
  56. /*
  57. *同步响应DWM状态消息
  58. */
  59. class WindowNotifier : public QWidget
  60. {
  61. public:
  62. WindowNotifier() { winId(); }
  63. void addWidget(QWidget *widget) { widgets.append(widget); }
  64. void removeWidget(QWidget *widget) { widgets.removeAll(widget); }
  65. bool winEvent(MSG *message, long *result);
  66. private:
  67. QWidgetList widgets;
  68. };
  69. static bool resolveLibs()
  70. {
  71. if (!pDwmIsCompositionEnabled) {
  72. QLibrary dwmLib(QString::fromAscii("dwmapi"));
  73. pDwmIsCompositionEnabled =(PtrDwmIsCompositionEnabled)dwmLib.resolve("DwmIsCompositionEnabled");
  74. pDwmExtendFrameIntoClientArea = (PtrDwmExtendFrameIntoClientArea)dwmLib.resolve("DwmExtendFrameIntoClientArea");
  75. pDwmenableAeroWindow = (PtrDwmenableAeroWindow)dwmLib.resolve("DwmenableAeroWindow");
  76. pDwmGetColorizationColor = (PtrDwmGetColorizationColor)dwmLib.resolve("DwmGetColorizationColor");
  77. }
  78. return pDwmIsCompositionEnabled != 0;
  79. }
  80. #endif
  81. /*!
  82. * 检查 DWM 是否开启
  83. *
  84. */
  85. bool QtWin::isCompositionEnabled()
  86. {
  87. #ifdef Q_WS_WIN
  88. if (resolveLibs()) {
  89. HRESULT hr = S_OK;
  90. BOOL isEnabled = false;
  91. hr = pDwmIsCompositionEnabled(&isEnabled);
  92. if (SUCCEEDED(hr))
  93. return isEnabled;
  94. }
  95. #endif
  96. return false;
  97. }
  98. /*!
  99. * 对一个widget实现Aero效果.
  100. *
  101. */
  102. bool QtWin::enableAeroWindow(QWidget *widget, bool enable)
  103. {
  104. Q_ASSERT(widget);
  105. bool result = false;
  106. #ifdef Q_WS_WIN
  107. if (resolveLibs()) {
  108. DWM_BLURBEHIND bb = {0};
  109. HRESULT hr = S_OK;
  110. bb.fEnable = enable;
  111. bb.dwFlags = DWM_BB_ENABLE;
  112. bb.hRgnBlur = NULL;
  113. widget->setAttribute(Qt::WA_TranslucentBackground, enable);
  114. widget->setAttribute(Qt::WA_NoSystemBackground, enable);
  115. hr = pDwmenableAeroWindow(widget->winId(), &bb);
  116. if (SUCCEEDED(hr)) {
  117. result = true;
  118. windowNotifier()->addWidget(widget);
  119. }
  120. }
  121. #endif
  122. return result;
  123. }
  124. /*!
  125. * 设置Aero绘图区
  126. */
  127. bool QtWin::extendFrameIntoClientArea(QWidget *widget, int left, int top, int right, int bottom)
  128. {
  129. Q_ASSERT(widget);
  130. Q_UNUSED(left);
  131. Q_UNUSED(top);
  132. Q_UNUSED(right);
  133. Q_UNUSED(bottom);
  134. bool result = false;
  135. #ifdef Q_WS_WIN
  136. if (resolveLibs()) {
  137. QLibrary dwmLib(QString::fromAscii("dwmapi"));
  138. HRESULT hr = S_OK;
  139. MARGINS m = {left, top, right, bottom};
  140. hr = pDwmExtendFrameIntoClientArea(widget->winId(), &m);
  141. if (SUCCEEDED(hr)) {
  142. result = true;
  143. windowNotifier()->addWidget(widget);
  144. }
  145. widget->setAttribute(Qt::WA_TranslucentBackground, result);
  146. }
  147. #endif
  148. return result;
  149. }
  150. /*!
  151. * 返回当前窗口颜色.
  152. */
  153. QColor QtWin::colorizatinColor()
  154. {
  155. QColor resultColor = QApplication::palette().window().color();
  156. #ifdef Q_WS_WIN
  157. if (resolveLibs()) {
  158. DWORD color = 0;
  159. BOOL opaque = FALSE;
  160. QLibrary dwmLib(QString::fromAscii("dwmapi"));
  161. HRESULT hr = S_OK;
  162. hr = pDwmGetColorizationColor(&color, &opaque);
  163. if (SUCCEEDED(hr))
  164. resultColor = QColor(color);
  165. }
  166. #endif
  167. return resultColor;
  168. }
  169. #ifdef Q_WS_WIN
  170. WindowNotifier *QtWin::windowNotifier()
  171. {
  172. static WindowNotifier *windowNotifierInstance = 0;
  173. if (!windowNotifierInstance)
  174. windowNotifierInstance = new WindowNotifier;
  175. return windowNotifierInstance;
  176. }
  177. /* 所有窗口响应 DWM 状态变换消息 */
  178. bool WindowNotifier::winEvent(MSG *message, long *result)
  179. {
  180. if (message && message->message == WM_DWMCOMPOSITIONCHANGED) {
  181. bool compositionEnabled = QtWin::isCompositionEnabled();
  182. foreach(QWidget * widget, widgets) {
  183. if (widget) {
  184. widget->setAttribute(Qt::WA_NoSystemBackground, compositionEnabled);
  185. }
  186. widget->update();
  187. }
  188. }
  189. return QWidget::winEvent(message, result);
  190. }
  191. #endif
  192. 使用的时候只需要在生成窗口的main.cpp中#include "qtwin.h",并使用相关函数,以下是main.cpp文件:
  193. #include <QtGui/QApplication>
  194. #include "widget.h"
  195. #include "qtwin.h"
  196. int main(int argc, char *argv[])
  197. {
  198. QApplication a(argc, argv);
  199. Widget window;
  200. window.setGeometry(1040,0,240,120);
  201. window.setWindowFlags(Qt::CustomizeWindowHint);
  202. #ifdef Q_WS_X11
  203. window.setAttribute(Qt::WA_TranslucentBackground);
  204. window.setAttribute(Qt::WA_NoSystemBackground, false);
  205. QPalette pal = window.palette();
  206. QColor bg = pal.window().color();
  207. bg.setAlpha(180);
  208. pal.setColor(QPalette::Window, bg);
  209. window.setPalette(pal);
  210. window.ensurePolished(); // workaround Oxygen filling the background
  211. window.setAttribute(Qt::WA_StyledBackground, false);
  212. #endif
  213. if (QtWin::isCompositionEnabled()) {
  214. QtWin::extendFrameIntoClientArea(&window);
  215. window.setContentsMargins(0, 0, 0, 0);
  216. }
  217. window.show();
  218. return a.exec();
  219. }

http://blog.csdn.net/huihui1988/article/details/5601497

Qt制作Aero特效窗口的更多相关文章

  1. Qt Style Sheets制作UI特效

    使用Qt Style Sheets制作UI特效  博客出处:http://developer.nokia.com/community/wiki/%E4%BD%BF%E7%94%A8Qt_Style_S ...

  2. QT制作一个图片播放器

    前言:使用qt制作了一个简单的图片播放器,可以播放gif.png等格式图片 先来看看播放器的功能(当然是很简陋的,没有很深入的设计): 1.点击图片列表中图片进行播放. 2.自动播放,播放的图片的间隔 ...

  3. QT笔记之不规则窗口的实现

    QT实现的不规则窗口,是根据图片的形状显示 1.去标题栏 2.设置窗口背景为透明色 3.最后给窗口设置背景色 注:背景图为镂空的 格式为.png 图片资源下载:http://pan.baidu.com ...

  4. QT笔记之自定义窗口拖拽移动

    1.QT自定义标题栏,拖拽标题栏移动窗口(只能拖拽标题,其他位置无法拖拽) 方法一: 转载:http://blog.sina.com.cn/s/blog_4ba5b45e0102e83h.html . ...

  5. Qt 制作安装包

    Qt 制作在线.离线 安装包 见如下文档

  6. Qt 鼠标样式特效探索样例(一)——利用时间器调用QWidget.move()函数

    Qt 鼠标样式特效探索样例(一)       心血来潮,突然想在Qt里玩一把鼠标样式,想到在浏览网页时,经常看到漂亮的鼠标动画,于是今天摸索着乱写个粗糙的demo,来满足自己的好奇心. 效果图 方案要 ...

  7. Qt全屏显示窗口、子窗口的相关函数

    Qt全屏显示函数         window.showFullScreen() Qt最大化显示函数         window.showMaximized() Qt最小化显示函数         ...

  8. Qt控制台和带窗口的区别_mickelfeng_新浪博客

    Qt控制台和带窗口的区别_mickelfeng_新浪博客     t控制台和带窗口的区别    (2012-04-30 10:50:53)    标签:    杂谈        分类: C/C    ...

  9. 使用qt制作一个简单的计算器

    前言:今天使用qt制作了一个很简单的计算器,觉得挺有意思的,所以在这里跟大家分享一下. 这里先跟大家说说使用到的函数: 一.槽连接函数 connect(信号发送者,发送的信号,信号接收者,信号接收者的 ...

随机推荐

  1. Qt如何去掉按钮等控件的虚线框(焦点框)(两种方法)

    方法1:可以通过代码ui->pushButton->setFocusPolicy(Qt::NoFocus)或在Qt Creator的属性列表中设置. 方法2:如果在嵌入式设备中需要通过按键 ...

  2. mysql Event、存储过程、表命令

    Mysql事件调度器(Event Scheduler)类似于定时器,可以在某一个时间点执行一个SQL语句或一个语句块(BEGIN ... END):或者每隔固定间隔重复执行.类似于Linux下的cro ...

  3. QT5.1.0,QT4.8.0以及VC2010、VC2012的测试对比

    QT5.1.0,QT4.8.0以及VC2010.VC2012的交叉测试对比. 测试1: 用VC2012静态编译了QT5.1.0. 编译速度很慢,生成完成后,用VC2012+QT5.1.0进行程序生成, ...

  4. 我的Android4.3新书即将上市,谢谢大家的支持

    首先感谢清华大学.电子工业.机械工业.人民邮电等各大出版社对本书的肯定.我想说中国的IT业如果没有你们的辛勤工作,是不会发展得这么快的.经过再三权衡,本书将选择人民邮电出版社于近几个月在全国出版发行. ...

  5. virtual hard disk

  6. java保留有效数字

    DecimalFormat df=DecimalFormat("######0.0") double d = df.format(xx): 保留一位就是"######0. ...

  7. Java单元测试:@BeforeClass,@Before,@Test,@After,@AfterClass中的问题详解

    在Junit4中还有的测试注解有:  @BeforeClass ,@Before,@Test,@After,@AfterClass 1.其中:@BeforeClass,@AfterClass是Juni ...

  8. 简单的Dao设计模式

    简单的DAO设计模式 这两天学习到了DAO(Data Access Object 数据存取对象)设计模式.想谈谈自己的感受,刚开始接触是感觉有点难,觉得自己逻辑理不清,主要是以前学的知识比较零散没有很 ...

  9. SPRING源码分析:IOC容器

    在Spring中,最基本的IOC容器接口是BeanFactory - 这个接口为具体的IOC容器的实现作了最基本的功能规定 - 不管怎么着,作为IOC容器,这些接口你必须要满足应用程序的最基本要求: ...

  10. iOS:UI系列之UINavigationController

    又到了总结的时间了,突然间感觉时间过得好快啊, 总觉的时间不够用,但是这也没办法啊, 只有自己挤时间了,虽然是零基础,但是这并不能代表什么啦,只要努力,收获总还是有的, 同时我也相信广大的博友肯定也有 ...