我正在尝试使用位图中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