VB omnipotent 4: creating transparent and translucent forms
-- Author: bellWe can often see such a form, and feel Very dazzling: Similarly, for programming on Windows systems, it seems that the first thing we think of is definitely not VB, and most Programmers think of VC. In fact, VB is very convenient and simple for this implementation, and uses the setlayeredwindowattributes () function in "USER32.
Setlayeredwindowattributes ()Function Description: function declaration:
Declare function using lib "USER32" () 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 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. Next we will do two experiments:First: Create a translucent form Step 1:Open VB and create a formStep 2:Set the background color of the form to: & hff0000Step 3:Paste the following code into the program: declare function getwindowlong lib "USER32" alias "getwindowlonga" () Declare function getwindowlong lib "USER32" alias "getwindowlonga" (byval hwnd as long, byval nindex as long) as long
Declare function setwindowlong lib "USER32" alias "setwindowlonga" () Declare function setwindowlong lib "USER32" alias "setwindowlonga" (byval hwnd as long, byval nindex as long, byval dwnewlong as long) as long
Declare function using lib "USER32" () Declare function setlayeredwindowattributes lib "USER32" (byval hwnd as long, byval crkey as long, byval balpha as byte, byval dwflags as long) as long
Private const ws_ex_layered = & h80000
Const gwl_exstyle = () const gwl_exstyle = (-20)
Private const lwa_alpha = & H2
Private const lwa_colorkey = & H1
Sub form_load () 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,100, lwa_alpha
The end sub running result is as follows:First: create a special form Place an image on the previous form:Paste the following code into the program: getwindowlong lib "USER32" alias "getwindowlonga" () getwindowlong lib "USER32" alias "getwindowlonga" (getwindowlong lib "USER32" alias "getwindowlonga "() getwindowlong lib "USER32" alias "getwindowlonga" (byval hwnd as long, byval nindex as long) as long
Setwindowlong lib "USER32" alias "setwindowlonga" () setwindowlong lib "USER32" alias "setwindowlonga" (setwindowlong lib "USER32" alias "setwindowlonga "() setwindowlong lib "USER32" alias "setwindowlonga" (byval hwnd as long, byval nindex as long, byval dwnewlong as long) as long
Using lib "USER32" () setlayeredwindowattributes lib "USER32" (setlayeredwindowattributes lib "USER32" () setlayeredwindowattributes lib "USER32" (byval hwnd as long, byval crkey as long, byval balpha as byte, byval dwflags as long) as long
Private const ws_ex_layered = & h80000
Const gwl_exstyle = () const gwl_exstyle = (-20)
Private const lwa_alpha = & H2
Private const lwa_colorkey = & H1
Sub form_load () 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
The end sub running result is as follows: