Using GDIP to create blank layered window GUhasI to draw onto with mouse

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
finnascript
Posts: 4
Joined: 08 Oct 2021, 01:46

Using GDIP to create blank layered window GUhasI to draw onto with mouse

14 Oct 2021, 21:21

I've tried digesting Tiq's GDIP tutorial examples and used a number of past forum posts but I'm still unable to solve this problem.

My apologies in advance for the long-winded example and the associated complexities explained but please note the following points...

I've created this code based on my larger project to showcase the specific scenario that's requiring a solution.

Therefore the sequence with Create() to auto-execute and generate a blank canvas layered window GUI for Draw()to make use of the Lbutton hotkey is required to fit within the larger project at a whole.

My Code:

Code: Select all

#SingleInstance Force

#Include Libraries\gdip.ahk

Create()

#IfWinActive, BlankGui AHK_Class AutoHotkeyGUI
	+LButton::Draw()
#IfWinActive

^!+r::reload 

Create(){

	pToken:=Gdip_Startup()
	pBitmap := Gdip_CreateBitmap(300, 700)

	Gui, BlankGui : +E0x80000
	Gui, BlankGui : Show, w300 h700, BlankGui

	WinGetPos, x,y,w,h,BlankGui

	hwnd := WinExist()

	hbm := CreateDIBSection(w,h)
	hdc := CreateCompatibleDC()
	obm := SelectObject(hdc, hbm)
		
	G := Gdip_GraphicsFromHDC(hdc)

	Gdip_SetSmoothingMode(G, 4)
	Gdip_SetInterpolationMode(G, 7)
	Gdip_DrawImage(G, pBitmap, 0,0, w,h)
	UpdateLayeredWindow(hwnd, hdc, x,y,w,h)
	Gdip_DisposeImage(pBitmap)
	Gdip_Shutdown("pToken")
}

; I'm not well versed with GDIP, or programming as a whole for that matter and I know that I've unsuccessfully attempted to create the blank layered window GUI in Create(); however, Draw() is the real problem I'm looking to solve.

Draw(){

	pToken:=Gdip_Startup()
	
	hwnd := WinExist()
	WinGetPos, x,y,w,h,BlankGui
	
	Pen := Gdip_CreatePen(0xFF000000, 1)

	CoordMode, Mouse, Relative

	While(GetKeyState(RegExReplace(A_ThisHotkey,"^(\w* & |\W*)"), "p"))	{

		MouseGetPos, Mx, My
		Send, !{PrintScreen}

		pBitmap:=Gdip_CreateBitmapFromClipboard()
		Gdip_GetDimensions(pBitmap, bw, bh)

		pBitmap2 := Gdip_CreateBitmap(bw, bh)
		G2 := Gdip_GraphicsFromImage(pBitmap2)

		Gdip_DrawImage(G2, pBitmap, 0, 0, w, h, 0, 0, bw, bh)
		Gdip_SetBitmapToClipboard(pBitmap2)
		Gdip_DeleteGraphics(G2)
		Gdip_DisposeImage(pBitmap)

		hbm := CreateDIBSection(w,h)
		hdc := CreateCompatibleDC()
		obm := SelectObject(hdc, hbm)
		
		G := Gdip_GraphicsFromHDC(hdc)
		Gdip_SetSmoothingMode(G, 4)
		Gdip_SetInterpolationMode(G, 7)
		Gdip_DrawImage(G, pBitmap, 0,0, w,h)

		Gdip_DisposeImage(pBitmap)
		
		Gdip_DrawRoundedRectangle(G, Pen, Mx, My, 4,4,4)
		
		UpdateLayeredWindow(hwnd, hdc, x,y,w,h)
		
		SelectObject(hdc, obm)
		DeleteObject(hbm),DeleteDC(hdc)
		Gdip_DeleteGraphics(G)
		
	}
	Gdip_Shutdown("pToken")
	Gdip_DeletePen(Pen)

	Return
}
User avatar
Spawnova
Posts: 554
Joined: 08 Jul 2015, 00:12
Contact:

Re: Using GDIP to create blank layered window GUhasI to draw onto with mouse

15 Oct 2021, 07:57

I have no idea what exactly you are trying to accomplish, but from reading your code it seems like you want a gui that you can draw on?
The code seemed to indicate you were copying the blank gui, then drawing it back on the gui with a roundedrectangle added then repeating the process.
If that is the case then you don't really want a LayeredWindow, those are mostly used for overlays, instead you can just draw directly on the Window

here's an example of how to do that, again this is assuming I have guessed correctly at what you were looking for.

Code: Select all

#SingleInstance Force

;if you plan to use gdip a lot, you should include it in your 
;'C:\Users\USERNAME\Documents\AutoHotKey\lib' folder, then you wont' have to #include it every time
#Include Libraries\gdip.ahk 


pToken:=Gdip_Startup()
CoordMode, Mouse, Client

w := 300
h := 700

gui, BlankGui : +hwndhwnd
Gui, BlankGui : Show, w%w% h%h%, MyBlankGui

; you only need to set this up once
hdc := GetDC(hwnd) ;get gui device context
G := Gdip_GraphicsFromHDC(hdc) ;create graphics from it
Gdip_SetSmoothingMode(G, 4)
Gdip_SetInterpolationMode(G, 7)

Pen := Gdip_CreatePen(0xFF000000, 1) ;resources like pens/brushes/bitmaps only need to be created once, unless they are dynamic
backCol := gdip_BrushCreateSolid(0xFF333333)

colorPens := []
loop 20
	colorPens.push(Gdip_CreatePen(random(0xFF555555,0xFFFFFFFF),1)) ;just some extra bits of fun, not required
	
gdip_fillrectangle(g,backCol,-1,-1,w+1,h+1) ;sets background color, essentially erasing everything as well
return


#IfWinactive, MyBlankGui
+LButton::
settimer,draw,10
return

+LButton up::
settimer,draw,off
return
#if


f9::reload
^!+r::reload 


draw:
MouseGetPos, Mx, My
;Gdip_DrawEllipse(G, Pen, mx-2, my-2, 4,4)  ;just used black pen

size := random(1,10)
sizeh := size/2

Gdip_DrawEllipse(G, colorPens[random(1,20)], mx-sizeh, my-sizeh, size,size)
Return
finnascript
Posts: 4
Joined: 08 Oct 2021, 01:46

Re: Using GDIP to create blank layered window GUhasI to draw onto with mouse

15 Oct 2021, 17:30

Spawnova wrote:
15 Oct 2021, 07:57
I have no idea what exactly you are trying to accomplish, but from reading your code it seems like you want a gui that you can draw on?
Thanks for answering my question... Feels awkward to ask if you understand English, because you mentioned something along the lines "the code seemed to indicate". I do see a bit of a dilemma with my post subject having something of a typo, except thats why I doubled down my instructions and needs at the beginning of the post before the code, just incase of an error like within the subject.
finnascript wrote:
14 Oct 2021, 21:21
I've created this code based on my larger project to showcase the specific scenario that's requiring a solution.

Therefore the sequence with Create() to auto-execute and generate a blank canvas layered window GUI for Draw()to make use of the Lbutton hotkey is required to fit within the larger project at a whole.

Not to be that guy, but you should read the instructions before you answer the question. Also Random() must be a function you created for your personal library because I don't have it in my distro.
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Using GDIP to create blank layered window GUhasI to draw onto with mouse

17 Oct 2021, 02:41

Something like this?

Code: Select all

;**************************************************************************************************	
#Include <My Altered GDIP lib> ;This is just the plain gdip lib (the name "My Altered GDIP lib" means nothing here, it is just "gdip.ahk" )
#Include <PopUpWindow Class> ;https://www.autohotkey.com/boards/viewtopic.php?f=6&t=94961
;**************************************************************************************************	
#SingleInstance, Force
SetBatchlines, -1
#NoEnv
CoordMode, Mouse, Client
Gdip_Startup()

MyPen := Gdip_CreatePen( "0xFF336699" , 10 )
Active := 0

Gui, 1:+AlwaysOnTop 
Gui, 1:Add, Button, gActivateDraw, Draw On Canvas
Gui, 1:Show, % "x" A_ScreenWIdth - 300 " y200 NA"

MyCanvas := New PopUpWindow( { WindowName: "2" , WindowOptions: " -DPIScale +AlwaysOnTop +Owner1 " , WindowSmoothing: 2 , X: 0 , Y: 0 , W: A_ScreenWidth , H: A_ScreenHeight } )
MyCanvas.ShowWindow()

return
GuiClose:
*ESC::ExitApp

Numpad3:: PopUpWindow.Helper()
	
ActivateDraw:
	Active := 1
	SoundBeep, 999
	return
	
DrawLine:
	MouseGetPos, End_X , End_Y
	MyCanvas.ClearWindow()
	Gdip_DrawLine( MyCanvas.G , MyPen, Start_X, Start_Y, End_X, End_Y)
	MyCanvas.UpdateWindow()
	return
	
;**************************************************************************************************	
#If ( Active )

LButton::
	WinActivate, % "AHK_ID " MyCanvas.Hwnd
	MouseGetPos, Start_X , Start_Y
	SetTimer, DrawLine, 30
	return

LButton Up::
	Active := 0
	SetTimer, DrawLine, Off 
	gosub, DrawLine
	loop, 3
		SoundBeep, 1555
	return
	
#If
;**************************************************************************************************	
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Using GDIP to create blank layered window GUhasI to draw onto with mouse

17 Oct 2021, 05:53

This is a POC I made a few weeks ago to prep for an active project. This is what the code above does when expanded out a bit.
Temp (1).gif
Temp (1).gif (181.92 KiB) Viewed 431 times
Press numpad1 to toggle drawing.
Press numpad2 to delete the last element.

*Requires windows 8+ (child layered windows)

Code: Select all

;***************************************************************************************************
#Include <My Altered Gdip Lib>   ;Replace with your path to the Gdip.ahk lib
#Include <PopUpWindow Class>
;***************************************************************************************************
#SingleInstance force
SetBatchLines, -1
GDIP_StartUp()
CoordMode, Mouse, Client

Elements := []
Index := 0

Gui1 := New PopUpWindow( { WindowName: "1" , WindowOptions: " -DPIScale +AlwaysOnTop " , WindowSmoothing: 2 , X: "Center" , Y: "Center" , W: 100 , H: 100 } )
Gui1.ShowWindow( MyWindowTitle := "" )
;***********************************************************************************
global Gui2 := New PopUpWindow( { WindowName: "2" , WindowOptions: " -DPIScale +AlwaysOnTop +Parent1" , WindowSmoothing: 2 , X: 0 , Y: 0 , W: 100 , H: 100 } )
Gui2.ShowWindow( MyWindowTitle := "" )
;***********************************************************************************
Gui3 := New PopUpWindow( { WindowName: "3" , WindowOptions: " -DPIScale +AlwaysOnTop +Parent2" , WindowSmoothing: 2 , X: 0 , Y: 0 , W: 100 , H: 100 } )
Gui3.ShowWindow( MyWindowTitle := "" )
;***********************************************************************************
Gui1.Margin := 10
Gui1.Background := {}
Gui1.Background.Bitmap := Gdip_CreateBitmapFromFile( "C:\Users\Hellbent\Desktop\AHK Tools\Color Picker Mini\Screen Shots\20210117095023.png" )
Gui1.Background.W := Gdip_GetImageWidth( Gui1.Background.Bitmap )
Gui1.Background.H := Gdip_GetImageHeight( Gui1.Background.Bitmap )
Gui1.SetWindowProperties( { X: "Center" , Y: "Center" , W: ( Gui1.Background.W + 2 * Gui1.Margin ) , H: ( Gui1.Background.H + 2 * Gui1.Margin ) } , 1 )
Gui2.SetWindowProperties( { W: ( Gui1.Background.W + 2 * Gui1.Margin ) , H: ( Gui1.Background.H + 2 * Gui1.Margin ) } , 1 )
Gui3.SetWindowProperties( { W: ( Gui1.Background.W + 2 * Gui1.Margin ) , H: ( Gui1.Background.H + 2 * Gui1.Margin ) } , 1 )
Gui1.DrawBitmap( Gui1.Background.Bitmap , { X: Gui1.Margin , Y: Gui1.Margin , W: Gui1.Background.W , H: Gui1.Background.H } , dispose := 0 )
Gui1.UpdateWindow()

return
GuiClose:
GuiContextMenu:
*ESC::ExitApp

Numpad3::PopUpWindow.Helper()

Numpad1::Active := !Active

#If ( Active )

LButton::
	Elements[ ++Index ] := {}
	MouseGetPos, x, y
	Elements[ Index ].Type := "FillRectangle"
	Elements[ Index ].SX := x
	Elements[ Index ].SY := y
	SetTimer, Draw, 30
	return
	
LButton Up::
	SetTimer, Draw, Off
	GDIPDraw[ Elements[ Index ].Type ]( Elements[ Index ] , Gui1 )
	Gui1.UpdateWindow()
	Gui2.ClearWindow()
	Gui2.UpdateWindow()
	return

#If

Numpad2::
	Gui1.ClearWindow()
	Gui1.DrawBitmap( Gui1.Background.Bitmap , { X: Gui1.Margin , Y: Gui1.Margin , W: Gui1.Background.W , H: Gui1.Background.H } , dispose := 0 )
	Elements.Pop()
	Loop, % Elements.Length() 	
		GDIPDraw[ Elements[ A_Index ].Type ]( Elements[ A_Index ] , Gui1 )
	Gui1.UpdateWindow()
	return


Draw:
	Gui2.ClearWindow()
	MouseGetPos, x, y
	( x < Elements[ Index ].SX ) ? ( Elements[ Index ].X := x , Elements[ Index ].W := Elements[ Index ].SX - x ) : ( Elements[ Index ].X := Elements[ Index ].SX , Elements[ Index ].W := x - Elements[ Index ].SX )
	( y < Elements[ Index ].SY ) ? ( Elements[ Index ].Y := y , Elements[ Index ].H := Elements[ Index ].SY - y ) : ( Elements[ Index ].Y := Elements[ Index ].SY , Elements[ Index ].H := y - Elements[ Index ].SY )
	GDIPDraw[ Elements[ Index ].Type ]( Elements[ Index ] , Gui2 )
	Gui2.UpdateWindow()
	return

 class GDIPDraw	{
	FillRectangle( obj , win ){
		Brush := Gdip_BrushCreateSolid( "0x66ff0000" ) , Gdip_FillRectangle( win.G , Brush , Obj.X , Obj.Y , Obj.W , Obj.H ) , Gdip_DeleteBrush( Brush )
	}
}
 
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Using GDIP to create blank layered window GUhasI to draw onto with mouse

17 Oct 2021, 06:03

Here is another POC from a few years ago that has a slightly different method of doing things.

This should run on older versions of windows ( pre windows 8 )

Temp (1).gif
Temp (1).gif (356.48 KiB) Viewed 428 times

Code: Select all

;***************************************************************************************************
;***************************************************************************************************
;***************************************************************************************************
#Include <My Altered Gdip Lib>  ;<------       Replace with your copy of GDIP
;***************************************************************************************************
;***************************************************************************************************
;***************************************************************************************************
#SingleInstance,Force
SetBatchLines,-1
CoordMode,Mouse,Client
Gdip_Startup()
global ColorList := ["000000","7F7F7F","880015","ED1C24","FF7F27","FFF200","22B14C","00A2E8","3F48CC","A349A4","FFFFFF","C3C3C3","B97A57","FFAEC9","FFC90E","EFE4B0","B5E61D","99D9EA","7092BE","C8BFE7"]
,ColorSelector := []
,Canvas:={}
,BrushType:=1,BrushColor:=ColorList[4],Zone:={X:10,Y:90,W:485,H:300},MyBrush,MyPen
Gui,1:+AlwaysOnTop -DPIScale hwndWinHwnd
Canvas.HWND:=WinHwnd
Canvas.hdc1:=GetDC(Canvas.HWND)
Canvas.hdc2:=CreateCompatibleDC()
Canvas.hbm:=CreateDIBSection(485,300)
Canvas.obm:=SelectObject(Canvas.hdc2,Canvas.hbm)
Canvas.G:=Gdip_GraphicsFromHDC(Canvas.hdc2)
Gdip_SetSmoothingMode(Canvas.G,2)
Bitmap1:=Gdip_CreateBitmap( 485,300 ) 
G1 := Gdip_GraphicsFromImage( Bitmap1 )
Gdip_SetSmoothingMode( G1 , 2 )
Bitmap2:=Gdip_CreateBitmap( 485,300 ) 
G2 := Gdip_GraphicsFromImage( Bitmap2 )
Gdip_SetSmoothingMode( G2 , 2 )
Brush := Gdip_BrushCreateSolid( "0xFF004444" )
Gdip_FillRectangle(G1,Brush,-1,-1,487,302)
Gdip_DrawImage(Canvas.G,Bitmap1)
Gui,1:Color,222429
x:=-20,w:=20
Loop,% ColorList.Length()	{
	ColorSelector[A_Index]:=New Colors(x+=w+5,y:=10,w,h:=w,Window:="1",color:=ColorList[A_Index])
}
global HB_Button:=[]
HB_Button.Push(New Clipboard_Master_v2_Buttons(x:=20, y:=40, w:=150, h:=30, window:="1", Label:="DrawDots", Text:="Draw Dots", Font:="Segoe UI", Font_Size:="10 Bold", Font_Color_Top:="33aaff", Font_Color_Bottom:="000000", Background_Color:="222429", y_Offset,Type:="Open Folder"))
HB_Button.Push(New Clipboard_Master_v2_Buttons(x+=w+10, y:=40, w:=150, h:=30, window:="1", Label:="DrawLines", Text:="Draw Lines", Font:="Segoe UI", Font_Size:="10 Bold", Font_Color_Top:="33aaff", Font_Color_Bottom:="000000", Background_Color, y_Offset,Type:="Open Folder"))
HB_Button.Push(New Clipboard_Master_v2_Buttons(x+=w+10, y:=40, w:=150, h:=30, window:="1", Label:="DrawRectangles", Text:="Draw Rectangles", Font:="Segoe UI", Font_Size:="10 Bold", Font_Color_Top:="33aaff", Font_Color_Bottom:="000000", Background_Color, y_Offset,Type:="Open Folder"))
SetBrush()
Gui,1:Show,% "x" A_ScreenWidth-520 " y50 w505 h400" ,HB Paint
BitBlt(Canvas.hdc1,10,90,485,300,Canvas.hdc2,0,0)
SetTimer,HB_Button_Hover,50
SetTimer,Paint_It,10
return
GuiClose:
*ESC::
	ExitApp

DrawDots:
	BrushType:=1
	return
DrawLines:
	BrushType:=2
	return
DrawRectangles:
	BrushType:=3
	return
	
Paint_It:
	if(WinActive("ahk_id " Canvas.HWND)){
		if(GetKeyState("Up"))
			MouseMove,0,-1,0,r
		else if(GetKeyState("Down"))
			MouseMove,0,1,0,r
		if(GetKeyState("Left"))
			MouseMove,-1,0,0,r
		else if(GetKeyState("Right"))
			MouseMove,1,0,0,r
		MouseGetPos,tx,ty
	}
	if(WinActive("ahk_id " Canvas.HWND)&&GetKeyState("LButton")){
		if(tx>=Zone.X&&tx<=Zone.X+Zone.W&&ty>=Zone.Y&&ty<=Zone.Y+Zone.H){
			if(BrushType=1){
				Gdip_FillEllipse( G1 , MyBrush , tx-15 , ty-95 , 10 , 10 )
			}else if(BrushType=2){
				MouseGetPos,sx,sy
				While(GetKeyState("LButton")){	
					MouseGetPos,cx,cy
					Gdip_DrawImage(G2,Bitmap1)
					Gdip_DrawLine(G2, MyPen, sx-10, sy-90, cx-10, cy-90)  
					Gdip_DrawImage(Canvas.G,Bitmap2)
					BitBlt(Canvas.hdc1,10,90,485,300,Canvas.hdc2,0,0)   
					sleep,10
				}
				Gdip_DrawLine(G1, MyPen, sx-10, sy-90, cx-10, cy-90)
			}else if(BrushType=3){
				CoordMode,Mouse,Client
				MouseGetPos,sx,sy
				Fx:=sx,Fy:=sy
				While(GetKeyState("LButton")){
					MouseGetPos,ex,ey
					(sx<ex)?(x1:=sx,x2:=ex-sx):(x1:=ex,x2:=sx-ex)
					(sy<ey)?(y1:=sy,y2:=ey-sy):(y1:=ey,y2:=sy-ey)
						(GetKeyState("Up"))?(Move(0,-1),(!GetKeyState("Shift"))?(sy-=1))
						(GetKeyState("Right"))?(Move(1,0),(!GetKeyState("Shift"))?(sx+=1))
						(GetKeyState("Down"))?(Move(0,1),(!GetKeyState("Shift"))?(sy+=1))
						(GetKeyState("Left"))?(Move(-1,0),(!GetKeyState("Shift"))?(sx-=1))
					if(GetKeyState("RButton")){
						Click U
						Gdip_DrawImage(Canvas.G,Bitmap1)
						BitBlt(Canvas.hdc1,10,90,485,300,Canvas.hdc2,0,0)
						;~ BitBlt(Canvas.hdc1,8,88,485,300,Canvas.hdc2,0,0)
						return
					}
					Gdip_DrawImage(G2,Bitmap1)
					Gdip_DrawRectangle( G2 , MyPen , x1-10 , y1-90 , x2 , y2 )
					;~ Gdip_DrawRectangle( G2 , MyPen , x1-8 , y1-90 , x2 , y2 )
					Gdip_DrawImage(Canvas.G,Bitmap2)
					BitBlt(Canvas.hdc1,10,90,485,300,Canvas.hdc2,0,0)
					;~ BitBlt(Canvas.hdc1,8,88,485,300,Canvas.hdc2,0,0)
					sleep,10
				}
				Gdip_DrawRectangle( G1 , MyPen , x1-10 , y1-90 , x2 , y2 )
				;~ Gdip_DrawRectangle( G1 , MyPen , x1-8 , y1-88 , x2 , y2 )
			}
			Gdip_DrawImage(Canvas.G,Bitmap1)
			BitBlt(Canvas.hdc1,10,90,485,300,Canvas.hdc2,0,0)
			;~ BitBlt(Canvas.hdc1,8,88,485,300,Canvas.hdc2,0,0)
		}
	}
	return
Move(x,y){
	MouseMove,x,y,0,R
}
SetBrush(){
	Gdip_DeleteBrush( MyBrush )
	MyBrush := Gdip_BrushCreateSolid( "0xFF" BrushColor)
	Gdip_DeletePen(MyPen)
	MyPen:=Gdip_CreatePen("0xFF" BrushColor,5)
}
SetColor(hwnd){
	Loop,% ColorSelector.Length()	{
		if(Format("{1:#x}", hwnd)=ColorSelector[A_Index].hwnd){
			BrushColor:=ColorList[A_Index]
			SetBrush()
			SoundBeep,400
			return
		}
	}
}
Class Colors	{
	__New(x:="",y:="",w:="",h:="",Window:="1",color:="FF0000",Color2:="222222"){
		This.X:=x,This.Y:=y,This.W:=w,This.H:=h
		This.Window:=Window,This.Color:= "0xFF" Color
		This._Trigger()
		sleep,20
		This._Draw()
	}_Trigger(){
		local hwnd
		Gui,% This.Window ": Add",Picture,% "x" This.X " y" This.Y " w" This.W " h" This.H " hwndhwnd 0xE gSetColor"
		This.Hwnd:=hwnd
	}_Draw(){
		pBitmap:=Gdip_CreateBitmap( This.W , This.H ) 
		G := Gdip_GraphicsFromImage( pBitmap )
		Gdip_SetSmoothingMode( G , 3 )
		Brush := Gdip_BrushCreateSolid( "0xFF222222" ),Gdip_FillRectangle( G , Brush , -1 , -1 , This.W+2 , This.H+2 ),Gdip_DeleteBrush( Brush )
		Brush := Gdip_BrushCreateSolid( This.Color ),Gdip_FillRectangle( G , Brush , 1 , 1 , This.W-2 , This.H-2 ),Gdip_DeleteBrush( Brush )
		Gdip_DeleteGraphics( G )
		This.BitMap := Gdip_CreateHBITMAPFromBitmap(pBitmap)
		Gdip_DisposeImage(pBitmap)
		SetImage( This.Hwnd , This.BitMap )
	}
}
HB_Button_Hover(){
	static Hover_On,Index
	MouseGetPos,,,,ctrl,2
	if(!Hover_On&&ctrl){
		Loop,% HB_Button.Length()	{
			if(ctrl=HB_Button[A_Index].Hwnd)
				HB_Button[A_Index].Draw_Hover(),Index:=A_Index,Hover_On:=1,break
		}
	}else if(Hover_On){
		if(HB_Button[Index].Hwnd!=ctrl)
			HB_Button[Index].Draw_Default(),Hover_On:=0
	}
}
class Clipboard_Master_v2_Buttons	{
	__New(x:=10, y:=10, w:=100, h:=30, window:="1", Label:="", Text:="Button", Font:="Arial", Font_Size:="10 Bold", Font_Color_Top:="FFFFFF", Font_Color_Bottom:="000000", Background_Color:="333437", y_Offset:=0, Typek:="",Name:="" ){
		This.X:=x,This.Y:=y,This.W:=w,This.H:=h,This.Window:=window,This.Label:=Label,This.Background_Color:="0xFF" Background_Color
		This.Text:=Text,This.Font:=Font,This.Font_Color_Top:="0xFF" Font_Color_Top,This.Font_Color_Bottom:="0xFF" Font_Color_Bottom
		This.Font_Size:=Font_Size,This.Y_Offset:=y_Offset,This.Type:=Typek,This.Name:=Name
		This._Create_Trigger(),This._Create_Default_Bitmap(),This._Create_Hover_Bitmap(),This._Create_Pressed_Bitmap()
		Sleep, 20
		This.Draw_Default()
		GuiControl,% This.Window ":Focus",% This.Hwnd
	}_Create_Trigger(){
		local hwnd,bbp
		Gui,% This.Window ":Add",Picture,% "x" This.X " y" This.Y " w" This.W " h" This.H " hwndhwnd 0xE"
		This.Hwnd:=hwnd
		bbp:=This._Draw_Pressed.Bind(This)
		GuiControl,% This.Window ":+G",% This.Hwnd,% bbp
		if(This.Label)
			(isFunc(This.Label))?(This.Function:=Func(This.Label))
	}_Create_Default_Bitmap(){
		;Bitmap Created Using: HB Bitmap Maker
		pBitmap:=Gdip_CreateBitmap( This.W , This.H ),G := Gdip_GraphicsFromImage( pBitmap ),Gdip_SetSmoothingMode( G , 1 )
		Brush := Gdip_BrushCreateSolid( This.Background_Color ),Gdip_FillRectangle( G , Brush , -2 , 0 , This.W+3 , This.H+3 ),Gdip_DeleteBrush( Brush )
		Brush := Gdip_BrushCreateSolid( "0xFF252627" ),Gdip_FillRectangle( G , Brush , 3 , 3 , This.W-6 , This.H-6 ),Gdip_DeleteBrush( Brush )
		Brush := Gdip_CreateLineBrushFromRect( 4 , 3 , This.W-9 , This.H-7 , "0xFF363C45" , "0xFF2B2C2E" , 1 , 1 ),Gdip_FillRectangle( G , Brush , 4 , 4 , This.W-8 , This.H-8 ),Gdip_DeleteBrush( Brush )
		Pen := Gdip_CreatePen( "0xFF4D535B" , 1 ),Gdip_DrawLine( G , Pen , 4 , 4 , This.W-5 , 4 ),Gdip_DeletePen( Pen )
		Brush := Gdip_BrushCreateSolid( This.Font_Color_Bottom ),Gdip_TextToGraphics( G , This.Text , "s" This.Font_Size " Center vCenter c" Brush " x0 y" 1+This.Y_Offset , This.Font , This.W , This.H ),Gdip_DeleteBrush( Brush )
		Brush := Gdip_BrushCreateSolid( This.Font_Color_Top ),Gdip_TextToGraphics( G , This.Text , "s" This.Font_Size " Center vCenter c" Brush " x1 y" 2+This.Y_Offset , This.Font , This.W , This.H ),Gdip_DeleteBrush( Brush )
		Gdip_DeleteGraphics( G ),This.Default_Bitmap:=Gdip_CreateHBITMAPFromBitmap(pBitmap),Gdip_DisposeImage(pBitmap)
	}_Create_Hover_Bitmap(){
		;Bitmap Created Using: HB Bitmap Maker
		pBitmap := Gdip_CreateBitmap( This.W , This.H ),G := Gdip_GraphicsFromImage( pBitmap ),Gdip_SetSmoothingMode( G , 1 )
		Brush := Gdip_BrushCreateSolid( This.Background_Color ),Gdip_FillRectangle( G , Brush , -2 , 0 , This.W+3 , This.H+3 ),Gdip_DeleteBrush( Brush )
		Brush := Gdip_BrushCreateSolid( This.Font_Color_Top ),Gdip_FillRectangle( G , Brush , 3 , 3 , This.W-6 , This.H-6 ),Gdip_DeleteBrush( Brush )
		Brush := Gdip_CreateLineBrushFromRect( 4 , 3 , This.W-9 , This.H-7 , "0xFF464C55" , "0xFF3B3C3E" , 1 , 1 ),Gdip_FillRectangle( G , Brush , 4 , 4 , This.W-8 , This.H-8 ),Gdip_DeleteBrush( Brush )
		Pen := Gdip_CreatePen( "0xFF4D535B" , 1 ),Gdip_DrawLine( G , Pen , 4 , 4 , This.W-5 , 4 ),Gdip_DeletePen( Pen )
		Brush := Gdip_BrushCreateSolid( This.Font_Color_Bottom ),Gdip_TextToGraphics( G , This.Text , "s" This.Font_Size " Center vCenter c" Brush " x0 y" 1+This.Y_Offset , This.Font , This.W , This.H ),Gdip_DeleteBrush( Brush )
		Brush := Gdip_BrushCreateSolid( This.Font_Color_Top ),Gdip_TextToGraphics( G , This.Text , "s" This.Font_Size " Center vCenter c" Brush " x1 y" 2+This.Y_Offset , This.Font , This.W , This.H ),Gdip_DeleteBrush( Brush )
		Gdip_DeleteGraphics( G ),This.Hover_Bitmap:=Gdip_CreateHBITMAPFromBitmap(pBitmap),Gdip_DisposeImage(pBitmap)
	}_Create_Pressed_Bitmap(){
		;Bitmap Created Using: HB Bitmap Maker
		pBitmap:=Gdip_CreateBitmap( This.W , This.H ),G := Gdip_GraphicsFromImage( pBitmap ),Gdip_SetSmoothingMode( G , 1 )
		Brush := Gdip_BrushCreateSolid( This.Background_Color ),Gdip_FillRectangle( G , Brush , -2 , 0 , This.W+3 , This.H+3 ),Gdip_DeleteBrush( Brush )
		;----------------------------------------------------------------
		;credit jeeswg   -  https://www.autohotkey.com/boards/viewtopic.php?f=76&t=65764
		cc:= This.Font_Color_Top
		Match := StrSplit(RegExReplace(cc, "^0x(..)(..)(..)(..)$", "0x$1 0x$2 0x$3 0x$4"), " ")
		cc := Format("0x{:02X}{:02X}{:02X}{:02X}", Match.1, Max(Match.2-0x33,0), Max(Match.3-0x33,0), Max(Match.4-0x33,0))
		Brush := Gdip_BrushCreateSolid( cc ),Gdip_FillRectangle( G , Brush , 3 , 3 , This.W-6 , This.H-6 ),Gdip_DeleteBrush( Brush )
		;----------------------------------------------------------------
		Brush := Gdip_CreateLineBrushFromRect( 4 , 3 , This.W-9 , This.H-7 , "0xFF2B2C2E" , "0xFF363C45" , 1 , 1 ),Gdip_FillRectangle( G , Brush , 4 , 4 , This.W-8 , This.H-8 ),Gdip_DeleteBrush( Brush )
		Pen := Gdip_CreatePen( "0x881D232B" , 1 ),Gdip_DrawLine( G , Pen , 4 , This.H-5 , This.W-5 , This.H-5 ),Gdip_DeletePen( Pen )
		Pen := Gdip_CreatePen( "0x881D232B" , 1 ),Gdip_DrawLine( G , Pen , 4 , 4 , This.W-5 , 4 ),Gdip_DeletePen( Pen )
		Pen := Gdip_CreatePen( "0x881D232B" , 1 ),Gdip_DrawLine( G , Pen , 4 , 4 , 4 , This.H-6 ),Gdip_DeletePen( Pen )
		Pen := Gdip_CreatePen( "0x881D232B" , 1 ),Gdip_DrawLine( G , Pen , This.W-5 , 4 , This.W-5 , This.H-6 ),Gdip_DeletePen( Pen )
		Brush := Gdip_BrushCreateSolid( This.Font_Color_Bottom ),Gdip_TextToGraphics( G , This.Text , "s" This.Font_Size " Center vCenter c" Brush " x0 y" 2+This.Y_Offset , This.Font , This.W , This.H ),Gdip_DeleteBrush( Brush )
		Brush := Gdip_BrushCreateSolid( This.Font_Color_Top ),Gdip_TextToGraphics( G , This.Text , "s" This.Font_Size " Center vCenter c" Brush " x1 y" 3+This.Y_Offset , This.Font , This.W , This.H ),Gdip_DeleteBrush( Brush )
		Gdip_DeleteGraphics( G ),This.Pressed_Bitmap:=Gdip_CreateHBITMAPFromBitmap(pBitmap),Gdip_DisposeImage(pBitmap)
	}_Draw_Pressed(){
		local ctrl
		GuiControl,% This.Window ":Focus",% This.Hwnd
		SetTimer,HB_Button_Hover,Off
		SetImage(This.Hwnd,This.Pressed_Bitmap)
		While(GetKeyState("LButton"))
			Sleep,10
		SetTimer,HB_Button_Hover,On
		MouseGetPos,,,,ctrl,2
		if(ctrl=This.Hwnd){
			This.Draw_Hover()
			if(This.Function)
				This.Function.Call()
			else if(This.Label)
				gosub,% This.Label
		}else	{
			This.Draw_Default()
		}
	}Draw_Hover(){
		SetImage(This.Hwnd,This.Hover_Bitmap)
	}Draw_Default(){
		SetImage(This.Hwnd,This.Default_Bitmap)
	}
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: RandomBoy, scriptor2016 and 350 guests