最简单是用SetLayeredWindowAttributes,背景最好白色 不过这样图像部分也透明了 #ifndef WS_EX_LAYERED #define WS_EX_LAYERED 0x00080000 #define LWA_COLORKEY 0x00000001 #define LWA_ALPHA 0x00000002 #endif // ndef WS_EX_LAYERED // Preparation for the function we want to import from USER32.DLL typedef BOOL (WINAPI *lpfnSetLayeredWindowAttributes)(HWND hWnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags); BOOL Transparent(HWND hWnd, BYTE byAlpha = 150) { lpfnSetLayeredWindowAttributes pSetLayeredWindowAttributes; // Here we import the function from USER32.DLL HMODULE hUser32 = GetModuleHandle("USER32.DLL"); if(hUser32 == NULL) return FALSE; pSetLayeredWindowAttributes = (lpfnSetLayeredWindowAttributes)GetProcAddress(hUser32, "SetLayeredWindowAttributes"); // If the import did not succeed, make sure your app can handle it! if(pSetLayeredWindowAttributes == NULL) return FALSE; //Fail!!! // Check the current state of the dialog, and then add the WS_EX_LAYERED attribute SetWindowLong(hWnd, GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED); pSetLayeredWindowAttributes(hWnd, 0, byAlpha, LWA_ALPHA); return TRUE; }
http://www.zjonline.com.cn/gb/node2/node195/node56935/node56938/userobject7ai24982.html
不过这样图像部分也透明了
#ifndef WS_EX_LAYERED
#define WS_EX_LAYERED 0x00080000
#define LWA_COLORKEY 0x00000001
#define LWA_ALPHA 0x00000002
#endif // ndef WS_EX_LAYERED
// Preparation for the function we want to import from USER32.DLL
typedef BOOL (WINAPI *lpfnSetLayeredWindowAttributes)(HWND hWnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags);
BOOL Transparent(HWND hWnd, BYTE byAlpha = 150)
{
lpfnSetLayeredWindowAttributes pSetLayeredWindowAttributes;
// Here we import the function from USER32.DLL
HMODULE hUser32 = GetModuleHandle("USER32.DLL");
if(hUser32 == NULL)
return FALSE; pSetLayeredWindowAttributes = (lpfnSetLayeredWindowAttributes)GetProcAddress(hUser32, "SetLayeredWindowAttributes");
// If the import did not succeed, make sure your app can handle it!
if(pSetLayeredWindowAttributes == NULL)
return FALSE; //Fail!!! // Check the current state of the dialog, and then add the WS_EX_LAYERED attribute
SetWindowLong(hWnd, GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED);
pSetLayeredWindowAttributes(hWnd, 0, byAlpha, LWA_ALPHA);
return TRUE;
}