我正在尝试在位图中创建一个具有 RGB ( 0, 0, 255 ) 识别的透明区域的子窗口,该位图中将在整个子窗口中绘制。
以下是我用来使其透明的代码,问题是子窗口透明部分下方的父窗口也变得透明,并且通过它可以看到桌面。
hwndChild = ::CreateWindowEx ( 0, "mycustomwindow", txt.c_str ( ), WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_CHILD, x, y, width, height, parent, 0, hInstance, ( LPVOID ) this ) ;
transparent ( );
void transparent ( )
{
HMODULE hUser32 = GetModuleHandle ( ( "USER32.DLL" ) );
COLORREF g_ColourKey = 0xFF00FF; // 0,0,255(magenta) in RGB hex value
typedef BOOL ( WINAPI *lpfnSetLayeredWindowAttributes ) ( HWND hWnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags );
lpfnSetLayeredWindowAttributes SetLayeredWindowAttributes;
SetLayeredWindowAttributes = ( lpfnSetLayeredWindowAttributes ) GetProcAddress ( hUser32, "SetLayeredWindowAttributes" );
if( SetLayeredWindowAttributes == NULL )
{
MessageBox ( hwndChild, "Error, cannot load window transparency\n REASON: Could not load User32.DLL", "Error!", MB_ICONSTOP | MB_OK );
}
else
{
DWORD dwStyle = GetWindowLong ( hwndChild, GWL_STYLE );
dwStyle &= ~( WS_CAPTION | WS_SIZEBOX );
SetWindowLong ( hwndChild, GWL_STYLE, dwStyle );
InvalidateRect ( hwndChild, NULL, true );
SetWindowPos ( hwndChild, NULL, 0, 0, wWidth, wHeight, SWP_NOMOVE | SWP_NOZORDER );
SetWindowLong ( hwndChild, GWL_EXSTYLE, GetWindowLong ( hwndChild, GWL_EXSTYLE ) | WS_EX_LAYERED );
SetLayeredWindowAttributes ( hwndChild, g_ColourKey, 0, LWA_COLORKEY );
}
};
父窗口(它是顶层窗口)本身使用相同的函数来使某些部分透明,具有以下 dwStyle : WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN