Bool
Setlayeredwindowattributes(
Hwnd, // handle to the layered window
Colorref crkey, // specifies the Color Key
Byte balpha, // value for the blend function
DWORD dwflags // action
);
Windows NT/2000/XP: supported ded in Windows 2000 and later.
Windows 95/98/me: unsupported.
Header: declared in winuser. h; Include windows. h.
Library: Use user32.lib.
Some constants:
Ws_ex_layered = 0x80000;
Lwa_alpha = 0x2;
Lwa_colorkey = 0x1
Dwflags contains lwa_alpha and lwa_colorkey. If lwa_alpha is set, balpha is used to determine the transparency. If lwa_colorkey is set, the transparent color is specified as the crkey, and other colors are displayed normally.
Note: To make the form transparent, you must first have the ws_ex_layered extension attribute (which is not available in the old SDK ).
Sample Code:
Add oninitdialog:
// Add the ws_ex_layered extension attribute
Setwindowlong (this-> getsafehwnd (), gwl_exstyle,
Getwindowlong (this-> getsafehwnd (), gwl_exstyle) ^ 0x80000 );
Hinstance hinst = loadlibrary ("user32.dll ");
If (hinst)
{
Typedef bool (winapi * myfunc) (hwnd, colorref, byte, DWORD );
Myfunc fun = NULL;
// ObtainSetlayeredwindowattributesFunction pointer
Fun = (myfunc) getprocaddress (hinst ,"Setlayeredwindowattributes");
If (fun) Fun (this-> getsafehwnd (), 0,128, 2 );
Freelibrary (hinst );
} Add # define lwa_colorkey 0x00000001 to the. h example code.
# Define ws_ex_layered 0x00080000
Typedef bool (winapi * lpfnsetlayeredwindowattributes) (hwnd, colorref crkey, byte balpha, DWORD dwflags );
LpfnsetlayeredwindowattributesSetlayeredwindowattributesIn. cpp, colorref maskcolor = RGB (0x80, 0xc0, 0xff );
Hmodule huser32 = getmodulehandle ("user32.dll ");
Setlayeredwindowattributes= (Lpfnsetlayeredwindowattributes) getprocaddress (huser32 ,"Setlayeredwindowattributes");
Setwindowlong (getsafehwnd (), gwl_exstyle, getwindowlong (getsafehwnd (), gwl_exstyle) | ws_ex_layered );
Setlayeredwindowattributes(Getsafehwnd (), maskcolor, 255, lwa_colorkey );
Freelibrary (huser32 );