Winapi transparent form setlayeredwindowattributes

Source: Internet
Author: User
Tags transparent color

Setlayeredwindowattributes
Bool setlayeredwindowattributes (
Hwnd,
Colorref crkey,
Byte balpha,
DWORD dwflags
);

Hwnd is the handle of a transparent form,
Crkey is the color value,
Balpha is transparency and the value range is [0,255].
Dwflags is transparent and can take two values:
When the value is lwa_alpha, The crkey parameter is invalid and the balpha parameter is valid;
When the value is lwa_colorkey, The balpha parameter is valid and all the areas in the form where the color is crkey will become transparent.
Lwa_alpha = 0x2
Lwa_colorkey = 0x1

To make the form transparent, you must first have the ws_ex_layered extension attribute.
(The old SDK does not define this attribute, so it can be directly specified as 0x80000 ).
Ws_ex_layered = 0x80000

Bytes -----------------------------------------------------------------------------------------
Setwindowlong (

This-> getsafehwnd (),

Gwl_exstyle,
Getwindowlong (this-> getsafehwnd (), gwl_exstyle) | 0x80000

);

// Get the setlayeredwindowattributes address
Hinstance hinst = loadlibrary ("user32.dll ");
If (hinst)
{
Typedef bool (winapi * myfunc) (hwnd, colorref, byte, DWORD );
Myfunc psetlayeredwindowattributes = NULL;

// Get the setlayeredwindowattributes function pointer
Psetlayeredwindowattributes = (myfunc) getprocaddress (hinst, "setlayeredwindowattributes ");
If (psetlayeredwindowattributes)
Psetlayeredwindowattributes (this-> getsafehwnd (), 0,128, 2 );
Freelibrary (hinst );
}

We can see that this function address is dynamically obtained from user32.dl and then called. In vs2005 environment, I can directly use setlayeredwindowattributes. The Code is as follows:
Setwindowlong (this-> getsafehwnd (), gwl_exstyle, getwindowlong (this-> getsafehwnd (), gwl_exstyle) ^ ws_ex_layered );
Setlayeredwindowattributes (this-> m_hwnd, 0,100, 2 );
If such code is used under vc6, an error will be reported in the future: the identifier setlayered?wattributes cannot be found. Why?
It turns out that the setlayeredwindowattributes function is only supported by systems above Win2000, while VC is a product of 98 years (almost 10 years), and its own SDK is naturally 98 years, therefore, the header file of this function and the corresponding Lib. after knowing the cause, you can easily download the SDK and start thinking about the latest one. it was win2003 in, but it was not supported by VC in the description. I went on to find an SDK for XP SP2, which is: external :(
Finally, the first two methods above are relatively secure, because the address of this function cannot be found under 98, but it is estimated that there will be no problem in the second method, now it's XP, and I just talked about how to play my computer for N years. haha.

Bytes ----------------------------------------------------------------------------------------
Usage example in VB:

Function setlayeredwindowattributes
With this function, you can easily implement a translucent form. According to Microsoft's requirements, when creating a transparent form, use the ws_ex_layered parameter (using createmediawex) or set this parameter after creation (using setwindowlong). I select the latter. All functions and constants are declared as follows:
Private declare function getwindowlong lib "USER32" alias "getwindowlonga" (byval hwnd as long, byval nindex as long) as long
Private declare function setwindowlong lib "USER32" alias "setwindowlonga" (byval hwnd as long, byval nindex as long, byval dwnewlong as long) as long
Private declare function setlayeredwindowattributes lib "USER32" (byval hwnd as long, byval crkey as long, byval balpha as byte, byval dwflags as long) as long
Hwnd is the handle of the transparent form, crkey is the color value, balpha is the transparency, value range is [0,255], dwflags is the transparent mode, you can take two values: When the value is lwa_alpha, the crkey parameter is invalid, and the balpha parameter is valid. When the value is lwa_colorkey, The balpha parameter is valid and all the areas in the form where the color is crkey will become transparent-this function is useful: instead of calling a lot of area analysis, creation, and merging functions to create an irregular form, you just need to specify the color value in the transparent area! See the specific code.
Private const ws_ex_layered = & h80000
Private const gwl_exstyle = (-20)
Private const lwa_alpha = & H2
Private const lwa_colorkey = & H1
Code 1: A translucent form
Private sub form_load ()
Dim RTN as long
RTN = getwindowlong (hwnd, gwl_exstyle)
RTN = RTN or ws_ex_layered
Setwindowlong hwnd, gwl_exstyle, RTN
Setlayeredwindowattributes hwnd, 0,200, lwa_alpha
End sub

Code 2: Irregular forms
Private sub form_load ()
Dim RTN as long
Borderstyler = 0
RTN = getwindowlong (hwnd, gwl_exstyle)
RTN = RTN or ws_ex_layered
Setwindowlong hwnd, gwl_exstyle, RTN
Setlayeredwindowattributes hwnd, & hff0000, 0, lwa_colorkey 'will be deducted from the blue in the window
End sub

Bytes ----------------------------------------------------------------------------------------

Delphi call instance:

Bytes ----------------------------------------------------------------------------------------

// Declaration:
Setlayeredwindowattributes(
Hwnd: thandle; {window handle}
Crkey: colorref; {transparent color}
Balpha: byte; {Alpha value}
Dwflags: DWORD {lwa_colorkey (= 1) indicates transparent color; lwa_alpha (= 2) indicates Alpha value}
): Boolean; {whether the setting is successful}

// Example (to control the transparency of external programs, use a calculator to give an example ):
UnitUnit1;

Interface

Uses
Windows, messages, sysutils, variants, classes, graphics, controls, forms,
Dialogs, stdctrls;

Type
Tform1 =Class(Tform)
Button1: tbutton;
Button2: tbutton;
ProcedureButton1click (Sender: tobject );
ProcedureButton2click (Sender: tobject );
End;

VaR
Form1: tform1;

Implementation

{$ R *. DFM}

{Set the Alpha transparency of the calculator}
ProcedureTform1.button1click (Sender: tobject );
VaR
H: hwnd;
Formstyle: integer;
Begin
H: = findwindow ('scical ',Nil);
Formstyle: = getwindowlong (handle, gwl_exstyle );
Setwindowlong (H, gwl_exstyle, formstyleOrWs_ex_layered );
Setlayeredwindowattributes(H, 0,128, lwa_alpha );
End;

{Set the white transparency in the calculator}
ProcedureTform1.button2click (Sender: tobject );
VaR
H: hwnd;
Formstyle: integer;
Begin
H: = findwindow ('scical ',Nil);
Formstyle: = getwindowlong (handle, gwl_exstyle );
Setwindowlong (H, gwl_exstyle, formstyleOrWs_ex_layered );
Setlayeredwindowattributes(H, clwhite, 255, lwa_colorkey );
End;

End.

 

From: http://hi.baidu.com/qqlt/blog/item/3182fbd35895d7d6a8ec9a7a.html

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.