Code archives/Miscellaneous/[MaxGUI/Win32]: Transparent Window Gadgets

This code has been declared by its author to be Public Domain code.

Download source code

[MaxGUI/Win32]: Transparent Window Gadgets by grable
(Posted 19 years ago)
Example:
SuperStrict

Import "LayeredGadgets.bmx"

Global window:TGadget = CreateWindow( "Window", 64,64, 256,256, Null, WINDOW_TITLEBAR | WINDOW_RESIZABLE | WINDOW_CLIENTCOORDS)
Global panel:TGadget = CreatePanel( 64, 64,  128,128, window)

SetPanelColor panel, 255,0,0

SetGadgetLayered window, True
SetGadgetLayeredAttribs window, $FF, 128

While WaitEvent()	
	If EventID() = EVENT_WINDOWCLOSE Then Exit
Wend
End


Hackish code to get it working inside windows (by using a second window)
SuperStrict

Import "LayeredGadgets.bmx"

Global window:TGadget = CreateWindow( "Window", 64,64, 256,256, Null, WINDOW_TITLEBAR | WINDOW_RESIZABLE | WINDOW_CLIENTCOORDS)
Global panel:TGadget = CreatePanel( 64, 64,  128,128, window)

' the fake window with the controls
Global window2:TGadget = CreateWindow( "Window2", 32,32, 64,64, window, WINDOW_CLIENTCOORDS)
Global button:TGadget = CreateButton( "Button", 4,4, 56,25, window2)

SetPanelColor panel, 255,0,0

SetGadgetLayered window2, True
SetGadgetAlpha window2, 0.5

' set the timer to make sure the window stays where we tell it to
CreateTimer 1000
AddHook EmitEventHook, move_window_hook

While WaitEvent()	
	If EventID() = EVENT_WINDOWCLOSE Then Exit
Wend
End


Extern "Win32"
	Function ClientToScreen:Int( hwnd:Int, point:Long Var)
EndExtern

Function move_window_hook:Object( id:Int, data:Object, context:Object)
	Local ev:TEvent = TEvent( data)
	If ev Then
		If ev.ID = EVENT_TIMERTICK Then
			' get the position of main window, and keep the child window in place
			Local point:Long	
			ClientToScreen( window.Query(QUERY_HWND), point)
			Local x:Int = Int Ptr( Varptr point)[0]
			Local y:Int = Int Ptr( Varptr point)[1]
			SetGadgetShape( window2, x+64,y+64, GadgetWidth(window2),GadgetHeight(window2))
			Return Null
		EndIf
	EndIf
	Return data
EndFunction
Rem
	transparent windows in windows ;)
	
	author: grable
	email : grable0@gmail.com
EndRem

SuperStrict

Import BRL.MaxGUI

?MacOS
DebugLog "LayeredGadgets.bmx doesnt support MacOS"
?Linux
DebugLog "LayeredGadgets.bmx doesnt support Linux"
?

Private

?Win32
Extern "Win32"
	Const GWL_EXSTYLE:Int = -20
	Const GWL_STYLE:Int = -16
	
	Const WS_EX_TRANSPARENT:Int = $20
	Const WS_EX_LAYERED:Int = $80000
	
	Const LWA_COLORKEY:Int = $1
	Const LWA_ALPHA:Int = $2

	Function GetWindowLong:Int( hwnd:Int, index:Int) = "GetWindowLongA@8"
	Function SetWindowLong:Int( hwnd:Int, index:Int, value:Int) = "SetWindowLongA@12"
	Function SetLayeredWindowAttributes:Int( hwnd:Int, ckey:Int, alpha:Byte, flags:Int)

'	typedef struct _BLENDFUNCTION {
'	  	Byte     BlendOp;
'	  	Byte     BlendFlags;
'	  	Byte     SourceConstantAlpha;
'	  	Byte     AlphaFormat;
'	}BLENDFUNCTION, *PBLENDFUNCTION, *LPBLENDFUNCTION;
'	Function 	UpdateLayeredWindow:Int( hwnd:Int, destdc:Int, destpoint:Long Var, size:Long Var, srcdc:Int, srcpoint:Long Var, ckey:Int, blendfunc:Byte Ptr, flags:Int)
EndExtern
?

Public

Function SetGadgetLayered( gadget:TGadget, layered:Int, transparent:Int = False)
	?Win32
	Local handle:Int = QueryGadget( gadget, QUERY_HWND)
	Local style:Int = GetWindowLong( handle, GWL_EXSTYLE)
	If layered Then 
		style :| WS_EX_LAYERED
	Else
		style :& ~WS_EX_LAYERED
	EndIf
	If transparent  Then 
		style:| WS_EX_TRANSPARENT
	Else
		style :& ~WS_EX_TRANSPARENT
	EndIf
	SetWindowLong( handle, GWL_EXSTYLE, style)
	?
EndFunction

Function SetGadgetColorKey( gadget:TGadget, ckey:Int)
	?Win32
	Local handle:Int = QueryGadget( gadget, QUERY_HWND)
	SetLayeredWindowAttributes( handle, ckey, 0, LWA_COLORKEY)
	?
EndFunction

?Win32
Function SetGadgetAlpha( gadget:TGadget, alpha:Float)
	Local handle:Int = QueryGadget( gadget, QUERY_HWND)
	SetLayeredWindowAttributes( handle, 0, Byte(alpha * 255), LWA_ALPHA)
EndFunction
?

Function SetGadgetLayeredAttribs( gadget:TGadget, ckey:Int, alpha:Byte)
	?Win32	
	Local handle:Int = QueryGadget( gadget, QUERY_HWND)
	SetLayeredWindowAttributes( handle, ckey, alpha, LWA_COLORKEY | LWA_ALPHA)
	?	
EndFunction