#include <GUIConstants.au3>
#include <Constants.au3>
#include <WindowsConstants.au3>
#include <WINAPI.au3>

;############# Constants ##########
Global Const $LWA_ALPHA = 0x2
Global Const $LWA_COLORKEY = 0x1

;############# Example ############

#Region - GUI Create
	$gui = GUICreate("trans", 300, 400, -1, -1, -1, $WS_EX_LAYERED)
	GUICtrlCreateLabel("This is text on a transparent Layered GUI", 10, 10, 200, 20, -1, $GUI_WS_EX_PARENTDRAG)
	GUICtrlSetTip(-1, "Click label to drag layered window")
	$layButt = GUICtrlCreateButton("Button", 10, 40, 40)
	GUISetBkColor(0xABCDEF)
	_WinAPI_SetLayeredWindowAttributes($gui, 0x010101)
	GUISetState()
	$guicontrol = GUICreate("ControlGUI", 300, 400, 100, 100)
	$checkTrans = GUICtrlCreateCheckbox("Transparent color 0xABCDEF (Checked) Or 0x010101", 10, 10)
	$checkBorder = GUICtrlCreateCheckbox("POPUP-Style", 10, 30)
	GUICtrlCreateLabel("Transparency for Layered GUI", 10, 50)
	$slidTrans = GUICtrlCreateSlider(10, 70, 200, 30)
	GUICtrlSetLimit($slidTrans, 255, 0)
	GUICtrlSetData(-1, 255)
	GUISetState()
#EndRegion - GUI Create

#Region - GUI SelectLoop
While 1
	$extMsg = GUIGetMsg(1)
	$msg = $extMsg[0]
	Switch $extMsg[1]
		Case $guicontrol
			Select
				Case $msg = $GUI_EVENT_CLOSE
					Exit
				Case $msg = $checkTrans Or $msg = $slidTrans
					
					; Change Attributes of Trans-Color and Window Transparency
					
					If BitAND(GUICtrlRead($checkTrans), $GUI_CHECKED) = $GUI_CHECKED Then
						_WinAPI_SetLayeredWindowAttributes($gui, 0xABCDEF, GUICtrlRead($slidTrans))
					Else
						_WinAPI_SetLayeredWindowAttributes($gui, 0x010101, GUICtrlRead($slidTrans))
					EndIf
					
				Case $msg = $checkBorder
					If BitAND(GUICtrlRead($checkBorder), $GUI_CHECKED) = $GUI_CHECKED Then
						GUISetStyle($WS_POPUP, -1, $gui)
					Else
						GUISetStyle($GUI_SS_DEFAULT_GUI, -1, $gui)
					EndIf
			EndSelect
		Case $gui
			Select
				Case $msg = $layButt
					Dim $transcolor, $alpha
					$info = _WinAPI_GetLayeredWindowAttributes($gui,$transcolor, $alpha)
					MsgBox(0, 'Layered GUI', "Button on layered Window Clicked" & @CRLF & "Information about this window: " & @CRLF & _
						"Transparent Color: " & $transcolor & @CRLF & _
						"Alpha Value: " & $alpha & @CRLF & _
						"LWA_COLORKEY: " & (BitAND($info,$LWA_COLORKEY)=$LWA_COLORKEY) & @CRLF & _
						"LWA_ALPHA: " & (BitAND($info,$LWA_ALPHA)=$LWA_ALPHA) 		)
				Case $msg = $GUI_EVENT_CLOSE
					Exit MsgBox(0, '', "Close from Layered GUI")
			EndSelect
	EndSwitch
WEnd
#EndRegion - GUI SelectLoop

;############# EndExample #########

;===============================================================================
;
; Function Name:   _WinAPI_SetLayeredWindowAttributes
; Description::    Sets Layered Window Attributes:) See MSDN for more informaion
; Parameter(s):
;                  $hwnd - Handle of GUI to work on
;                  $i_transcolor - Transparent color
;                  $Transparency - Set Transparancy of GUI
;                  $isColorRef - If True, $i_transcolor is a COLORREF( 0x00bbggrr ), else an RGB-Color
; Requirement(s):  Layered Windows
; Return Value(s): Success: 1
;                  Error: 0
;                   @error: 1 to 3 - Error from DllCall
;                   @error: 4 - Function did not succeed - use
;                               _WinAPI_GetLastErrorMessage or _WinAPI_GetLastError to get more information
; Author(s):       Prog@ndy
;
; Link :           @@MsdnLink@@ SetLayeredWindowAttributes
; Example :        Yes
;===============================================================================
;
Func _WinAPI_SetLayeredWindowAttributes($hwnd, $i_transcolor, $Transparency = 255, $dwFlages = 0x03, $isColorRef = False)
	If $dwFlages = Default Or $dwFlages = "" Or $dwFlages < 0 Then $dwFlages = 0x03
	If Not $isColorRef Then
		$i_transcolor = Hex(String($i_transcolor), 6)
		$i_transcolor = Execute('0x00' & StringMid($i_transcolor, 5, 2) & StringMid($i_transcolor, 3, 2) & StringMid($i_transcolor, 1, 2))
	EndIf
	Local $Ret = DllCall("user32.dll", "int", "SetLayeredWindowAttributes", "hwnd", $hwnd, "long", $i_transcolor, "byte", $Transparency, "long", $dwFlages)
	Select
		Case @error
			Return SetError(@error, 0, 0)
		Case $Ret[0] = 0
			Return SetError(4, _WinAPI_GetLastError(), 0)
		Case Else
			Return 1
	EndSelect
EndFunc   ;==>_WinAPI_SetLayeredWindowAttributes

;===============================================================================
;
; Function Name:   _WinAPI_GetLayeredWindowAttributes
; Description::    Gets Layered Window Attributes:) See MSDN for more informaion
; Parameter(s):
;                  $hwnd - Handle of GUI to work on
;                  $i_transcolor - Returns Transparent color ( dword as 0x00bbggrr  or string "0xRRGGBB")
;                  $Transparency - Returns Transparancy of GUI
;                  $isColorRef - If True, $i_transcolor will be a COLORREF( 0x00bbggrr ), else an RGB-Color
; Requirement(s):  Layered Windows
; Return Value(s): Success: Usage of LWA_ALPHA and LWA_COLORKEY (use BitAnd)
;                  Error: 0
;                   @error: 1 to 3 - Error from DllCall
;                   @error: 4 - Function did not succeed
;                               - use _WinAPI_GetLastErrorMessage or _WinAPI_GetLastError to get more information
;                               - @extended contains _WinAPI_GetLastError
; Author(s):       Prog@ndy
;
; Link :           @@MsdnLink@@ GetLayeredWindowAttributes
; Example :        Yes
;===============================================================================
;
Func _WinAPI_GetLayeredWindowAttributes($hwnd, ByRef $i_transcolor, ByRef $Transparency,$asColorRef = False)
	$i_transcolor = -1
	$Transparency = -1
	Local $Ret = DllCall("user32.dll", "int", "GetLayeredWindowAttributes", "hwnd", $hwnd, "long*", $i_transcolor, "byte*", $Transparency, "long*", 0)
	Select
		Case @error
			Return SetError(@error, 0, 0)
		Case $Ret[0] = 0
			Return SetError(4, _WinAPI_GetLastError(), 0)
		Case Else
			If Not $asColorRef Then
				$Ret[2] = Hex(String($Ret[2]), 6)
				$Ret[2] = '0x' & StringMid($Ret[2], 5, 2) & StringMid($Ret[2], 3, 2) & StringMid($Ret[2], 1, 2)
			EndIf
			$i_transcolor = $Ret[2]
			$Transparency = $Ret[3]
			Return $Ret[4]
	EndSelect
EndFunc   ;==>_WinAPI_GetLayeredWindowAttributes