Implementing transparent Windows under Win32

Source: Internet
Author: User

The first to write the transparent window code, after Baidu, found SetLayeredWindowAttributes () This function, but because of the lack of online case, so many people can not very good use of this method, I spent a few days to write a realization window transparent class, Put it out here for everyone to use



#define Ws_ex_layered 0x00080000#define lwa_colorkey 0x00000001#define lwa_alpha 0x0000    0002class ctransparent {public:static bool Settransparentwnd (HWND hwnd, COLORREF Crkey, UINT calpha, BOOL dwFlags);    Static BOOL Freeuser32dll ();    Static BOOL changetransparency (HWND hwnd,colorref crkey, UINT balpha, DWORD dwFlags);    Static BOOL Loaduser32dll ();    Static Tranfunc M_pfunc;    Static hinstance m_hinstance; static BOOL initialized;}; HInstance ctransparent::m_hinstance = NULL; Tranfunc ctransparent::m_pfunc = NULL; BOOL ctransparent::initialized = false;//////////////////////////////////////////////////////////////////////// Construction/destruction//////////////////////////////////////////////////////////////////////bool CTransparent    :: Loaduser32dll () {BOOL bRet = FALSE;        if (!initialized) {m_hinstance = LoadLibrary (L "User32.DLL"); if (m_hinstance) {//Get SetLayeredWindowAttributes function pointer m_pfUnc= (Tranfunc) GetProcAddress (m_hinstance, "setlayeredwindowattributes");                 if (m_pfunc! = NULL) {initialized = TRUE;            BRet = TRUE; }}} return bRet;} BOOL Ctransparent::changetransparency (HWND hwnd, COLORREF Crkey, UINT balpha, DWORD dwFlags) {//The value of the transparency SP is 0-255//where 0    For complete transparency should avoid appearing BOOL bRet = FALSE;        if (M_pfunc = = NULL) Loaduser32dll ();          if (m_pfunc! = NULL) {m_pfunc (hWnd, Crkey, Balpha, dwFlags);       BRet = TRUE; } return BRet;}         BOOL Ctransparent::freeuser32dll () {if (m_hinstance! = NULL) {freelibrary (m_hinstance);        if (m_pfunc! = null)//empty m_pfunc = null;     if (initialized! = NULL)//empty initialized = NULL; } return TRUE;       BOOL Ctransparent::settransparentwnd (HWND hwnd, COLORREF Crkey, UINT Calpha, bool dwFlags) {bool BRet = TRUE; To make the form transparent, you first have to have the ws_ex_layered extension property ws_ex_layered = 0x80000//seTwindowlong (This->getsafehwnd (), Gwl_exstyle,getwindowlong (This->getsafehwnd (), GWL_EXSTYLE) ^0x80000); SetWindowLong (hwnd, Gwl_exstyle, GetWindowLong (hwnd, Gwl_exstyle) |          ws_ex_layered);        if (dwFlags) {changetransparency (hWnd, Crkey, Calpha, Lwa_alpha);//Set Calpha value 0-255} else {    Changetransparency (HWnd, Crkey, Calpha, Lwa_colorkey); Crkey the same color becomes transparent} return bRet;}

This static method can be called here.

HWND M_hmywnd =:: CreateWindow (L "Loginwnddll", L "Trawnd", Ws_popup, (int) ((xres-m_wndwidth)/2), (int) ((Yres-m_wndheig HT)/2), M_wndwidth, m_wndheight, 0, 0, hInst, 0);//Register a window here the third parameter represents the type of window Ctransparent::settransparentwnd (HWND) m_ Hmywnd, 0XFFFFFF, 200, 2); Set window transparent:: ShowWindow (M_hmywnd, sw_show);
In fact, the above class used dynamic call user32.dll in the SetLayeredWindowAttributes function, through this function to implement the window transparent way, this method is similar to most methods on the web, but adds a class to encapsulate

Implementing transparent Windows under Win32

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.