How do I set the WINDOWCLASSX hbrBackground alpha channel? (C++)

淺唱寂寞╮ 提交于 2020-01-04 18:30:35

问题


So, I have a WINDOWCLASSX that I want to set the background to, including the alpha channel, but I only saw an "RGB" macro; no RGBA.

So how do I set alpha on hbrBackground? Here is my code:

    WNDCLASSEX wincl;  


wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;      
wincl.style = CS_DBLCLKS;               
wincl.cbSize = sizeof (WNDCLASSEX);

wincl.hIcon = LoadIcon (GetModuleHandle(0), MAKEINTRESOURCE(IDI_MYAPP));
wincl.hIconSm = LoadIcon (GetModuleHandle(0), MAKEINTRESOURCE(IDI_MYAPP));
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = MAKEINTRESOURCE(IDR_MAINFRAME);               
wincl.cbClsExtra = 0;                     
wincl.cbWndExtra = 0;                     

wincl.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);

On that last line, I want to be able to set alpha.

-Thanks for any help.


回答1:


You cannot create an alpha channel using a background brush on the window class. You have to apply the WS_EX_LAYERED style to the window instead and then use either SetLayeredWindowAttributes() or UpdateLayeredWindow() to set the window's alpha channel. Read the MSDN documentation for more details:

Using Layered Windows

Layered Windows




回答2:


This just worked out for me:

// Set WS_EX_LAYERED on this window 
SetWindowLong(g_mainWnd, GWL_EXSTYLE, GetWindowLong(g_mainWnd, GWL_EXSTYLE) | WS_EX_LAYERED);

// Make this window 70% alpha
SetLayeredWindowAttributes(g_mainWnd, 0, (255 * 70) / 100, LWA_ALPHA);

The g_mainWnd variable is the reference to the corresponding window (in my case, a HWND variable).



来源:https://stackoverflow.com/questions/10022391/how-do-i-set-the-windowclassx-hbrbackground-alpha-channel-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!