Progressive implementation of form Transparency and Other setlayeredwindowattributes usage

Source: Internet
Author: User
Tags transparent color

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.