Ticket #868: LayeredAttrib.au3.txt

File LayeredAttrib.au3.txt, 6.3 KB (added by ProgAndy, 15 years ago)

The funcs and an example

Line 
1#include <GUIConstants.au3>
2#include <Constants.au3>
3#include <WindowsConstants.au3>
4#include <WINAPI.au3>
5
6;############# Constants ##########
7Global Const $LWA_ALPHA = 0x2
8Global Const $LWA_COLORKEY = 0x1
9
10;############# Example ############
11
12#Region - GUI Create
13        $gui = GUICreate("trans", 300, 400, -1, -1, -1, $WS_EX_LAYERED)
14        GUICtrlCreateLabel("This is text on a transparent Layered GUI", 10, 10, 200, 20, -1, $GUI_WS_EX_PARENTDRAG)
15        GUICtrlSetTip(-1, "Click label to drag layered window")
16        $layButt = GUICtrlCreateButton("Button", 10, 40, 40)
17        GUISetBkColor(0xABCDEF)
18        _WinAPI_SetLayeredWindowAttributes($gui, 0x010101)
19        GUISetState()
20        $guicontrol = GUICreate("ControlGUI", 300, 400, 100, 100)
21        $checkTrans = GUICtrlCreateCheckbox("Transparent color 0xABCDEF (Checked) Or 0x010101", 10, 10)
22        $checkBorder = GUICtrlCreateCheckbox("POPUP-Style", 10, 30)
23        GUICtrlCreateLabel("Transparency for Layered GUI", 10, 50)
24        $slidTrans = GUICtrlCreateSlider(10, 70, 200, 30)
25        GUICtrlSetLimit($slidTrans, 255, 0)
26        GUICtrlSetData(-1, 255)
27        GUISetState()
28#EndRegion - GUI Create
29
30#Region - GUI SelectLoop
31While 1
32        $extMsg = GUIGetMsg(1)
33        $msg = $extMsg[0]
34        Switch $extMsg[1]
35                Case $guicontrol
36                        Select
37                                Case $msg = $GUI_EVENT_CLOSE
38                                        Exit
39                                Case $msg = $checkTrans Or $msg = $slidTrans
40                                       
41                                        ; Change Attributes of Trans-Color and Window Transparency
42                                       
43                                        If BitAND(GUICtrlRead($checkTrans), $GUI_CHECKED) = $GUI_CHECKED Then
44                                                _WinAPI_SetLayeredWindowAttributes($gui, 0xABCDEF, GUICtrlRead($slidTrans))
45                                        Else
46                                                _WinAPI_SetLayeredWindowAttributes($gui, 0x010101, GUICtrlRead($slidTrans))
47                                        EndIf
48                                       
49                                Case $msg = $checkBorder
50                                        If BitAND(GUICtrlRead($checkBorder), $GUI_CHECKED) = $GUI_CHECKED Then
51                                                GUISetStyle($WS_POPUP, -1, $gui)
52                                        Else
53                                                GUISetStyle($GUI_SS_DEFAULT_GUI, -1, $gui)
54                                        EndIf
55                        EndSelect
56                Case $gui
57                        Select
58                                Case $msg = $layButt
59                                        Dim $transcolor, $alpha
60                                        $info = _WinAPI_GetLayeredWindowAttributes($gui,$transcolor, $alpha)
61                                        MsgBox(0, 'Layered GUI', "Button on layered Window Clicked" & @CRLF & "Information about this window: " & @CRLF & _
62                                                "Transparent Color: " & $transcolor & @CRLF & _
63                                                "Alpha Value: " & $alpha & @CRLF & _
64                                                "LWA_COLORKEY: " & (BitAND($info,$LWA_COLORKEY)=$LWA_COLORKEY) & @CRLF & _
65                                                "LWA_ALPHA: " & (BitAND($info,$LWA_ALPHA)=$LWA_ALPHA)           )
66                                Case $msg = $GUI_EVENT_CLOSE
67                                        Exit MsgBox(0, '', "Close from Layered GUI")
68                        EndSelect
69        EndSwitch
70WEnd
71#EndRegion - GUI SelectLoop
72
73;############# EndExample #########
74
75;===============================================================================
76;
77; Function Name:   _WinAPI_SetLayeredWindowAttributes
78; Description::    Sets Layered Window Attributes:) See MSDN for more informaion
79; Parameter(s):
80;                  $hwnd - Handle of GUI to work on
81;                  $i_transcolor - Transparent color
82;                  $Transparency - Set Transparancy of GUI
83;                  $isColorRef - If True, $i_transcolor is a COLORREF( 0x00bbggrr ), else an RGB-Color
84; Requirement(s):  Layered Windows
85; Return Value(s): Success: 1
86;                  Error: 0
87;                   @error: 1 to 3 - Error from DllCall
88;                   @error: 4 - Function did not succeed - use
89;                               _WinAPI_GetLastErrorMessage or _WinAPI_GetLastError to get more information
90; Author(s):       Prog@ndy
91;
92; Link :           @@MsdnLink@@ SetLayeredWindowAttributes
93; Example :        Yes
94;===============================================================================
95;
96Func _WinAPI_SetLayeredWindowAttributes($hwnd, $i_transcolor, $Transparency = 255, $dwFlages = 0x03, $isColorRef = False)
97        If $dwFlages = Default Or $dwFlages = "" Or $dwFlages < 0 Then $dwFlages = 0x03
98        If Not $isColorRef Then
99                $i_transcolor = Hex(String($i_transcolor), 6)
100                $i_transcolor = Execute('0x00' & StringMid($i_transcolor, 5, 2) & StringMid($i_transcolor, 3, 2) & StringMid($i_transcolor, 1, 2))
101        EndIf
102        Local $Ret = DllCall("user32.dll", "int", "SetLayeredWindowAttributes", "hwnd", $hwnd, "long", $i_transcolor, "byte", $Transparency, "long", $dwFlages)
103        Select
104                Case @error
105                        Return SetError(@error, 0, 0)
106                Case $Ret[0] = 0
107                        Return SetError(4, _WinAPI_GetLastError(), 0)
108                Case Else
109                        Return 1
110        EndSelect
111EndFunc   ;==>_WinAPI_SetLayeredWindowAttributes
112
113;===============================================================================
114;
115; Function Name:   _WinAPI_GetLayeredWindowAttributes
116; Description::    Gets Layered Window Attributes:) See MSDN for more informaion
117; Parameter(s):
118;                  $hwnd - Handle of GUI to work on
119;                  $i_transcolor - Returns Transparent color ( dword as 0x00bbggrr  or string "0xRRGGBB")
120;                  $Transparency - Returns Transparancy of GUI
121;                  $isColorRef - If True, $i_transcolor will be a COLORREF( 0x00bbggrr ), else an RGB-Color
122; Requirement(s):  Layered Windows
123; Return Value(s): Success: Usage of LWA_ALPHA and LWA_COLORKEY (use BitAnd)
124;                  Error: 0
125;                   @error: 1 to 3 - Error from DllCall
126;                   @error: 4 - Function did not succeed
127;                               - use _WinAPI_GetLastErrorMessage or _WinAPI_GetLastError to get more information
128;                               - @extended contains _WinAPI_GetLastError
129; Author(s):       Prog@ndy
130;
131; Link :           @@MsdnLink@@ GetLayeredWindowAttributes
132; Example :        Yes
133;===============================================================================
134;
135Func _WinAPI_GetLayeredWindowAttributes($hwnd, ByRef $i_transcolor, ByRef $Transparency,$asColorRef = False)
136        $i_transcolor = -1
137        $Transparency = -1
138        Local $Ret = DllCall("user32.dll", "int", "GetLayeredWindowAttributes", "hwnd", $hwnd, "long*", $i_transcolor, "byte*", $Transparency, "long*", 0)
139        Select
140                Case @error
141                        Return SetError(@error, 0, 0)
142                Case $Ret[0] = 0
143                        Return SetError(4, _WinAPI_GetLastError(), 0)
144                Case Else
145                        If Not $asColorRef Then
146                                $Ret[2] = Hex(String($Ret[2]), 6)
147                                $Ret[2] = '0x' & StringMid($Ret[2], 5, 2) & StringMid($Ret[2], 3, 2) & StringMid($Ret[2], 1, 2)
148                        EndIf
149                        $i_transcolor = $Ret[2]
150                        $Transparency = $Ret[3]
151                        Return $Ret[4]
152        EndSelect
153EndFunc   ;==>_WinAPI_GetLayeredWindowAttributes