Qt makes aero special effect window

Source: Internet
Author: User
QT is a beginner. He reads books and makes little things by himself. Recently, a sudden whim, I want to make a little clever, beautiful interface a little forgotten as the desktop. After thinking for a long time, I found that the aero Effect of Vista/win7 was good. Besides, I am using win7 now. So I went online and found out how to make the aero effect by using QT. After Google finally found the function and usage. So we made a simple aero special effect window.

The following are header files and implementation files: // 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 extendframeworkclientarea (qwidget * widget, int left =-1, int Top =-1, int right =-1, int Bottom =-1); static bool iscompositionenabled (); static qcolor colorizatincolor (); Private: static windownotif IER * 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 0 X00000004 // your has been specified # define defaults 0x031e // composition changed window messagetypedef struct _ blank {DWORD dwflags; bool fenable; hrgn hrgnblur; bool finished;} blank, * blank; typedef struct _ margins {int cxleftwidth; int cxrightwidth; int cytopheight; int cybottomheight;} margins, * pmargins; typedef hresult (Wina PI * enabled) (bool * pfenabled); typedef hresult (winapi * enabled) (hwnd, const margins * pmarinset); typedef hresult (winapi * ptrdwmenableaerowindow) (hwnd, const dwm_blurbehind * pblurbehind); typedef hresult (winapi * ptrdwmgetcolorizationcolor) (DWORD * pcrcolorization, bool * pfopaqueblend); static 1_pdwmiscompositionenabled = 0 s Tatic ptrdwmenableaerowindow pdwmenableaerowindow = 0; static synchronized messages = 0; static synchronized pdwmgetcolorizationcolor = 0;/** synchronously responds to the DWM status message */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"); pdwmexico tendframeworkclientarea = (ptrdwmexico tendframeworkclientarea) dwmlib. resolve ("dwmextendframeworkclientarea"); pdwmenableaerowindow = (ptrdwmenableaerowindow) dwmlib. resolve ("dwmenableaerowindow"); pdwmgetcolorizationcolor = (ptrdwmg Etcolorizationcolor) dwmlib. Resolve ("dwmgetcolorizationcolor");} return pdwmiscompositionenabled! = 0;} # endif /*! * Check whether DWM is enabled **/bool qtwin: iscompositionenabled () {# ifdef q_ws_winif (resolvelibs () {hresult hR = s_ OK; bool isenabled = false; hR = pdwmiscompositionenabled (& isenabled); If (succeeded (HR) return isenabled ;}# endifreturn false ;}/*! * Implement aero for a widget. **/bool qtwin: enableaerowindow (qwidget * widget, bool enable) {q_assert (widget); bool result = false; # ifdef q_ws_winif (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 = pdwmenableaerowindo W (widget-> winid (), & bb); If (succeeded (HR) {result = true; windownotifier ()-> addwidget (widget );}} # endifreturn result ;}/*! * Set the aero drawing area */bool qtwin: extendframeworkclientarea (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_winif (resolvelibs () {qlibrary dwmlib (qstring :: fromascii ("dwmapi"); hresult hR = s_ OK; margins M = {left, top, right, bottom}; HR = pdwmextendframeworkclientarea (widget-> winid (), & M ); If (succeeded (HR) {result = true; windownotifier ()-> addwidget (widget);} widget-> setattribute (QT: wa_translucentbackground, result );} # endifreturn result ;}/*! * Returns the color of the current window. */qcolor qtwin: colorizatincolor () {qcolor resultcolor = qapplication: palette (). window (). color (); # ifdef q_ws_winif (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) ;}# endifreturn resultcolor ;} # ifdef q_ws_winwindownotifier * qtwin :: Windownotifier () {static windownotifier * windownotifierinstance = 0; If (! Windownotifierinstance) windownotifierinstance = new windownotifier; return windownotifierinstance;}/* All windows respond to the DWM status change message */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, composi Tionenabled) ;}widget-> Update () ;}return qwidget: winevent (message, result) ;}# when using endif, you only need to generate main in the window. # include "qtwin. H ", and use related functions. The following is main. CPP file: # 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: mimizewindowhint); # ifdef q_ws_x11window.setattribu Te (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 backgroundwindow. setattribute (QT: wa_styledbackground, false); # endifif (qtwin: iscompositionenabled () {qtwin: exte Ndframeworkclientarea (& window); window. setcontentsmargins (0, 0, 0, 0);} window. show (); Return a.exe C. Recently, a sudden whim, I want to make a little clever, beautiful interface a little forgotten as the desktop. After thinking for a long time, I found that the aero Effect of Vista/win7 was good. Besides, I am using win7 now. So I went online and found out how to make the aero effect by using QT. After Google finally found the function and usage. So we made a simple aero special effect window.

The following are header files and implementation files: // 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 extendframeworkclientarea (qwidget * widget, int left =-1, int Top =-1, int right =-1, int Bottom =-1); static bool iscompositionenabled (); static qcolor colorizatincolor (); Private: static windownotif IER * 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 0 X00000004 // your has been specified # define defaults 0x031e // composition changed window messagetypedef struct _ blank {DWORD dwflags; bool fenable; hrgn hrgnblur; bool finished;} blank, * blank; typedef struct _ margins {int cxleftwidth; int cxrightwidth; int cytopheight; int cybottomheight;} margins, * pmargins; typedef hresult (Wina PI * enabled) (bool * pfenabled); typedef hresult (winapi * enabled) (hwnd, const margins * pmarinset); typedef hresult (winapi * ptrdwmenableaerowindow) (hwnd, const dwm_blurbehind * pblurbehind); typedef hresult (winapi * ptrdwmgetcolorizationcolor) (DWORD * pcrcolorization, bool * pfopaqueblend); static 1_pdwmiscompositionenabled = 0 s Tatic ptrdwmenableaerowindow pdwmenableaerowindow = 0; static synchronized messages = 0; static synchronized pdwmgetcolorizationcolor = 0;/** synchronously responds to the DWM status message */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"); pdwmexico tendframeworkclientarea = (ptrdwmexico tendframeworkclientarea) dwmlib. resolve ("dwmextendframeworkclientarea"); pdwmenableaerowindow = (ptrdwmenableaerowindow) dwmlib. resolve ("dwmenableaerowindow"); pdwmgetcolorizationcolor = (ptrdwmg Etcolorizationcolor) dwmlib. Resolve ("dwmgetcolorizationcolor");} return pdwmiscompositionenabled! = 0;} # endif /*! * Check whether DWM is enabled **/bool qtwin: iscompositionenabled () {# ifdef q_ws_winif (resolvelibs () {hresult hR = s_ OK; bool isenabled = false; hR = pdwmiscompositionenabled (& isenabled); If (succeeded (HR) return isenabled ;}# endifreturn false ;}/*! * Implement aero for a widget. **/bool qtwin: enableaerowindow (qwidget * widget, bool enable) {q_assert (widget); bool result = false; # ifdef q_ws_winif (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 = pdwmenableaerowindo W (widget-> winid (), & bb); If (succeeded (HR) {result = true; windownotifier ()-> addwidget (widget );}} # endifreturn result ;}/*! * Set the aero drawing area */bool qtwin: extendframeworkclientarea (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_winif (resolvelibs () {qlibrary dwmlib (qstring :: fromascii ("dwmapi"); hresult hR = s_ OK; margins M = {left, top, right, bottom}; HR = pdwmextendframeworkclientarea (widget-> winid (), & M ); If (succeeded (HR) {result = true; windownotifier ()-> addwidget (widget);} widget-> setattribute (QT: wa_translucentbackground, result );} # endifreturn result ;}/*! * Returns the color of the current window. */qcolor qtwin: colorizatincolor () {qcolor resultcolor = qapplication: palette (). window (). color (); # ifdef q_ws_winif (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) ;}# endifreturn resultcolor ;} # ifdef q_ws_winwindownotifier * qtwin :: Windownotifier () {static windownotifier * windownotifierinstance = 0; If (! Windownotifierinstance) windownotifierinstance = new windownotifier; return windownotifierinstance;}/* All windows respond to the DWM status change message */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 );} # When Using endif, you only need to generate the main. # include "qtwin. H ", and use related functions. The following is main. CPP file: # 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: mimizewindowhint); # ifdef q_ws_x111_1_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 backgroundwindow. setattribute (QT: wa_styledbackground, false); # endifif (qtwin: iscompositionenabled () {qtwin: extendframeworkclientarea (& window); window. setcontentsmargins (0, 0, 0, 0);} window. show (); Return a.exe C ();}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.