Gdip within a function Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Archandrion
Posts: 31
Joined: 26 May 2018, 22:23

Gdip within a function

24 Jun 2018, 05:51

How can I get TestGdipToScreen() to work. The code only works when it is not inside the function as shown below where I commented out the function's beginning and ending. Also without the GDIpHelper what would be the proper code to include in the function as I tried pToken := Gdip_Startup() at the beginning and Gdip_Shutdown(pToken) at the end but that did not work. I have created other functions that just use those pToken := Gdip_Startup() and Gdip_Shutdown(pToken) that work so I am not sure what I'm doing wrong in this particular instance.

Code: Select all

#Include %A_ScriptDir%
#Include Gdip_All.ahk
#Include GDIpHelper.ahk

;t::TestGdipToScreen()


;TestGdipToScreen(){
SetUpGDIP()
StartDrawGDIP()
ClearDrawGDIP()

CenterX := Round(A_ScreenWidth / 2)
CenterY := Round(A_ScreenHeight / 2)

TexToGraphicsText := "This is a test"
TexToGraphicsTextColor := "cff000000"
TexToGraphicsTextSize := "72"

;ex. "x460 y190 cff000000 r4 s72"
TexToGraphicsOpions := "x" . CenterX . A_Space . "y" . CenterY . A_Space . TexToGraphicsTextColor .  A_Space . "r4" . A_Space . "s" . TexToGraphicsTextSize



Gdip_SetSmoothingMode(G, 4)

;ex. "x460 y190 cff000000 r4 s72"
Gdip_TextToGraphics(G, TexToGraphicsText , TexToGraphicsOpions)


EndDrawGDIP()
Return
;}

`::ExitApp
r::Reload
The above worked but the code below did not.

Code: Select all

#Include %A_ScriptDir%
#Include Gdip_All.ahk
#Include GDIpHelper.ahk

t::TestGdipToScreen()


TestGdipToScreen(){
SetUpGDIP()
StartDrawGDIP()
ClearDrawGDIP()

CenterX := Round(A_ScreenWidth / 2)
CenterY := Round(A_ScreenHeight / 2)

TexToGraphicsText := "This is a test"
TexToGraphicsTextColor := "cff000000"
TexToGraphicsTextSize := "72"

;ex. "x460 y190 cff000000 r4 s72"
TexToGraphicsOpions := "x" . CenterX . A_Space . "y" . CenterY . A_Space . TexToGraphicsTextColor .  A_Space . "r4" . A_Space . "s" . TexToGraphicsTextSize



Gdip_SetSmoothingMode(G, 4)

;ex. "x460 y190 cff000000 r4 s72"
Gdip_TextToGraphics(G, TexToGraphicsText , TexToGraphicsOpions)


EndDrawGDIP()
Return
}

`::ExitApp
r::Reload
Last edited by Archandrion on 24 Jun 2018, 06:54, edited 3 times in total.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: GDI within a function  Topic is solved

24 Jun 2018, 06:42

you "helper" lib appears to be dumping its contents into the global scope. you can either:
  • make you custom function assume-global
  • declare the global variables that u wish to use inside the function
  • pass them as arguments
see https://autohotkey.com/docs/Functions.htm#Global
Archandrion
Posts: 31
Joined: 26 May 2018, 22:23

Re: GDI within a function

24 Jun 2018, 07:12

swagfag wrote:you "helper" lib appears to be dumping its contents into the global scope. you can either:
  • make you custom function assume-global
  • declare the global variables that u wish to use inside the function
  • pass them as arguments
see https://autohotkey.com/docs/Functions.htm#Global
Thanks, assume-global seems to be the simplest option. The code below is now working.

Code: Select all

#Include %A_ScriptDir%
#Include .\lib\Gdip_All.ahk
#Include .\lib\GDIpHelper.ahk

t::TestGdipToScreen()


TestGdipToScreen(){
Global ;This fixed the issue

SetUpGDIP()
StartDrawGDIP()
ClearDrawGDIP()

CenterX := Round(A_ScreenWidth / 2)
CenterY := Round(A_ScreenHeight / 2)

TexToGraphicsText := "This is a test"
TexToGraphicsTextColor := "cff000000"
TexToGraphicsTextSize := "72"

;ex. "x460 y190 cff000000 r4 s72"
TexToGraphicsOpions := "x" . CenterX . A_Space . "y" . CenterY . A_Space . TexToGraphicsTextColor .  A_Space . "r4" . A_Space . "s" . TexToGraphicsTextSize



Gdip_SetSmoothingMode(G, 4)

;ex. "x460 y190 cff000000 r4 s72"
Gdip_TextToGraphics(G, TexToGraphicsText , TexToGraphicsOpions)


EndDrawGDIP()
Return
}

`::ExitApp
r::Reload
To avoid dumping the variables into the global scope I will use the code below in the future as it is also working without the GDIpHelper.

Code: Select all

#Include %A_ScriptDir%
#Include .\lib\Gdip_All.ahk
#Include .\lib\GDIpHelper.ahk

t::TestGdipToScreen()


TestGdipToScreen(){

    ;Gdip Srartup Block--------------------Start
    ;SetUpGDIP
    iWidth := -1
    iHeight := -1
    Width := iWidth
    Height := iHeight
    If (iWidth < 0) {
    	Width := A_ScreenWidth
    }
    if (iHeight < 0) {
    	height := A_ScreenHeight
    }
    
    
    ;JustTheBasics
    	; Start gdi+
    If !pToken := Gdip_Startup()
    {
    	MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
    	ExitApp
    }

    
    ; Create a layered window (+E0x80000 : must be used for UpdateLayeredWindow to work!) that is always on top (+AlwaysOnTop), has no taskbar entry or caption
    Gui, 1: -Caption +E0x80000 +LastFound +AlwaysOnTop +ToolWindow +OwnDialogs
    
    ; Show the window
    Gui, 1: Show, NA
    
    ; Get a handle to this window we have created in order to update it later
    hwnd1 := WinExist()
    
    ;StartDrawGDIP
    ; Create a gdi bitmap with width and height of what we are going to draw into it. This is the entire drawing area for everything
    hbm := CreateDIBSection(Width, Height)
    
    ; Get a device context compatible with the screen
    hdc := CreateCompatibleDC()
    
    ; Select the bitmap into the device context
    obm := SelectObject(hdc, hbm)
    
    ; Get a pointer to the graphics of the bitmap, for use with drawing functions
    G := Gdip_GraphicsFromHDC(hdc)	
    Gdip_GraphicsClear(G)
    ;Gdip Srartup Block--------------------End
     

CenterX := Round(A_ScreenWidth / 2)
CenterY := Round(A_ScreenHeight / 2)

TexToGraphicsText := "This is a test"
TexToGraphicsTextColor := "cff000000"
TexToGraphicsTextSize := "72"

;ex. "x460 y190 cff000000 r4 s72"
TexToGraphicsOpions := "x" . CenterX . A_Space . "y" . CenterY . A_Space . TexToGraphicsTextColor .  A_Space . "r4" . A_Space . "s" . TexToGraphicsTextSize



Gdip_SetSmoothingMode(G2, 4)

;ex. "x460 y190 cff000000 r4 s72"
Gdip_TextToGraphics(G, TexToGraphicsText , TexToGraphicsOpions)


    ;Gdip Close Block--------------------Start
    ;EndDrawGDIP() {
    ; Update the specified window we have created (hwnd1) with a handle to our bitmap (hdc), specifying the x,y,w,h we want it positioned on our screen
    ; So this will position our gui at (0,0) with the Width and Height specified earlier
    UpdateLayeredWindow(hwnd1, hdc, 0, 0, Width, Height)
    
    
    ; Select the object back into the hdc
    SelectObject(hdc, obm)
    
    ; Now the bitmap may be deleted
    DeleteObject(hbm)
    
    ; Also the device context related to the bitmap may be deleted
    DeleteDC(hdc)
    
    ; The graphics may now be deleted
    Gdip_DeleteGraphics(G)
    Gdip_Shutdown(pToken)
    ;Gdip Close Block--------------------End
Return
}

`::ExitApp
r::Reload

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Lamron750 and 400 guests