I'm one of those people who rarely ask for help as I like to figure things out myself, but I've got totally stumped on what I assume is a really simple problem. All I want to do is to load images into a Gui created in GDIP and all works well until I try to load a new image. Where I've messed up with my controls and variables. here' the script I have:
Code: Select all
#SingleInstance,Force
#NoEnv
ListLines,Off
SetBatchLines,-1
global HB_Button:=[]
pToken:=Gdip_Startup()
hwnd1 := WinExist()
Main := New Custom_Window( x:= "" , y:= "" , w:= 600 , h:= 400 , Name:= "1" , Options:= "+AlwaysOnTop -Caption -DPIScale" , Title:= "Custom Gui Window" , Background_Bitmap:= HB_BITMAP_MAKER() )
Main.Show_Window()
;/*
Gui,1:Add,Text,x6 y3 w190 h24 BackgroundTrans gMove_Window
Gui,1:Add,Text,x360 y5 w15 h15 BackgroundTrans gMin_Window
Gui,1:Add,Text,x380 y5 w15 h15 BackgroundTrans gGuiClose
Gui,1:Color,333333,333333
Gui,1:Font,cWhite s8
;*/
; Add New Buttons
HB_Button.Push( New HB_Flat_Rounded_Button_Type_1( x:=10 , y := 12 , w := 100 , h := 35 , Button_Color := "865ABB" , Button_Background_Color := "333333" , Text := "Load" , Font := "Arial" , Font_Size := 10 " Bold" , Font_Color_Top := "888888" , Font_Color_Bottom := "111111" , Window := "1" , Label := "Load_image" , Default_Button := 1 , Roundness:=2 ) )
;Start the Button Hover Watch Timer
SetTimer, HB_Button_Hover , 50
return
GuiClose:
GuiContextMenu:
*Esc::
ExitApp
Load_image:
Gui Submit, nohide
FileSelectFile, picture, 3, , Open a file, Image Files (JPG;*.jpg;*.PNG;*.png)
pBitmap := Gdip_CreateBitmapFromFile(picture)
Width := Gdip_GetImageWidth(pBitmap)
Height := Gdip_GetImageHeight(pBitmap)
hBitmap := Gdip_CreateHBITMAPFromBitmap(pBitmap)
Gui, 1: Add, Picture, x10 y+30 w%Width% h%Height% 0xE vMyPicture
GuiControlGet, hwnd, hwnd, MyPicture
SetImage(hwnd, hBitmap)
Gui, 1: Show, AutoSize
return
HB_BITMAP_MAKER(){
;Bitmap Created Using: HB Bitmap Maker
pBitmap := Gdip_CreateBitmap( 600 , 400 ) , G := Gdip_GraphicsFromImage( pBitmap ) , Gdip_SetSmoothingMode( G , 4 )
Brush := Gdip_BrushCreateSolid( "0xFF333333" ) , Gdip_FillRectangle( G , Brush , 0 , 0 , 600 , 400 ) , Gdip_DeleteBrush( Brush )
Gdip_DeleteGraphics( G )
return pBitmap
}
Move_Window(){
PostMessage,0xA1,2
}
Min_Window(){
Gui,1:Minimize
}
Class Custom_Window {
__New(x:="",y:="",w:=300,h:=200,Name:=1,Options:="+AlwaysOnTop -Caption -DPIScale",Title:="",Background_Bitmap:=""){
This.X:=x
This.Y:=y
This.W:=w
This.H:=h
This.Name:=Name
This.Title:=Title
This.Options:=Options
This.Background_Bitmap:=Background_Bitmap
This.Create_Window()
}
Create_Window(){
Gui,% This.Name ":New",% This.Options " +LastFound"
This.Hwnd:=WinExist()
if(This.Background_Bitmap)
This.Draw_Background_Bitmap()
}
Draw_Background_Bitmap(){
This.Bitmap:=Gdip_CreateHBITMAPFromBitmap(This.Background_Bitmap)
Gdip_DisposeImage(This.Background_Bitmap)
Gui,% This.Name ":Add",Picture,% "x0 y0 w" This.W " h" This.H " 0xE"
GuiControlGet,hwnd,% This.Name ":hwnd",Static1
This.Background_Hwnd:=hwnd
SetImage(This.Background_Hwnd,This.Bitmap)
}
Show_Window(){
if(This.X&&This.Y)
Gui,% This.Name ":Show",% "x" This.X " y" This.Y " w" This.W " h" This.H,% This.Title
else if(This.X&&!This.Y)
Gui,% This.Name ":Show",% "x" This.X " w" This.W " h" This.H,% This.Title
else if(!This.X&&This.Y)
Gui,% This.Name ":Show",% "y" This.Y " w" This.W " h" This.H,% This.Title
else
Gui,% This.Name ":Show",% " w" This.W " h" This.H,% This.Title
}
}
Test_Button_Press:
;-----------------------------------------------------
; In the button label, add the focus grab line
GuiControl , % HB_Button[ A_GuiControl ].Window ": Focus" , % HB_Button[ A_GuiControl ].Hwnd
; Add the button press function call and test
if( ! HB_Button[ A_GuiControl ].Draw_Pressed() )
return
;----------------------------------------------
Run,calc.exe
return
;-----------------------------------------------------------------
;Add the hover fuction to the bottom of your script (or go lib route if you know how to)
HB_Button_Hover(){
Static Index , Hover_On
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 = 1 )
if( ctrl != HB_Button[ Index ].Hwnd )
HB_Button[ Index ].Draw_Default() , Hover_On := 0
}
;-----------------------------------------------------------------
;-----------------------------------------------------------------
;Add the button class to the bottom of your script (or go lib route if you know how to)
class HB_Flat_Rounded_Button_Type_1 {
__New( x := 10 , y := 10 , w := 150 , h := 40 , Button_Color := "FF0000" , Button_Background_Color := "222222" , Text := "Button" , Font := "Arial" , Font_Size := 16 , Font_Color_Top := "000000" , Font_Color_Bottom := "FFFFFF" , Window := "1" , Label := "" , Default_Button := 1, Roundness:=5 ){
This.Roundness:=Roundness
This.Text_Color_Top := "0xFF" Font_Color_Top
This.Text_Color_Bottom := "0xFF" Font_Color_Bottom
This.Font := Font
This.Font_Size := Font_Size
This.Text := Text
This.X := x
This.Y := y
This.W := w
This.H := h
This.Button_Background_Color := "0xFF" Button_Background_Color
This.Button_Color := "0xFF" Button_Color
This.Window := Window
This.Label := Label
This.Default_Button := Default_Button
This.Create_Default_Bitmap()
This.Create_Hover_Bitmap()
This.Create_Pressed_Bitmap()
This.Create_Trigger()
sleep, 20
This.Draw_Default()
}
Create_Trigger(){
global
num := HB_Button.Length()+1
Gui , % This.Window ": Add" , Picture , % "x" This.X " y" This.Y " w" This.W " h" This.H " hwndHwnd v" Num " g" This.Label " 0xE"
This.Number := Num , This.Hwnd := Hwnd
}
Create_Default_Bitmap(){
;Bitmap Created Using: HB Bitmap Maker
pBitmap:=Gdip_CreateBitmap( This.W , This.H ) ;96x29
G := Gdip_GraphicsFromImage( pBitmap )
Gdip_SetSmoothingMode( G , 2 )
Brush := Gdip_BrushCreateSolid( This.Button_Background_Color )
Gdip_FillRectangle( G , Brush , -1 , -1 , This.W+2 , This.H+2 )
Gdip_DeleteBrush( Brush )
Brush := Gdip_CreateLineBrushFromRect( 0 , 0 , This.W , This.H , "0xFF61646A" , "0xFF2E2124" , 1 , 1 )
Gdip_FillRoundedRectangle( G , Brush , 0 , 1 , This.W , This.H-3 , This.Roundness )
Gdip_DeleteBrush( Brush )
;---------------------------------------------------
if(This.Default_Button)
Brush := Gdip_CreateLineBrushFromRect( 0 , 0 , This.W , This.H , "0xFF4C4F54" , "0xFF35373B" , 1 , 1 )
else
Brush := Gdip_CreateLineBrushFromRect( 0 , 0 , This.W , This.H , This.Button_Color , "0xFF35373B" , 1 , 1 )
;-------------------------------------------
Gdip_FillRoundedRectangle( G , Brush , 1 , 2 , This.W-2 , This.H-5 , This.Roundness )
Gdip_DeleteBrush( Brush )
Pen := Gdip_CreatePen( "0xFF1A1C1F" , 1 )
Gdip_DrawRoundedRectangle( G , Pen , 0 , 0 , This.W-1 , This.H-3 , This.Roundness )
Gdip_DeletePen( Pen )
Brush := Gdip_BrushCreateSolid( This.Text_Color_Bottom )
Gdip_TextToGraphics( G , This.Text , "s" This.Font_Size " Center vCenter c" Brush " x1 y2 " , This.Font , This.W , This.H-1 )
Gdip_DeleteBrush( Brush )
Brush := Gdip_BrushCreateSolid( This.Text_Color_Top )
Gdip_TextToGraphics( G , This.Text , "s" This.Font_Size " Center vCenter c" Brush " x0 y1 " , This.Font , This.W , This.H-1 )
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 ) ;96x29
G := Gdip_GraphicsFromImage( pBitmap )
Gdip_SetSmoothingMode( G , 2 )
Brush := Gdip_BrushCreateSolid( This.Button_Background_Color )
Gdip_FillRectangle( G , Brush , -1 , -1 , This.W+2 , This.H+2 )
Gdip_DeleteBrush( Brush )
Brush := Gdip_CreateLineBrushFromRect( 0 , 0 , This.W , This.H , "0xFF61646A" , "0xFF2E2124" , 1 , 1 )
Gdip_FillRoundedRectangle( G , Brush , 0 , 1 , This.W , This.H-3 , This.Roundness )
Gdip_DeleteBrush( Brush )
;---------------------------------------------------------------------------
if(This.Default_Button)
Brush := Gdip_CreateLineBrushFromRect( 0 , 0 , This.W , This.H , "0xFF55585D" , "0xFF3B3E41" , 1 , 1 )
else
Brush := Gdip_CreateLineBrushFromRect( 0 , 0 , This.W , This.H , "0x44A826A2" , "0xFF3B3E41" , 1 , 1 )
;----------------------------------------------------------------
Gdip_FillRoundedRectangle( G , Brush , 1 , 2 , This.W-2 , This.H-5 , This.Roundness )
Gdip_DeleteBrush( Brush )
Pen := Gdip_CreatePen( "0xFF1A1C1F" , 1 )
Gdip_DrawRoundedRectangle( G , Pen , 0 , 0 , This.W-1 , This.H-3 , This.Roundness )
Gdip_DeletePen( Pen )
Brush := Gdip_BrushCreateSolid( This.Text_Color_Bottom )
Gdip_TextToGraphics( G , This.Text , "s" This.Font_Size " Center vCenter c" Brush " x1 y2" , This.Font , This.W , This.H-1 )
Gdip_DeleteBrush( Brush )
Brush := Gdip_BrushCreateSolid( This.Text_Color_Top )
Gdip_TextToGraphics( G , This.Text , "s" This.Font_Size " Center vCenter c" Brush " x0 y1" , This.Font , This.W , This.H-1 )
Gdip_DeleteBrush( Brush )
Gdip_DeleteGraphics( G )
This.Hover_Bitmap := Gdip_CreateHBITMAPFromBitmap(pBitmap)
Gdip_DisposeImage(pBitmap)
}
Create_Pressed_Bitmap(){
pBitmap:=Gdip_CreateBitmap( This.W , This.H )
G := Gdip_GraphicsFromImage( pBitmap )
Gdip_SetSmoothingMode( G , 2 )
Brush := Gdip_BrushCreateSolid( This.Button_Background_Color )
Gdip_FillRectangle( G , Brush , -1 , -1 , This.W+2 , This.H+2 )
Gdip_DeleteBrush( Brush )
Brush := Gdip_CreateLineBrushFromRect( 0 , 0 , This.W , This.H , "0xFF2A2C2E" , "0xFF45474E" , 1 , 1 )
Gdip_FillRoundedRectangle( G , Brush , 0 , 1 , This.W , This.H-3 , This.Roundness )
Gdip_DeleteBrush( Brush )
Brush := Gdip_BrushCreateSolid( "0xFF2A2C2E" )
Gdip_FillRoundedRectangle( G , Brush , 0 , 0 , This.W , This.H-8 , This.Roundness )
Gdip_DeleteBrush( Brush )
Brush := Gdip_BrushCreateSolid( "0xFF46474D" )
Gdip_FillRoundedRectangle( G , Brush , 0 , 7 , This.W , This.H-8 , This.Roundness )
Gdip_DeleteBrush( Brush )
;------------------------------------------------------------------
if(This.Default_Button)
Brush := Gdip_CreateLineBrushFromRect( 5 , 3 , This.W ,This.H-7 , "0xFF333639" , "0xFF43474B" , 1 , 1 )
else
Brush := Gdip_CreateLineBrushFromRect( 5 , 3 , This.W ,This.H-7 , "0xFF333639" , "0xFF0066aa" , 1 , 1 )
;-----------------------------------------------------------------------
Gdip_FillRoundedRectangle( G , Brush , 1 , 2 , This.W-3 , This.H-6 , This.Roundness )
Gdip_DeleteBrush( Brush )
Pen := Gdip_CreatePen( "0xFF1A1C1F" , 1 )
Gdip_DrawRoundedRectangle( G , Pen , 0 , 0 , This.W-1 , This.H-3 , This.Roundness )
Gdip_DeletePen( Pen )
Brush := Gdip_BrushCreateSolid( This.Text_Color_Bottom )
Gdip_TextToGraphics( G , This.Text , "s" This.Font_Size " Center vCenter c" Brush " x1 y3" , This.Font , This.W , This.H-1 )
Gdip_DeleteBrush( Brush )
Brush := Gdip_BrushCreateSolid( This.Text_Color_Top )
Gdip_TextToGraphics( G , This.Text , "s" This.Font_Size " Center vCenter c" Brush " x0 y2" , This.Font , This.W , This.H-1 )
Gdip_DeleteBrush( Brush )
Gdip_DeleteGraphics( G )
This.Pressed_Bitmap := Gdip_CreateHBITMAPFromBitmap( pBitmap )
Gdip_DisposeImage( pBitmap )
}
Draw_Default(){
SetImage( This.Hwnd , This.Default_Bitmap )
}
Draw_Hover(){
SetImage( This.Hwnd , This.Hover_Bitmap )
}
Draw_Pressed(){
SetImage( This.Hwnd , This.Pressed_Bitmap )
SetTimer , HB_Button_Hover , Off
While( GetKeyState( "LButton" ) )
sleep , 10
SetTimer , HB_Button_Hover , On
MouseGetPos,,,, ctrl , 2
if( This.Hwnd != ctrl ){
This.Draw_Default()
return False
}else {
This.Draw_Hover()
return true
}
}
}
Many thanks Rob