问题
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