Create a transparent form using VC

Source: Internet
Author: User
Tags transparent color

You can use SetLayeredWindowAttributes to easily create transparent forms. This function is only supported at w2k and later. If you want to use it directly, you may need to download the latest SDK. However, this function is implemented in user32.dll of w2k. If you do not want to download a huge sdk, you can directly use GetProcAddress to obtain the pointer of this function.

The function prototype of SetLayeredWindowAttributes is as follows:

BOOL SetLayeredWindowAttributes (
HWND 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. (Note that it cannot be used in win9x)
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 include LWA_ALPHA and LWA_COLORKEY.

If LWA_ALPHA is set, bAlpha is used to determine the transparency.

If the LWA_COLORKEY is set, the transparent color is specified as the crKey, and other colors are displayed normally.

To make the form transparent, you must first have the WS_EX_LAYERED extended attribute (the old sdk does not define this attribute, so you can directly specify it as 0x80000 ).

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;
// Get the SetLayeredWindowAttributes function pointer
Fun = (MYFUNC) GetProcAddress (hInst, "SetLayeredWindowAttributes ");
If (fun) fun (this-> GetSafeHwnd (), 0,128, 2 );
FreeLibrary (hInst );
}

A slight modification can also make a fade-out effect. Note that the third parameter (128) should not be too small. If it is 0, it will be completely transparent and invisible.

How to make the frame window icons animated

TIMER can be used, but TIMER cannot be used for timing. TIMER sends window messages. TIMER cannot be processed in a timely manner when the window is busy with messages such as keyboard and mouse, which leads to a long interval.

You can use a separate TIMER thread and Sleep () timing to solve this problem.

UINT Timer (LPVOID param)
{
HWND hWnd = (HWND) param;
While (1)
{
Sleep (MS );
PostMessage (hWnd, CH_PICTURE, NULL, NULL)
}
}

Send custom messages after Sleep (MS. The message processing function selects an ICON or BITMAP for display. For example:

MyBotton. SetBitmap (HBITMAP) Bitmap [I]);

Bitmap is a Bitmap array with j bitmaps. When the message processing function runs once, I accumulates once. When I = j, I returns to 0;

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: info-contact@alibabacloud.com 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.