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 grable2007
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)
Rem
	transparent windows in windows ;)
	
	author: grable
	email : grable0@gmail.com
EndRem

SuperStrict

Import MaxGUI.Drivers

?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

Comments

CASO2007
I tried this with the CreatePanel example, set the colored panel and button to 0.5, and.... NOTHING HAPPENED. Is it me?

PS: I'm using XP


grable2007
As far as i know this wont work inside a window, only on windows them self, sorry :/

Ive corrected the name/title to reflect this, and added an example.

EDIT: There is a way to get it to work, but its a bit hackish. see the example above.


CASO2007
I tried the example. THAT IS AWESOME! I don't really know what to do with it right now, but IT'S AWESOME.


Code Archives Forum