UseGDIP()

Post your working scripts, libraries and tools for AHK v1.1 and older
just me
Posts: 9763
Joined: 02 Oct 2013, 08:51
Location: Germany

UseGDIP()

06 Jun 2015, 05:15

Some of us including me are sometimes using direct GDI+ DllCalls instead of including the GDIP lib. In this cases, you have to determine when and where to load the DLL and call GdiplusStartup() to initialize GDI+, and also, when and where to call GdiplusShutdown() and unload the DLL. To simplify this process I wrote this function. It should be stored in one of the function library folders to be auto-included when needed. It loads and initializes GDI+ at load-time and also calls GdiplusShutdown() and unloads the DLL when the script terminates. Of course, you are still responsible to destroy all existing GDI+ objects before the shutdown will be done.

Code: Select all

UseGDIP(Params*) { ; Loads and initializes the Gdiplus.dll at load-time
   ; GET_MODULE_HANDLE_EX_FLAG_PIN = 0x00000001
   Static GdipObject := ""
        , GdipModule := ""
        , GdipToken  := ""
   Static OnLoad := UseGDIP()
   If (GdipModule = "") {
      If !DllCall("LoadLibrary", "Str", "Gdiplus.dll", "UPtr")
         UseGDIP_Error("The Gdiplus.dll could not be loaded!`n`nThe program will exit!")
      If !DllCall("GetModuleHandleEx", "UInt", 0x00000001, "Str", "Gdiplus.dll", "PtrP", GdipModule, "UInt")
         UseGDIP_Error("The Gdiplus.dll could not be loaded!`n`nThe program will exit!")
      VarSetCapacity(SI, 24, 0), NumPut(1, SI, 0, "UInt") ; size of 64-bit structure
      If DllCall("Gdiplus.dll\GdiplusStartup", "PtrP", GdipToken, "Ptr", &SI, "Ptr", 0)
         UseGDIP_Error("GDI+ could not be startet!`n`nThe program will exit!")
      GdipObject := {Base: {__Delete: Func("UseGDIP").Bind(GdipModule, GdipToken)}}
   }
   Else If (Params[1] = GdipModule) && (Params[2] = GdipToken)
      DllCall("Gdiplus.dll\GdiplusShutdown", "Ptr", GdipToken)
}
UseGDIP_Error(ErrorMsg) {
   MsgBox, 262160, UseGDIP, %ErrorMsg%
   ExitApp
}
Note: On my Win 8.1 64-bit system UseGDIP() needs about 220 KB additional memory.

Edit: Function updated on 2020-11-10: Added GetModuleHandleEx() to prevent untimely release.
Last edited by just me on 10 Nov 2020, 06:15, edited 1 time in total.
guest3456
Posts: 3477
Joined: 09 Oct 2013, 10:31

Re: UseGDIP()

07 Jun 2015, 19:35

just me wrote:Some of us including me are sometimes using direct GDI+ DllCalls instead of including the GDIP lib.
curious why you're doing this?

just me
Posts: 9763
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: UseGDIP()

08 Jun 2015, 01:06

guest3456 wrote:curious why you're doing this?
  1. Because I usually try to avoid unnecessary dependencies for scripts I want to publish and to maintain.
  2. Because I don't want to include a ~2500 lines library when all I need are a few DllCalls.
  3. Because I can.
guest3456
Posts: 3477
Joined: 09 Oct 2013, 10:31

Re: UseGDIP()

08 Jun 2015, 10:14

you can just cut out the functions you don't use..

User avatar
jNizM
Posts: 3201
Joined: 30 Sep 2013, 01:33
Contact:

Re: UseGDIP()

09 Jun 2015, 01:11

I think he mean more like this:

He (and I) prefer the direct input way of the DllCall's like:

Code: Select all

...
DllCall("gdiplus.dll\GdipCreateFromHDC", "Ptr", hdc, "PtrP", pGraphics)
DllCall("gdiplus.dll\GdipSetSmoothingMode", "Ptr", pGraphics, "Int", 4)
...
Instand of using big lib and everything as function:

Code: Select all

#Include <over_9000_lines_big_gdip_lib.ahk>

...
pGraphics := Gdip_GraphicsFromHDC(hdc)
Gdip_SetSmoothingMode(pGraphics, 4)
...

/*
Gdip_GraphicsFromHDC(hdc)
{
    DllCall("gdiplus\GdipCreateFromHDC", A_PtrSize ? "UPtr" : "UInt", hdc, A_PtrSize ? "UPtr*" : "UInt*", pGraphics)
    return pGraphics
}

Gdip_SetSmoothingMode(pGraphics, SmoothingMode)
{
   return DllCall("gdiplus\GdipSetSmoothingMode", A_PtrSize ? "UPtr" : "UInt", pGraphics, "int", SmoothingMode)
}
*/
[AHK] v2.0.18 | [WIN] 11 Pro (23H2) | [GitHub] Profile

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 129 guests