[class] LayeredWindow ( WS_EX_LAYERED )

Post your working scripts, libraries and tools
User avatar
Hellbent
Posts: 1061
Joined: 23 Sep 2017, 13:34

[class] LayeredWindow ( WS_EX_LAYERED )

16 Feb 2020, 17:05

This is a simple class to make setting up and using a layered gui window ( WS_EX_LAYERED / E0x80000 ) quick and easy.

***Requires the gdip lib***

It currently has a way to automatically add a trigger to allow click and drag movement of the window, but it only works for windows 8+ ( 8 or 10 ).
If you're running windows 7 or lower you will need to use windows messages / etc. to do that.

Here is the class

Code: Select all

;***********************************************************************************************************************************************************************
;***																																								 ***
;***																		Layered Window Class																	 ***
;***																																								 ***
;***********************************************************************************************************************************************************************

class LayeredWindow	{
;LayeredWindow class By: Hellbent
	__New( x := 0 , y := 0 , w := 100 , h := 100 , window := 1 , title := " " , smoothing := 4 , options := "" , autoShow := 1 , GdipStart := 0 , WinMover := "" , BackgroundColor := "" ){
		This.X := x , This.Y := y , This.W := w , This.H := h 
		This.Window := window , This.Title := title
		This.Options := options , This.Smoothing := smoothing
		( GdipStart ) ? ( This.Token := Gdip_Startup() )
		This._CreateWindow()
		( autoShow ) ? ( This.ShowWindow() )
		This._SetUpdateLayeredWindow()
		( WinMover ) ? ( This._AddMoveTrigger( WinMover ) )
		( BackgroundColor ) ? ( This.PaintBackground( BackgroundColor , 1 ) )
	}
	_CreateWindow(){
		Gui , % This.Window ": New" , % " +E0x80000 +LastFound -Caption " This.Options 
		This.Hwnd := WinExist()
		This.hbm := CreateDIBSection( This.W , This.H )
		This.hdc := CreateCompatibleDC()
		This.obm := SelectObject( This.hdc , This.hbm )
		This.G := Gdip_GraphicsFromHDC( This.hdc )
		Gdip_SetSmoothingMode( This.G , This.Smoothing )
	}
	_SetUpdateLayeredWindow(){
		UpdateLayeredWindow( This.hwnd , This.hdc , This.X , This.Y , This.W , This.H )
	}
	UpdateWindow(){
		UpdateLayeredWindow( This.hwnd , This.hdc )
	}
	ShowWindow( update := 1 , Position := "" ){ 
		if( !Position )
			Gui , % This.Window ": Show" , % "w" This.W " h" This.H  , % ( This.Title ) ? ( This.Title ) : ( "" )
		else
			Gui , % This.Window ": Show" , % "x" Position.X " y" Position.Y "w" Position.W " h" Position.H  , % ( This.Title ) ? ( This.Title )  : ( "" )
		( update ) ? ( This.UpdateWindow() )
	}
	_AddMoveTrigger( positons ){ 
		local hwnd , bd
		Gui , % This.Window " : Add" , Text , % "x" positons.x " y" positons.y " w" positons.w " h" positons.h " hwndhwnd"
		This.MoveHwnd := hwnd
		bd := This._WindowMover.Bind( This )
		GuiControl , % This.Window ": +G" , % This.MoveHwnd , % bd
	}
	_WindowMover(){ 
		PostMessage, 0xA1 , 2
	}
	PaintBackground( BackgroundColor := "0xFF000000" , update := 0){ 
		local Brush
		This.BackgroundColor := BackgroundColor
		Brush := Gdip_BrushCreateSolid( BackgroundColor )
		Gdip_FillRectangle( This.G , Brush , 0 , 0 , This.W , This.H )
		Gdip_DeleteBrush( Brush )
		( update ) ? ( This.UpdateWindow() )
	}
	Draw( pBitmap , Positions := "" , update := 1 , disposeBitmap := 0  , PaintBackground := 0){ 
	(PaintBackground) ? ( This.PaintBackground( This.BackgroundColor ) )	
		Gdip_DrawImage( This.G 
						, pBitmap 
						, ( Positions.X1 ) ? ( Positions.X1 ) : ( Positions.X ) ? ( Positions.X ) : ( "" )
						, ( Positions.Y1 ) ? ( Positions.Y1 ) : ( Positions.Y ) ? ( Positions.Y ) : ( "" ) 
						, ( Positions.W1 ) ? ( Positions.W1 ) : ( Positions.W ) ? ( Positions.W ) : ( "" ) 
						, ( Positions.H1 ) ? ( Positions.H1 ) : ( Positions.H ) ? ( Positions.H ) : ( "" ) 
						, ( Positions.X2 ) ? ( Positions.X2 ) : ( Positions.SX ) ? ( Positions.SX ) : ( "" )
						, ( Positions.Y2 ) ? ( Positions.Y2 ) : ( Positions.SY ) ? ( Positions.SY ) : ( "" )
						, ( Positions.W2 ) ? ( Positions.W2 ) : ( Positions.SW ) ? ( Positions.SW ) : ( "" )
						, ( Positions.H2 ) ? ( Positions.H2 ) : ( Positions.SH ) ? ( Positions.SH ) : ( "" ) )
		( update ) ? ( This.UpdateWindow() )
		( disposeBitmap ) ? ( Gdip_DisposeImage( pBitmap ) )
	}
	ClearWindow( update := "" ){ 
		Gdip_GraphicsClear( This.G )
		( update ) ? ( This.UpdateWindow() )
	}
	Add_Trigger(PositionObject,Label:="GuiClose"){
		Gui, % This.Window ": Add", Text, % "x" PositionObject.X " y" PositionObject.Y " w" PositionObject.W " h" PositionObject.H " g" Label " BackgroundTrans"
	}
	Draw_Text(PosObj,Text,Font:="Arial",FontSize:="12",Color1:="0xFFFFFFFF",Color2:="",PosObj2:="",update:=0){
		local Brush
		(!color2&&color2!="000000")?(Color2:=Color1)
		(StrLen(Color1)=6)?( Color1 := "0xFF" Color1 )
		(StrLen(Color2)=6)?( Color2 := "0xFF" Color2 )
		(PosObj2="")?(PosObj2:=PosObj)
		Brush := Gdip_CreateLineBrushFromRect( 0 , 0 , PosObj2.W , PosObj2.H , Color1 , Color2 , 1 , 1 )
		Gdip_TextToGraphics(This.G, Text, "s" FontSize " c" Brush " Center vCenter Bold x" PosObj.X " y" PosObj.Y, Font , PosObj.W, PosObj.H)
		Gdip_DeleteBrush( Brush )
		( update ) ? ( This.UpdateWindow() )
	}
	Draw_Rectangle(Thickness:="5",PosObj:="",Color1:=0xFFFFFFFF,Color2:="",PosObj2:="",update:=0){
		local Brush, pPen
		(!color2)?(Color2:=Color1)
		(StrLen(Color1)=6)?( Color1 := "0xFF" Color1 )
		(StrLen(Color2)=6)?( Color2 := "0xFF" Color2 )
		(PosObj2="")?(PosObj2:=PosObj)
		Brush := Gdip_CreateLineBrushFromRect( 0 , 0 , PosObj2.W , PosObj2.H , Color1 , Color2 , 1 , 1 )
		pPen := Gdip_CreatePenFromBrush(Brush, Thickness)
		Gdip_DrawRectangle( This.G, pPen, PosObj.X , PosObj.Y , PosObj.W , PosObj.H )
		Gdip_DeleteBrush( Brush )
		Gdip_DeletePen(pPen)
		( update ) ? ( This.UpdateWindow() )
	}
	DeleteWindow( TurnOffGdip := 0 ){
		SelectObject( This.hdc , This.obm )
		DeleteObject( This.hbm )
		DeleteDC( This.hdc )
		Gdip_DeleteGraphics( This.G )
		( TurnOffGdip && This.Token ) ? ( Gdip_Shutdown( This.Token ) )
		Gui, % This.Window " : Destroy"
	}
}

Here is a simple example of using the class.

Code: Select all

;***************************************************************************************************
;***************************************************************************************************
#Include <My Altered Gdip Lib>  ;<------       Replace with your copy of GDIP
;***************************************************************************************************
;***************************************************************************************************
#Include <LayeredWindow class>  ;<------- Replace 
;***************************************************************************************************
;***************************************************************************************************
#SingleInstance,Force
SetBatchLines -1
OnExit,GuiClose

Window := New LayeredWindow( x := "" , y := "", w := 300 , h := 100 , window := 1 , title := "Test Window" , smoothing := 4 , options := "+AlwaysOnTop" , autoShow := 1 , GdipStart := 1 , { x: 0 , y: 0 , w: 300 , h: 42 } , BackgroundColor := "0xFF222529" )


return
GuiClose:
GuiContextMenu:
*ESC::
	Window.DeleteWindow( 1 )
	ExitApp

;________________________________________________________________________________________________________
;________________________________________________________________________________________________________
;        <><><><><>   Testing   <><><><><>
;________________________________________________________________________________________________________
;________________________________________________________________________________________________________
numpad1:: ;Test painting the background and clearing the graphics
	Window.PaintBackground( "0xFF3377ff" , 1 )
	sleep, 500
	Window.ClearWindow( 1 )
	sleep, 500
	Window.PaintBackground( "0xFFff0000" , 1 )
	return

Numpad2:: ;Test moving the window and drawing a bitmap to the graphics
	Window.ShowWindow( 1 , { x: 200 , y: 200 , w: Window.W , h: Window.H } )
	sleep, 500
	Window.PaintBackground( "0xFF222529" , 1 )
	Window.Draw( HB_BITMAP_MAKER() , { X: 75 , Y: 2 , W: 150 , H: 40 } , , 1 )
	return

Numpad3:: ;Test deleting the layered window	, shuting down gdip , and creating a new window
	Window.DeleteWindow( 1 )
	Window := ""
	sleep, 1000
	Window := New LayeredWindow( x := "" , y := "", w := 300 , h := 100 , window := 1 , title := "New Test Window" , smoothing := 4 , options := "+AlwaysOnTop" , autoShow := 1 , GdipStart := 1 , { x: 0 , y: 0 , w: 300 , h: 42 } , BackgroundColor := "0xFF222529" )
	Window.ShowWindow( 1 , { x: 700 , y: 200 , w: Window.W , h: Window.H } )
	sleep, 500
	Window.Draw( HB_BITMAP_MAKER() , { X: 75 , Y: 2 , W: 150 , H: 40 } , , 1 )
	return
;***********************************************************************************************************************************************************************
;***********************************************************************************************************************************************************************
HB_BITMAP_MAKER(){ ;Create Test Bitmap
	;Bitmap Created Using: HB Bitmap Maker
	pBitmap:=Gdip_CreateBitmap( 150 , 40 ) 
	 G := Gdip_GraphicsFromImage( pBitmap )
	Gdip_SetSmoothingMode( G , 2 )
	Brush := Gdip_BrushCreateSolid( "0xFF272928" )
	Gdip_FillRectangle( G , Brush , 0 , 0 , 150 , 40 )
	Gdip_DeleteBrush( Brush )
	Brush := Gdip_CreateLineBrushFromRect( 7 , 21 , 2 , 23 , "0xFF52C2DA" , "0xFF111111" , 1 , 1 )
	Gdip_FillRoundedRectangle( G , Brush , 1 , 1 , 148 , 37 , 18 )
	Gdip_DeleteBrush( Brush )
	Brush := Gdip_BrushCreateSolid( "0xFF343933" )
	Gdip_FillRoundedRectangle( G , Brush , 6 , 4 , 138 , 31 , 14 )
	Gdip_DeleteBrush( Brush )
	Brush := Gdip_BrushCreateSolid( "0xFF272928" )
	Gdip_FillRectangle( G , Brush , 30 , 1 , 90 , 10 )
	Gdip_DeleteBrush( Brush )
	Brush := Gdip_BrushCreateSolid( "0xFF272928" )
	Gdip_FillRectangle( G , Brush , 30 , 28 , 90 , 10 )
	Gdip_DeleteBrush( Brush )
	Brush := Gdip_BrushCreateSolid( "0xFF232B32" )
	Gdip_FillRoundedRectangle( G , Brush , 9 , 6 , 132 , 27 , 10 )
	Gdip_DeleteBrush( Brush )
	Brush := Gdip_CreateLineBrush( 71 , 20 , 70 , 20 , "0xFF24404C" , "0xFF3E8191" , 2 )
	Gdip_FillRoundedRectangle( G , Brush , 11 , 8 , 128 , 23 , 10 )
	Gdip_DeleteBrush( Brush )
	Brush := Gdip_BrushCreateSolid( "0xFF0FBBE1" )
	Gdip_FillRectangle( G , Brush , 20 , 9 , 110 , 1 )
	Gdip_DeleteBrush( Brush )
	Brush := Gdip_BrushCreateSolid( "0xFF0FBBE1" )
	Gdip_FillRectangle( G , Brush , 30 , 5 , 90 , 1 )
	Gdip_DeleteBrush( Brush )
	Brush := Gdip_BrushCreateSolid( "0xFF0FBBE1" )
	Gdip_FillRectangle( G , Brush , 30 , 34 , 90 , 1 )
	Gdip_DeleteBrush( Brush )
	Brush := Gdip_BrushCreateSolid( "0xFF0FBBE1" )
	Gdip_FillRectangle( G , Brush , 20 , 30 , 110 , 1 )
	Gdip_DeleteBrush( Brush )
	Brush := Gdip_BrushCreateSolid( "0xFF111111" )
	Gdip_TextToGraphics( G , "Hellbent" , "s16 Center vCenter c" Brush " x0 y1" , "Arial Black" , 150 , 40 )
	Gdip_DeleteBrush( Brush )
	Brush := Gdip_BrushCreateSolid( "0xFF111111" )
	Gdip_TextToGraphics( G , "Hellbent" , "s16 Center vCenter c" Brush " x0 y1" , "Arial Black" , 150 , 40 )
	Gdip_DeleteBrush( Brush )
	Brush := Gdip_BrushCreateSolid( "0xFF111111" )
	Gdip_TextToGraphics( G , "Hellbent" , "s16 Center vCenter c" Brush " x1 y2" , "Arial Black" , 150 , 40 )
	Gdip_DeleteBrush( Brush )
	Brush := Gdip_BrushCreateSolid( "0xFFE7FEFF" )
	Gdip_TextToGraphics( G , "Hellbent" , "s16 Center vCenter c" Brush " x0 y1" , "Arial Black" , 150 , 40 )
	Gdip_DeleteBrush( Brush )
	Gdip_DeleteGraphics( G )
	return pBitmap
}
Another simple example. (drawing a line from the center of the screen to the current cursor position)
Spoiler
.


Here is a more elaborate example.

***This example requires Windows 8+ ( 8 or 10 )***
Spoiler
.
Last edited by Hellbent on 17 Apr 2020, 22:22, edited 1 time in total.
User avatar
elModo7
Posts: 188
Joined: 01 Sep 2017, 02:38
GitHub: elModo7
Location: Spain
Contact:

Re: [class] LayeredWindow ( WS_EX_LAYERED )

18 Feb 2020, 11:08

I am using the line draw one for some old image tracking scripts, makes them more appealing to the eye!
Image
:beer:
User avatar
Hellbent
Posts: 1061
Joined: 23 Sep 2017, 13:34

Re: [class] LayeredWindow ( WS_EX_LAYERED )

18 Feb 2020, 20:17

elModo7 wrote:
18 Feb 2020, 11:08
I am using the line draw one for some old image tracking scripts, makes them more appealing to the eye!
What do you use the image tracking for?
User avatar
elModo7
Posts: 188
Joined: 01 Sep 2017, 02:38
GitHub: elModo7
Location: Spain
Contact:

Re: [class] LayeredWindow ( WS_EX_LAYERED )

19 Feb 2020, 02:28

Mostly to automatically avoid enemy collision and finish levels on super mario bros.
And to add more juice to some old scripts like the shiny hunting one:
Spoiler
:beer:
User avatar
Hellbent
Posts: 1061
Joined: 23 Sep 2017, 13:34

Re: [class] LayeredWindow ( WS_EX_LAYERED )

19 Feb 2020, 04:45

@elModo7

4 games at once, that is something else lol.
User avatar
Hellbent
Posts: 1061
Joined: 23 Sep 2017, 13:34

Re: [class] LayeredWindow ( WS_EX_LAYERED )

17 Apr 2020, 22:50

***OP Update***

Added a few new methods (functions) to the class for a program I'm working on.

Here is a simple example of using the new methods.

Code: Select all

;***************************************************************************************************
;***************************************************************************************************
;***************************************************************************************************
#Include <My Altered Gdip Lib>  ;<------       Replace with your copy of GDIP
#Include <LayeredWindow Class>  ;<------       Replace with your copy
;***************************************************************************************************
;***************************************************************************************************
;***************************************************************************************************
#SingleInstance, Force
SetBatchLines, -1
OnExit, GuiClose

	;Creating a new layered window
MainWindow := New LayeredWindow( x := "" , y := "" , w := 800 , h := 300 , window := 1 , title := " " , smoothing := 2 , options := "-DPIScale +AlwaysOnTop" , autoShow := 1 , GdipStart := 1 , WinMover := {X:0, Y:0, W:w-30, H:h} , BackgroundColor := "0x99222529" )

	;Adding lines, click triggers, text.
MainWindow.Draw_Rectangle(THIC:=4,{X:44,Y:44,W:w-120,H:h-120},Color1:="ffffff",Color2:="336699",PosObj2:="")
MainWindow.Draw_Rectangle(THIC:=1,{X:190,Y:190,W:w-320,H:h-220},Color1:="336699",Color2:="",PosObj2:="")
MainWindow.Draw_Rectangle(THIC:=3,{X:1,Y:1,W:w-3,H:h-3},Color1:="000000",Color2:="",PosObj2:="")
MainWindow.Add_Trigger({X:w-30,Y:0,W:30,H:30},"GuiClose")
MainWindow.Add_Trigger({X:w-30,Y:h-30,W:30,H:30},"Bob")
MainWindow.Draw_Text({X:0,Y:0,W:w,H:h},"Hellbent Waz Here",Font:="Arial",FS:=36,Color1:="888800",Color2:="000000",{X:0,Y:0,W:w,H:20})
MainWindow.Draw_Text({X:3,Y:3,W:w,H:h},"Hellbent Waz Here",Font:="Arial",FS,Color1:="FFFFFF")

	;Redrawing the window
MainWindow.UpdateWindow()

return

GuiClose:
GuiContextMenu:
*ESC::
		;Deleting the window and turning off GDIP.
	MainWindow.DeleteWindow(1)
	ExitApp

Bob:
	ToolTip, here
	return
	


Or here is a actual use case (Sans Code).
Spoiler
burque505
Posts: 1396
Joined: 22 Jan 2017, 19:37

Re: [class] LayeredWindow ( WS_EX_LAYERED )

18 Apr 2020, 08:24

@Hellbent, are those coronavirus cells bouncing around there, or have I just been under a stay-at-home order too long? :crazy:
ah_sis90
Posts: 3
Joined: 19 Mar 2019, 04:37

Re: [class] LayeredWindow ( WS_EX_LAYERED )

14 May 2020, 02:55

elModo7 wrote:
18 Feb 2020, 11:08
I am using the line draw one for some old image tracking scripts, makes them more appealing to the eye!
Image Broken Link for safety

Hi,

is it possible to pass the source?

thanks in advance
User avatar
elModo7
Posts: 188
Joined: 01 Sep 2017, 02:38
GitHub: elModo7
Location: Spain
Contact:

Re: [class] LayeredWindow ( WS_EX_LAYERED )

15 May 2020, 09:13

@ah_sis90 It was mostly a test so I ended up removing it, it was 2 imagesearch one for mario and another one for enemies, if distance was equal or lower than a certain point it would jump.
:beer:
User avatar
Hellbent
Posts: 1061
Joined: 23 Sep 2017, 13:34

Re: [class] LayeredWindow ( WS_EX_LAYERED )

16 May 2020, 14:19

burque505 wrote:
18 Apr 2020, 08:24
@Hellbent, are those coronavirus cells bouncing around there, or have I just been under a stay-at-home order too long? :crazy:
I don't know what you're talking about, those are clearly green frogs jumping around the screen :D
shootermcgavin333
Posts: 4
Joined: 02 Aug 2020, 21:12

Re: [class] LayeredWindow ( WS_EX_LAYERED )

03 Aug 2020, 03:55

elModo7 wrote:
18 Feb 2020, 11:08
I am using the line draw one for some old image tracking scripts, makes them more appealing to the eye!
This is awesome! Could you please please please share the script? I am trying to make a line generator
User avatar
elModo7
Posts: 188
Joined: 01 Sep 2017, 02:38
GitHub: elModo7
Location: Spain
Contact:

Re: [class] LayeredWindow ( WS_EX_LAYERED )

03 Aug 2020, 11:19

shootermcgavin333 wrote:
03 Aug 2020, 03:55
elModo7 wrote:
18 Feb 2020, 11:08
I am using the line draw one for some old image tracking scripts, makes them more appealing to the eye!
This is awesome! Could you please please please share the script? I am trying to make a line generator
I said in a previous post I deleted that one, it was just for testing imagesearch + the line, nothing too fancy.
Sorry for the inconvenience :|
:beer:

Return to “Scripts and Functions”

Who is online

Users browsing this forum: LTdT, Saiapatsu and 20 guests