EntropicBlackhole

AHK Sketch (GDIP's functions included)

Dec 12th, 2021 (edited)
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #SingleInstance, Force
  2. #NoEnv
  3. SetBatchLines, -1
  4. if !pToken := Gdip_Startup()
  5. {
  6.     MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
  7.     ExitApp
  8. }
  9. OnExit, Exit
  10. Width := 600
  11. Height := 500
  12. Gui, 1: +E0x80000 +LastFound
  13. Gui, 1: Show
  14. hwnd1 := WinExist()
  15. hbm := CreateDIBSection(Width, Height)
  16. hdc := CreateCompatibleDC()
  17. obm := SelectObject(hdc, hbm)
  18. G := Gdip_GraphicsFromHDC(hdc)
  19. pBrush := Gdip_BrushCreateSolid(0xFFFF0000)
  20. Gdip_FillRectangle(G, pBrush, 0, 0, 600, 500)
  21. Gdip_DeleteBrush(pBrush)
  22. pBrush := Gdip_BrushCreateSolid(0xFFFFFFFF)
  23. Gdip_FillEllipse(G, pBrush, 5, 420, 75, 75)
  24. Gdip_FillEllipse(G, pBrush, 520, 420, 75, 75)
  25. Gdip_DeleteBrush(pBrush)
  26. pBrush := Gdip_BrushCreateSolid(0xFFC4C4C4)
  27. Gdip_FillRectangle(G, pBrush, 75, 75, 450, 350)
  28. Gdip_DeleteBrush(pBrush)
  29. Gdip_TextToGraphics(G, "AHK Sketch", "w450 h75 x80 y10 s40 Center", "Rondo")
  30. UpdateLayeredWindow(hwnd1, hdc, A_ScreenWidth/4+Width/8, A_ScreenHeight/4, Width, Height)
  31. MyPen := Gdip_CreatePen("0xFF000000", 5)
  32. xPos := BackupX := 300
  33. yPos := BackupY := 250
  34. return
  35.  
  36. #If WinActive("ahk_id " hwnd1)
  37. a::
  38. d::
  39. s::
  40. w::
  41. #If
  42. dir := (A_ThisHotkey = "s" || A_ThisHotkey = "w") ? "x" : "y"
  43. xdir := (A_ThisHotkey = "s" || A_ThisHotkey = "w") ? "y" : "x"
  44. Backup%dir% := %dir%pos
  45. %xdir%Pos += (A_ThisHotkey = "d" || A_ThisHotkey = "a") ? ((A_ThisHotkey = "d") ? (xPos > 520 ? 0 : 5) : (xPos < 80 ? 0 : -5)) : ((A_ThisHotkey = "s") ? (yPos > 420 ? 0 : 5) : (yPos < 80 ? 0 : -5))
  46. Gdip_DrawLine(G, MyPen, BackupX, BackupY, XPos, YPos)
  47. UpdateLayeredWindow(hwnd1, hdc, A_ScreenWidth/4+Width/8, A_ScreenHeight/4, Width, Height)
  48. return
  49.  
  50. Esc::
  51. Exit:
  52. Gdip_Shutdown(pToken)
  53. ExitApp
  54. return
  55.  
  56. Gdip_Startup()
  57. {
  58.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  59.    
  60.     if !DllCall("GetModuleHandle", "str", "gdiplus", Ptr)
  61.         DllCall("LoadLibrary", "str", "gdiplus")
  62.     VarSetCapacity(si, A_PtrSize = 8 ? 24 : 16, 0), si := Chr(1)
  63.     DllCall("gdiplus\GdiplusStartup", A_PtrSize ? "UPtr*" : "uint*", pToken, Ptr, &si, Ptr, 0)
  64.     return pToken
  65. }
  66. CreateDIBSection(w, h, hdc="", bpp=32, ByRef ppvBits=0)
  67. {
  68.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  69.    
  70.     hdc2 := hdc ? hdc : GetDC()
  71.     VarSetCapacity(bi, 40, 0)
  72.    
  73.     NumPut(w, bi, 4, "uint")
  74.     , NumPut(h, bi, 8, "uint")
  75.     , NumPut(40, bi, 0, "uint")
  76.     , NumPut(1, bi, 12, "ushort")
  77.     , NumPut(0, bi, 16, "uInt")
  78.     , NumPut(bpp, bi, 14, "ushort")
  79.    
  80.     hbm := DllCall("CreateDIBSection"
  81.                     , Ptr, hdc2
  82.                     , Ptr, &bi
  83.                     , "uint", 0
  84.                     , A_PtrSize ? "UPtr*" : "uint*", ppvBits
  85.                     , Ptr, 0
  86.                     , "uint", 0, Ptr)
  87.  
  88.     if !hdc
  89.         ReleaseDC(hdc2)
  90.     return hbm
  91. }
  92. CreateCompatibleDC(hdc=0)
  93. {
  94.    return DllCall("CreateCompatibleDC", A_PtrSize ? "UPtr" : "UInt", hdc)
  95. }
  96. SelectObject(hdc, hgdiobj)
  97. {
  98.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  99.    
  100.     return DllCall("SelectObject", Ptr, hdc, Ptr, hgdiobj)
  101. }
  102. Gdip_GraphicsFromHDC(hdc)
  103. {
  104.     DllCall("gdiplus\GdipCreateFromHDC", A_PtrSize ? "UPtr" : "UInt", hdc, A_PtrSize ? "UPtr*" : "UInt*", pGraphics)
  105.     return pGraphics
  106. }
  107. Gdip_BrushCreateSolid(ARGB=0xff000000)
  108. {
  109.     DllCall("gdiplus\GdipCreateSolidFill", "UInt", ARGB, A_PtrSize ? "UPtr*" : "UInt*", pBrush)
  110.     return pBrush
  111. }
  112. Gdip_FillRectangle(pGraphics, pBrush, x, y, w, h)
  113. {
  114.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  115.    
  116.     return DllCall("gdiplus\GdipFillRectangle"
  117.                     , Ptr, pGraphics
  118.                     , Ptr, pBrush
  119.                     , "float", x
  120.                     , "float", y
  121.                     , "float", w
  122.                     , "float", h)
  123. }
  124. Gdip_FillEllipse(pGraphics, pBrush, x, y, w, h)
  125. {
  126.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  127.    
  128.     return DllCall("gdiplus\GdipFillEllipse", Ptr, pGraphics, Ptr, pBrush, "float", x, "float", y, "float", w, "float", h)
  129. }
  130. Gdip_DeleteBrush(pBrush)
  131. {
  132.    return DllCall("gdiplus\GdipDeleteBrush", A_PtrSize ? "UPtr" : "UInt", pBrush)
  133. }
  134. Gdip_DrawLine(pGraphics, pPen, x1, y1, x2, y2)
  135. {
  136.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  137.    
  138.     return DllCall("gdiplus\GdipDrawLine"
  139.                     , Ptr, pGraphics
  140.                     , Ptr, pPen
  141.                     , "float", x1
  142.                     , "float", y1
  143.                     , "float", x2
  144.                     , "float", y2)
  145. }
  146. Gdip_CloneBrush(pBrush)
  147. {
  148.     DllCall("gdiplus\GdipCloneBrush", A_PtrSize ? "UPtr" : "UInt", pBrush, A_PtrSize ? "UPtr*" : "UInt*", pBrushClone)
  149.     return pBrushClone
  150. }
  151. Gdip_FontFamilyCreate(Font)
  152. {
  153.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  154.    
  155.     if (!A_IsUnicode)
  156.     {
  157.         nSize := DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &Font, "int", -1, "uint", 0, "int", 0)
  158.         VarSetCapacity(wFont, nSize*2)
  159.         DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &Font, "int", -1, Ptr, &wFont, "int", nSize)
  160.     }
  161.    
  162.     DllCall("gdiplus\GdipCreateFontFamilyFromName"
  163.                     , Ptr, A_IsUnicode ? &Font : &wFont
  164.                     , "uint", 0
  165.                     , A_PtrSize ? "UPtr*" : "UInt*", hFamily)
  166.    
  167.     return hFamily
  168. }
  169. Gdip_FontCreate(hFamily, Size, Style=0)
  170. {
  171.    DllCall("gdiplus\GdipCreateFont", A_PtrSize ? "UPtr" : "UInt", hFamily, "float", Size, "int", Style, "int", 0, A_PtrSize ? "UPtr*" : "UInt*", hFont)
  172.    return hFont
  173. }
  174. Gdip_StringFormatCreate(Format=0, Lang=0)
  175. {
  176.    DllCall("gdiplus\GdipCreateStringFormat", "int", Format, "int", Lang, A_PtrSize ? "UPtr*" : "UInt*", hFormat)
  177.    return hFormat
  178. }
  179. CreateRectF(ByRef RectF, x, y, w, h)
  180. {
  181.    VarSetCapacity(RectF, 16)
  182.    NumPut(x, RectF, 0, "float"), NumPut(y, RectF, 4, "float"), NumPut(w, RectF, 8, "float"), NumPut(h, RectF, 12, "float")
  183. }
  184. Gdip_SetStringFormatAlign(hFormat, Align)
  185. {
  186.    return DllCall("gdiplus\GdipSetStringFormatAlign", A_PtrSize ? "UPtr" : "UInt", hFormat, "int", Align)
  187. }
  188. Gdip_TextToGraphics(pGraphics, Text, Options, Font="Arial", Width="", Height="", Measure=0)
  189. {
  190.     IWidth := Width, IHeight:= Height
  191.    
  192.     RegExMatch(Options, "i)X([\-\d\.]+)(p*)", xpos)
  193.     RegExMatch(Options, "i)Y([\-\d\.]+)(p*)", ypos)
  194.     RegExMatch(Options, "i)W([\-\d\.]+)(p*)", Width)
  195.     RegExMatch(Options, "i)H([\-\d\.]+)(p*)", Height)
  196.     RegExMatch(Options, "i)C(?!(entre|enter))([a-f\d]+)", Colour)
  197.     RegExMatch(Options, "i)Top|Up|Bottom|Down|vCentre|vCenter", vPos)
  198.     RegExMatch(Options, "i)NoWrap", NoWrap)
  199.     RegExMatch(Options, "i)R(\d)", Rendering)
  200.     RegExMatch(Options, "i)S(\d+)(p*)", Size)
  201.  
  202.     if !Gdip_DeleteBrush(Gdip_CloneBrush(Colour2))
  203.         PassBrush := 1, pBrush := Colour2
  204.    
  205.     if !(IWidth && IHeight) && (xpos2 || ypos2 || Width2 || Height2 || Size2)
  206.         return -1
  207.  
  208.     Style := 0, Styles := "Regular|Bold|Italic|BoldItalic|Underline|Strikeout"
  209.     Loop, Parse, Styles, |
  210.     {
  211.         if RegExMatch(Options, "\b" A_loopField)
  212.         Style |= (A_LoopField != "StrikeOut") ? (A_Index-1) : 8
  213.     }
  214.  
  215.     Align := 0, Alignments := "Near|Left|Centre|Center|Far|Right"
  216.     Loop, Parse, Alignments, |
  217.     {
  218.         if RegExMatch(Options, "\b" A_loopField)
  219.             Align |= A_Index//2.1      ; 0|0|1|1|2|2
  220.     }
  221.  
  222.     xpos := (xpos1 != "") ? xpos2 ? IWidth*(xpos1/100) : xpos1 : 0
  223.     ypos := (ypos1 != "") ? ypos2 ? IHeight*(ypos1/100) : ypos1 : 0
  224.     Width := Width1 ? Width2 ? IWidth*(Width1/100) : Width1 : IWidth
  225.     Height := Height1 ? Height2 ? IHeight*(Height1/100) : Height1 : IHeight
  226.     if !PassBrush
  227.         Colour := "0x" (Colour2 ? Colour2 : "ff000000")
  228.     Rendering := ((Rendering1 >= 0) && (Rendering1 <= 5)) ? Rendering1 : 4
  229.     Size := (Size1 > 0) ? Size2 ? IHeight*(Size1/100) : Size1 : 12
  230.  
  231.     hFamily := Gdip_FontFamilyCreate(Font)
  232.     hFont := Gdip_FontCreate(hFamily, Size, Style)
  233.     FormatStyle := NoWrap ? 0x4000 | 0x1000 : 0x4000
  234.     hFormat := Gdip_StringFormatCreate(FormatStyle)
  235.     pBrush := PassBrush ? pBrush : Gdip_BrushCreateSolid(Colour)
  236.     if !(hFamily && hFont && hFormat && pBrush && pGraphics)
  237.         return !pGraphics ? -2 : !hFamily ? -3 : !hFont ? -4 : !hFormat ? -5 : !pBrush ? -6 : 0
  238.    
  239.     CreateRectF(RC, xpos, ypos, Width, Height)
  240.     Gdip_SetStringFormatAlign(hFormat, Align)
  241.     Gdip_SetTextRenderingHint(pGraphics, Rendering)
  242.     ReturnRC := Gdip_MeasureString(pGraphics, Text, hFont, hFormat, RC)
  243.  
  244.     if vPos
  245.     {
  246.         StringSplit, ReturnRC, ReturnRC, |
  247.        
  248.         if (vPos = "vCentre") || (vPos = "vCenter")
  249.             ypos += (Height-ReturnRC4)//2
  250.         else if (vPos = "Top") || (vPos = "Up")
  251.             ypos := 0
  252.         else if (vPos = "Bottom") || (vPos = "Down")
  253.             ypos := Height-ReturnRC4
  254.        
  255.         CreateRectF(RC, xpos, ypos, Width, ReturnRC4)
  256.         ReturnRC := Gdip_MeasureString(pGraphics, Text, hFont, hFormat, RC)
  257.     }
  258.  
  259.     if !Measure
  260.         E := Gdip_DrawString(pGraphics, Text, hFont, hFormat, pBrush, RC)
  261.  
  262.     if !PassBrush
  263.         Gdip_DeleteBrush(pBrush)
  264.     Gdip_DeleteStringFormat(hFormat)  
  265.     Gdip_DeleteFont(hFont)
  266.     Gdip_DeleteFontFamily(hFamily)
  267.     return E ? E : ReturnRC
  268. }
  269. Gdip_DeleteFont(hFont)
  270. {
  271.    return DllCall("gdiplus\GdipDeleteFont", A_PtrSize ? "UPtr" : "UInt", hFont)
  272. }
  273. Gdip_DeleteFontFamily(hFamily)
  274. {
  275.    return DllCall("gdiplus\GdipDeleteFontFamily", A_PtrSize ? "UPtr" : "UInt", hFamily)
  276. }
  277. Gdip_DeleteStringFormat(hFormat)
  278. {
  279.    return DllCall("gdiplus\GdipDeleteStringFormat", A_PtrSize ? "UPtr" : "UInt", hFormat)
  280. }
  281. Gdip_DrawString(pGraphics, sString, hFont, hFormat, pBrush, ByRef RectF)
  282. {
  283.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  284.    
  285.     if (!A_IsUnicode)
  286.     {
  287.         nSize := DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &sString, "int", -1, Ptr, 0, "int", 0)
  288.         VarSetCapacity(wString, nSize*2)
  289.         DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &sString, "int", -1, Ptr, &wString, "int", nSize)
  290.     }
  291.    
  292.     return DllCall("gdiplus\GdipDrawString"
  293.                     , Ptr, pGraphics
  294.                     , Ptr, A_IsUnicode ? &sString : &wString
  295.                     , "int", -1
  296.                     , Ptr, hFont
  297.                     , Ptr, &RectF
  298.                     , Ptr, hFormat
  299.                     , Ptr, pBrush)
  300. }
  301. Gdip_MeasureString(pGraphics, sString, hFont, hFormat, ByRef RectF)
  302. {
  303.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  304.    
  305.     VarSetCapacity(RC, 16)
  306.     if !A_IsUnicode
  307.     {
  308.         nSize := DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &sString, "int", -1, "uint", 0, "int", 0)
  309.         VarSetCapacity(wString, nSize*2)  
  310.         DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &sString, "int", -1, Ptr, &wString, "int", nSize)
  311.     }
  312.    
  313.     DllCall("gdiplus\GdipMeasureString"
  314.                     , Ptr, pGraphics
  315.                     , Ptr, A_IsUnicode ? &sString : &wString
  316.                     , "int", -1
  317.                     , Ptr, hFont
  318.                     , Ptr, &RectF
  319.                     , Ptr, hFormat
  320.                     , Ptr, &RC
  321.                     , "uint*", Chars
  322.                     , "uint*", Lines)
  323.    
  324.     return &RC ? NumGet(RC, 0, "float") "|" NumGet(RC, 4, "float") "|" NumGet(RC, 8, "float") "|" NumGet(RC, 12, "float") "|" Chars "|" Lines : 0
  325. }
  326. Gdip_SetTextRenderingHint(pGraphics, RenderingHint)
  327. {
  328.     return DllCall("gdiplus\GdipSetTextRenderingHint", A_PtrSize ? "UPtr" : "UInt", pGraphics, "int", RenderingHint)
  329. }
  330. UpdateLayeredWindow(hwnd, hdc, x="", y="", w="", h="", Alpha=255)
  331. {
  332.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  333.    
  334.     if ((x != "") && (y != ""))
  335.         VarSetCapacity(pt, 8), NumPut(x, pt, 0, "UInt"), NumPut(y, pt, 4, "UInt")
  336.  
  337.     if (w = "") ||(h = "")
  338.         WinGetPos,,, w, h, ahk_id %hwnd%
  339.    
  340.     return DllCall("UpdateLayeredWindow"
  341.                     , Ptr, hwnd
  342.                     , Ptr, 0
  343.                     , Ptr, ((x = "") && (y = "")) ? 0 : &pt
  344.                     , "int64*", w|h<<32
  345.                     , Ptr, hdc
  346.                     , "int64*", 0
  347.                     , "uint", 0
  348.                     , "UInt*", Alpha<<16|1<<24
  349.                     , "uint", 2)
  350. }
  351. Gdip_CreatePen(ARGB, w)
  352. {
  353.    DllCall("gdiplus\GdipCreatePen1", "UInt", ARGB, "float", w, "int", 2, A_PtrSize ? "UPtr*" : "UInt*", pPen)
  354.    return pPen
  355. }
  356. Gdip_Shutdown(pToken)
  357. {
  358.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  359.    
  360.     DllCall("gdiplus\GdiplusShutdown", Ptr, pToken)
  361.     if hModule := DllCall("GetModuleHandle", "str", "gdiplus", Ptr)
  362.         DllCall("FreeLibrary", Ptr, hModule)
  363.     return 0
  364. }
  365. GetDC(hwnd=0)
  366. {
  367.     return DllCall("GetDC", A_PtrSize ? "UPtr" : "UInt", hwnd)
  368. }
  369. ReleaseDC(hdc, hwnd=0)
  370. {
  371.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  372.    
  373.     return DllCall("ReleaseDC", Ptr, hwnd, Ptr, hdc)
  374. }
Add Comment
Please, Sign In to add comment