如何设置WINDOWCLASSX hbrBackground alpha通道? (C ++)

时间:2012-04-05 03:43:49

标签: c++ winapi user-interface alpha winmain

所以,我有一个WINDOWCLASSX,我想设置背景,包括alpha通道,但我只看到一个“RGB”宏;没有RGBA。

那么如何在hbrBackground上设置alpha?这是我的代码:

    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);

在最后一行,我希望能够设置alpha。

- 感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

您无法使用窗口类上的背景画笔创建Alpha通道。您必须将WS_EX_LAYERED样式应用于窗口,然后使用SetLayeredWindowAttributes()UpdateLayeredWindow()来设置窗口的Alpha通道。有关更多详细信息,请阅读MSDN文档:

Using Layered Windows

Layered Windows

答案 1 :(得分:1)

这对我来说很有用:

// 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);

g_mainWnd变量是对应窗口的引用(在我的例子中是HWND变量)。