Guest User

Gdip.ahk

a guest
May 20th, 2018
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; Gdip standard library v1.45 by tic (Tariq Porter) 07/09/11
  2. ; Modifed by Rseding91 using fincs 64 bit compatible Gdip library 5/1/2013
  3. ; Supports: Basic, _L ANSi, _L Unicode x86 and _L Unicode x64
  4. ;
  5. ; Updated 2/20/2014 - fixed Gdip_CreateRegion() and Gdip_GetClipRegion() on AHK Unicode x86
  6. ; Updated 5/13/2013 - fixed Gdip_SetBitmapToClipboard() on AHK Unicode x64
  7. ;
  8. ;#####################################################################################
  9. ;#####################################################################################
  10. ; STATUS ENUMERATION
  11. ; Return values for functions specified to have status enumerated return type
  12. ;#####################################################################################
  13. ;
  14. ; Ok =                      = 0
  15. ; GenericError              = 1
  16. ; InvalidParameter          = 2
  17. ; OutOfMemory               = 3
  18. ; ObjectBusy                = 4
  19. ; InsufficientBuffer        = 5
  20. ; NotImplemented            = 6
  21. ; Win32Error                = 7
  22. ; WrongState                = 8
  23. ; Aborted                   = 9
  24. ; FileNotFound              = 10
  25. ; ValueOverflow             = 11
  26. ; AccessDenied              = 12
  27. ; UnknownImageFormat        = 13
  28. ; FontFamilyNotFound        = 14
  29. ; FontStyleNotFound         = 15
  30. ; NotTrueTypeFont           = 16
  31. ; UnsupportedGdiplusVersion = 17
  32. ; GdiplusNotInitialized     = 18
  33. ; PropertyNotFound          = 19
  34. ; PropertyNotSupported      = 20
  35. ; ProfileNotFound           = 21
  36. ;
  37. ;#####################################################################################
  38. ;#####################################################################################
  39. ; FUNCTIONS
  40. ;#####################################################################################
  41. ;
  42. ; UpdateLayeredWindow(hwnd, hdc, x="", y="", w="", h="", Alpha=255)
  43. ; BitBlt(ddc, dx, dy, dw, dh, sdc, sx, sy, Raster="")
  44. ; StretchBlt(dDC, dx, dy, dw, dh, sDC, sx, sy, sw, sh, Raster="")
  45. ; SetImage(hwnd, hBitmap)
  46. ; Gdip_BitmapFromScreen(Screen=0, Raster="")
  47. ; CreateRectF(ByRef RectF, x, y, w, h)
  48. ; CreateSizeF(ByRef SizeF, w, h)
  49. ; CreateDIBSection
  50. ;
  51. ;#####################################################################################
  52.  
  53. ; Function:                 UpdateLayeredWindow
  54. ; Description:              Updates a layered window with the handle to the DC of a gdi bitmap
  55. ;
  56. ; hwnd                      Handle of the layered window to update
  57. ; hdc                       Handle to the DC of the GDI bitmap to update the window with
  58. ; Layeredx                  x position to place the window
  59. ; Layeredy                  y position to place the window
  60. ; Layeredw                  Width of the window
  61. ; Layeredh                  Height of the window
  62. ; Alpha                     Default = 255 : The transparency (0-255) to set the window transparency
  63. ;
  64. ; return                    If the function succeeds, the return value is nonzero
  65. ;
  66. ; notes                     If x or y omitted, then layered window will use its current coordinates
  67. ;                           If w or h omitted then current width and height will be used
  68.  
  69. UpdateLayeredWindow(hwnd, hdc, x="", y="", w="", h="", Alpha=255)
  70. {
  71.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  72.    
  73.     if ((x != "") && (y != ""))
  74.         VarSetCapacity(pt, 8), NumPut(x, pt, 0, "UInt"), NumPut(y, pt, 4, "UInt")
  75.  
  76.     if (w = "") ||(h = "")
  77.         WinGetPos,,, w, h, ahk_id %hwnd%
  78.    
  79.     return DllCall("UpdateLayeredWindow"
  80.                     , Ptr, hwnd
  81.                     , Ptr, 0
  82.                     , Ptr, ((x = "") && (y = "")) ? 0 : &pt
  83.                     , "int64*", w|h<<32
  84.                     , Ptr, hdc
  85.                     , "int64*", 0
  86.                     , "uint", 0
  87.                     , "UInt*", Alpha<<16|1<<24
  88.                     , "uint", 2)
  89. }
  90.  
  91. ;#####################################################################################
  92.  
  93. ; Function              BitBlt
  94. ; Description           The BitBlt function performs a bit-block transfer of the color data corresponding to a rectangle
  95. ;                       of pixels from the specified source device context into a destination device context.
  96. ;
  97. ; dDC                   handle to destination DC
  98. ; dx                    x-coord of destination upper-left corner
  99. ; dy                    y-coord of destination upper-left corner
  100. ; dw                    width of the area to copy
  101. ; dh                    height of the area to copy
  102. ; sDC                   handle to source DC
  103. ; sx                    x-coordinate of source upper-left corner
  104. ; sy                    y-coordinate of source upper-left corner
  105. ; Raster                raster operation code
  106. ;
  107. ; return                If the function succeeds, the return value is nonzero
  108. ;
  109. ; notes                 If no raster operation is specified, then SRCCOPY is used, which copies the source directly to the destination rectangle
  110. ;
  111. ; BLACKNESS             = 0x00000042
  112. ; NOTSRCERASE           = 0x001100A6
  113. ; NOTSRCCOPY            = 0x00330008
  114. ; SRCERASE              = 0x00440328
  115. ; DSTINVERT             = 0x00550009
  116. ; PATINVERT             = 0x005A0049
  117. ; SRCINVERT             = 0x00660046
  118. ; SRCAND                = 0x008800C6
  119. ; MERGEPAINT            = 0x00BB0226
  120. ; MERGECOPY             = 0x00C000CA
  121. ; SRCCOPY               = 0x00CC0020
  122. ; SRCPAINT              = 0x00EE0086
  123. ; PATCOPY               = 0x00F00021
  124. ; PATPAINT              = 0x00FB0A09
  125. ; WHITENESS             = 0x00FF0062
  126. ; CAPTUREBLT            = 0x40000000
  127. ; NOMIRRORBITMAP        = 0x80000000
  128.  
  129. BitBlt(ddc, dx, dy, dw, dh, sdc, sx, sy, Raster="")
  130. {
  131.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  132.    
  133.     return DllCall("gdi32\BitBlt"
  134.                     , Ptr, dDC
  135.                     , "int", dx
  136.                     , "int", dy
  137.                     , "int", dw
  138.                     , "int", dh
  139.                     , Ptr, sDC
  140.                     , "int", sx
  141.                     , "int", sy
  142.                     , "uint", Raster ? Raster : 0x00CC0020)
  143. }
  144.  
  145. ;#####################################################################################
  146.  
  147. ; Function              StretchBlt
  148. ; Description           The StretchBlt function copies a bitmap from a source rectangle into a destination rectangle,
  149. ;                       stretching or compressing the bitmap to fit the dimensions of the destination rectangle, if necessary.
  150. ;                       The system stretches or compresses the bitmap according to the stretching mode currently set in the destination device context.
  151. ;
  152. ; ddc                   handle to destination DC
  153. ; dx                    x-coord of destination upper-left corner
  154. ; dy                    y-coord of destination upper-left corner
  155. ; dw                    width of destination rectangle
  156. ; dh                    height of destination rectangle
  157. ; sdc                   handle to source DC
  158. ; sx                    x-coordinate of source upper-left corner
  159. ; sy                    y-coordinate of source upper-left corner
  160. ; sw                    width of source rectangle
  161. ; sh                    height of source rectangle
  162. ; Raster                raster operation code
  163. ;
  164. ; return                If the function succeeds, the return value is nonzero
  165. ;
  166. ; notes                 If no raster operation is specified, then SRCCOPY is used. It uses the same raster operations as BitBlt    
  167.  
  168. StretchBlt(ddc, dx, dy, dw, dh, sdc, sx, sy, sw, sh, Raster="")
  169. {
  170.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  171.    
  172.     return DllCall("gdi32\StretchBlt"
  173.                     , Ptr, ddc
  174.                     , "int", dx
  175.                     , "int", dy
  176.                     , "int", dw
  177.                     , "int", dh
  178.                     , Ptr, sdc
  179.                     , "int", sx
  180.                     , "int", sy
  181.                     , "int", sw
  182.                     , "int", sh
  183.                     , "uint", Raster ? Raster : 0x00CC0020)
  184. }
  185.  
  186. ;#####################################################################################
  187.  
  188. ; Function              SetStretchBltMode
  189. ; Description           The SetStretchBltMode function sets the bitmap stretching mode in the specified device context
  190. ;
  191. ; hdc                   handle to the DC
  192. ; iStretchMode          The stretching mode, describing how the target will be stretched
  193. ;
  194. ; return                If the function succeeds, the return value is the previous stretching mode. If it fails it will return 0
  195. ;
  196. ; STRETCH_ANDSCANS      = 0x01
  197. ; STRETCH_ORSCANS       = 0x02
  198. ; STRETCH_DELETESCANS   = 0x03
  199. ; STRETCH_HALFTONE      = 0x04
  200.  
  201. SetStretchBltMode(hdc, iStretchMode=4)
  202. {
  203.     return DllCall("gdi32\SetStretchBltMode"
  204.                     , A_PtrSize ? "UPtr" : "UInt", hdc
  205.                     , "int", iStretchMode)
  206. }
  207.  
  208. ;#####################################################################################
  209.  
  210. ; Function              SetImage
  211. ; Description           Associates a new image with a static control
  212. ;
  213. ; hwnd                  handle of the control to update
  214. ; hBitmap               a gdi bitmap to associate the static control with
  215. ;
  216. ; return                If the function succeeds, the return value is nonzero
  217.  
  218. SetImage(hwnd, hBitmap)
  219. {
  220.     SendMessage, 0x172, 0x0, hBitmap,, ahk_id %hwnd%
  221.     E := ErrorLevel
  222.     DeleteObject(E)
  223.     return E
  224. }
  225.  
  226. ;#####################################################################################
  227.  
  228. ; Function              SetSysColorToControl
  229. ; Description           Sets a solid colour to a control
  230. ;
  231. ; hwnd                  handle of the control to update
  232. ; SysColor              A system colour to set to the control
  233. ;
  234. ; return                If the function succeeds, the return value is zero
  235. ;
  236. ; notes                 A control must have the 0xE style set to it so it is recognised as a bitmap
  237. ;                       By default SysColor=15 is used which is COLOR_3DFACE. This is the standard background for a control
  238. ;
  239. ; COLOR_3DDKSHADOW              = 21
  240. ; COLOR_3DFACE                  = 15
  241. ; COLOR_3DHIGHLIGHT             = 20
  242. ; COLOR_3DHILIGHT               = 20
  243. ; COLOR_3DLIGHT                 = 22
  244. ; COLOR_3DSHADOW                = 16
  245. ; COLOR_ACTIVEBORDER            = 10
  246. ; COLOR_ACTIVECAPTION           = 2
  247. ; COLOR_APPWORKSPACE            = 12
  248. ; COLOR_BACKGROUND              = 1
  249. ; COLOR_BTNFACE                 = 15
  250. ; COLOR_BTNHIGHLIGHT            = 20
  251. ; COLOR_BTNHILIGHT              = 20
  252. ; COLOR_BTNSHADOW               = 16
  253. ; COLOR_BTNTEXT                 = 18
  254. ; COLOR_CAPTIONTEXT             = 9
  255. ; COLOR_DESKTOP                 = 1
  256. ; COLOR_GRADIENTACTIVECAPTION   = 27
  257. ; COLOR_GRADIENTINACTIVECAPTION = 28
  258. ; COLOR_GRAYTEXT                = 17
  259. ; COLOR_HIGHLIGHT               = 13
  260. ; COLOR_HIGHLIGHTTEXT           = 14
  261. ; COLOR_HOTLIGHT                = 26
  262. ; COLOR_INACTIVEBORDER          = 11
  263. ; COLOR_INACTIVECAPTION         = 3
  264. ; COLOR_INACTIVECAPTIONTEXT     = 19
  265. ; COLOR_INFOBK                  = 24
  266. ; COLOR_INFOTEXT                = 23
  267. ; COLOR_MENU                    = 4
  268. ; COLOR_MENUHILIGHT             = 29
  269. ; COLOR_MENUBAR                 = 30
  270. ; COLOR_MENUTEXT                = 7
  271. ; COLOR_SCROLLBAR               = 0
  272. ; COLOR_WINDOW                  = 5
  273. ; COLOR_WINDOWFRAME             = 6
  274. ; COLOR_WINDOWTEXT              = 8
  275.  
  276. SetSysColorToControl(hwnd, SysColor=15)
  277. {
  278.    WinGetPos,,, w, h, ahk_id %hwnd%
  279.    bc := DllCall("GetSysColor", "Int", SysColor, "UInt")
  280.    pBrushClear := Gdip_BrushCreateSolid(0xff000000 | (bc >> 16 | bc & 0xff00 | (bc & 0xff) << 16))
  281.    pBitmap := Gdip_CreateBitmap(w, h), G := Gdip_GraphicsFromImage(pBitmap)
  282.    Gdip_FillRectangle(G, pBrushClear, 0, 0, w, h)
  283.    hBitmap := Gdip_CreateHBITMAPFromBitmap(pBitmap)
  284.    SetImage(hwnd, hBitmap)
  285.    Gdip_DeleteBrush(pBrushClear)
  286.    Gdip_DeleteGraphics(G), Gdip_DisposeImage(pBitmap), DeleteObject(hBitmap)
  287.    return 0
  288. }
  289.  
  290. ;#####################################################################################
  291.  
  292. ; Function              Gdip_BitmapFromScreen
  293. ; Description           Gets a gdi+ bitmap from the screen
  294. ;
  295. ; Screen                0 = All screens
  296. ;                       Any numerical value = Just that screen
  297. ;                       x|y|w|h = Take specific coordinates with a width and height
  298. ; Raster                raster operation code
  299. ;
  300. ; return                If the function succeeds, the return value is a pointer to a gdi+ bitmap
  301. ;                       -1:     one or more of x,y,w,h not passed properly
  302. ;
  303. ; notes                 If no raster operation is specified, then SRCCOPY is used to the returned bitmap
  304.  
  305. Gdip_BitmapFromScreen(Screen=0, Raster="")
  306. {
  307.     if (Screen = 0)
  308.     {
  309.         Sysget, x, 76
  310.         Sysget, y, 77  
  311.         Sysget, w, 78
  312.         Sysget, h, 79
  313.     }
  314.     else if (SubStr(Screen, 1, 5) = "hwnd:")
  315.     {
  316.         Screen := SubStr(Screen, 6)
  317.         if !WinExist( "ahk_id " Screen)
  318.             return -2
  319.         WinGetPos,,, w, h, ahk_id %Screen%
  320.         x := y := 0
  321.         hhdc := GetDCEx(Screen, 3)
  322.     }
  323.     else if (Screen&1 != "")
  324.     {
  325.         Sysget, M, Monitor, %Screen%
  326.         x := MLeft, y := MTop, w := MRight-MLeft, h := MBottom-MTop
  327.     }
  328.     else
  329.     {
  330.         StringSplit, S, Screen, |
  331.         x := S1, y := S2, w := S3, h := S4
  332.     }
  333.  
  334.     if (x = "") || (y = "") || (w = "") || (h = "")
  335.         return -1
  336.  
  337.     chdc := CreateCompatibleDC(), hbm := CreateDIBSection(w, h, chdc), obm := SelectObject(chdc, hbm), hhdc := hhdc ? hhdc : GetDC()
  338.     BitBlt(chdc, 0, 0, w, h, hhdc, x, y, Raster)
  339.     ReleaseDC(hhdc)
  340.    
  341.     pBitmap := Gdip_CreateBitmapFromHBITMAP(hbm)
  342.     SelectObject(chdc, obm), DeleteObject(hbm), DeleteDC(hhdc), DeleteDC(chdc)
  343.     return pBitmap
  344. }
  345.  
  346. ;#####################################################################################
  347.  
  348. ; Function              Gdip_BitmapFromHWND
  349. ; Description           Uses PrintWindow to get a handle to the specified window and return a bitmap from it
  350. ;
  351. ; hwnd                  handle to the window to get a bitmap from
  352. ;
  353. ; return                If the function succeeds, the return value is a pointer to a gdi+ bitmap
  354. ;
  355. ; notes                 Window must not be not minimised in order to get a handle to it's client area
  356.  
  357. Gdip_BitmapFromHWND(hwnd)
  358. {
  359.     WinGetPos,,, Width, Height, ahk_id %hwnd%
  360.     hbm := CreateDIBSection(Width, Height), hdc := CreateCompatibleDC(), obm := SelectObject(hdc, hbm)
  361.     PrintWindow(hwnd, hdc)
  362.     pBitmap := Gdip_CreateBitmapFromHBITMAP(hbm)
  363.     SelectObject(hdc, obm), DeleteObject(hbm), DeleteDC(hdc)
  364.     return pBitmap
  365. }
  366.  
  367. ;#####################################################################################
  368.  
  369. ; Function              CreateRectF
  370. ; Description           Creates a RectF object, containing a the coordinates and dimensions of a rectangle
  371. ;
  372. ; RectF                 Name to call the RectF object
  373. ; x                     x-coordinate of the upper left corner of the rectangle
  374. ; y                     y-coordinate of the upper left corner of the rectangle
  375. ; w                     Width of the rectangle
  376. ; h                     Height of the rectangle
  377. ;
  378. ; return                No return value
  379.  
  380. CreateRectF(ByRef RectF, x, y, w, h)
  381. {
  382.    VarSetCapacity(RectF, 16)
  383.    NumPut(x, RectF, 0, "float"), NumPut(y, RectF, 4, "float"), NumPut(w, RectF, 8, "float"), NumPut(h, RectF, 12, "float")
  384. }
  385.  
  386. ;#####################################################################################
  387.  
  388. ; Function              CreateRect
  389. ; Description           Creates a Rect object, containing a the coordinates and dimensions of a rectangle
  390. ;
  391. ; RectF                 Name to call the RectF object
  392. ; x                     x-coordinate of the upper left corner of the rectangle
  393. ; y                     y-coordinate of the upper left corner of the rectangle
  394. ; w                     Width of the rectangle
  395. ; h                     Height of the rectangle
  396. ;
  397. ; return                No return value
  398.  
  399. CreateRect(ByRef Rect, x, y, w, h)
  400. {
  401.     VarSetCapacity(Rect, 16)
  402.     NumPut(x, Rect, 0, "uint"), NumPut(y, Rect, 4, "uint"), NumPut(w, Rect, 8, "uint"), NumPut(h, Rect, 12, "uint")
  403. }
  404. ;#####################################################################################
  405.  
  406. ; Function              CreateSizeF
  407. ; Description           Creates a SizeF object, containing an 2 values
  408. ;
  409. ; SizeF                 Name to call the SizeF object
  410. ; w                     w-value for the SizeF object
  411. ; h                     h-value for the SizeF object
  412. ;
  413. ; return                No Return value
  414.  
  415. CreateSizeF(ByRef SizeF, w, h)
  416. {
  417.    VarSetCapacity(SizeF, 8)
  418.    NumPut(w, SizeF, 0, "float"), NumPut(h, SizeF, 4, "float")    
  419. }
  420. ;#####################################################################################
  421.  
  422. ; Function              CreatePointF
  423. ; Description           Creates a SizeF object, containing an 2 values
  424. ;
  425. ; SizeF                 Name to call the SizeF object
  426. ; w                     w-value for the SizeF object
  427. ; h                     h-value for the SizeF object
  428. ;
  429. ; return                No Return value
  430.  
  431. CreatePointF(ByRef PointF, x, y)
  432. {
  433.    VarSetCapacity(PointF, 8)
  434.    NumPut(x, PointF, 0, "float"), NumPut(y, PointF, 4, "float")    
  435. }
  436. ;#####################################################################################
  437.  
  438. ; Function              CreateDIBSection
  439. ; Description           The CreateDIBSection function creates a DIB (Device Independent Bitmap) that applications can write to directly
  440. ;
  441. ; w                     width of the bitmap to create
  442. ; h                     height of the bitmap to create
  443. ; hdc                   a handle to the device context to use the palette from
  444. ; bpp                   bits per pixel (32 = ARGB)
  445. ; ppvBits               A pointer to a variable that receives a pointer to the location of the DIB bit values
  446. ;
  447. ; return                returns a DIB. A gdi bitmap
  448. ;
  449. ; notes                 ppvBits will receive the location of the pixels in the DIB
  450.  
  451. CreateDIBSection(w, h, hdc="", bpp=32, ByRef ppvBits=0)
  452. {
  453.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  454.    
  455.     hdc2 := hdc ? hdc : GetDC()
  456.     VarSetCapacity(bi, 40, 0)
  457.    
  458.     NumPut(w, bi, 4, "uint")
  459.     , NumPut(h, bi, 8, "uint")
  460.     , NumPut(40, bi, 0, "uint")
  461.     , NumPut(1, bi, 12, "ushort")
  462.     , NumPut(0, bi, 16, "uInt")
  463.     , NumPut(bpp, bi, 14, "ushort")
  464.    
  465.     hbm := DllCall("CreateDIBSection"
  466.                     , Ptr, hdc2
  467.                     , Ptr, &bi
  468.                     , "uint", 0
  469.                     , A_PtrSize ? "UPtr*" : "uint*", ppvBits
  470.                     , Ptr, 0
  471.                     , "uint", 0, Ptr)
  472.  
  473.     if !hdc
  474.         ReleaseDC(hdc2)
  475.     return hbm
  476. }
  477.  
  478. ;#####################################################################################
  479.  
  480. ; Function              PrintWindow
  481. ; Description           The PrintWindow function copies a visual window into the specified device context (DC), typically a printer DC
  482. ;
  483. ; hwnd                  A handle to the window that will be copied
  484. ; hdc                   A handle to the device context
  485. ; Flags                 Drawing options
  486. ;
  487. ; return                If the function succeeds, it returns a nonzero value
  488. ;
  489. ; PW_CLIENTONLY         = 1
  490.  
  491. PrintWindow(hwnd, hdc, Flags=0)
  492. {
  493.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  494.    
  495.     return DllCall("PrintWindow", Ptr, hwnd, Ptr, hdc, "uint", Flags)
  496. }
  497.  
  498. ;#####################################################################################
  499.  
  500. ; Function              DestroyIcon
  501. ; Description           Destroys an icon and frees any memory the icon occupied
  502. ;
  503. ; hIcon                 Handle to the icon to be destroyed. The icon must not be in use
  504. ;
  505. ; return                If the function succeeds, the return value is nonzero
  506.  
  507. DestroyIcon(hIcon)
  508. {
  509.     return DllCall("DestroyIcon", A_PtrSize ? "UPtr" : "UInt", hIcon)
  510. }
  511.  
  512. ;#####################################################################################
  513.  
  514. PaintDesktop(hdc)
  515. {
  516.     return DllCall("PaintDesktop", A_PtrSize ? "UPtr" : "UInt", hdc)
  517. }
  518.  
  519. ;#####################################################################################
  520.  
  521. CreateCompatibleBitmap(hdc, w, h)
  522. {
  523.     return DllCall("gdi32\CreateCompatibleBitmap", A_PtrSize ? "UPtr" : "UInt", hdc, "int", w, "int", h)
  524. }
  525.  
  526. ;#####################################################################################
  527.  
  528. ; Function              CreateCompatibleDC
  529. ; Description           This function creates a memory device context (DC) compatible with the specified device
  530. ;
  531. ; hdc                   Handle to an existing device context                   
  532. ;
  533. ; return                returns the handle to a device context or 0 on failure
  534. ;
  535. ; notes                 If this handle is 0 (by default), the function creates a memory device context compatible with the application's current screen
  536.  
  537. CreateCompatibleDC(hdc=0)
  538. {
  539.    return DllCall("CreateCompatibleDC", A_PtrSize ? "UPtr" : "UInt", hdc)
  540. }
  541.  
  542. ;#####################################################################################
  543.  
  544. ; Function              SelectObject
  545. ; Description           The SelectObject function selects an object into the specified device context (DC). The new object replaces the previous object of the same type
  546. ;
  547. ; hdc                   Handle to a DC
  548. ; hgdiobj               A handle to the object to be selected into the DC
  549. ;
  550. ; return                If the selected object is not a region and the function succeeds, the return value is a handle to the object being replaced
  551. ;
  552. ; notes                 The specified object must have been created by using one of the following functions
  553. ;                       Bitmap - CreateBitmap, CreateBitmapIndirect, CreateCompatibleBitmap, CreateDIBitmap, CreateDIBSection (A single bitmap cannot be selected into more than one DC at the same time)
  554. ;                       Brush - CreateBrushIndirect, CreateDIBPatternBrush, CreateDIBPatternBrushPt, CreateHatchBrush, CreatePatternBrush, CreateSolidBrush
  555. ;                       Font - CreateFont, CreateFontIndirect
  556. ;                       Pen - CreatePen, CreatePenIndirect
  557. ;                       Region - CombineRgn, CreateEllipticRgn, CreateEllipticRgnIndirect, CreatePolygonRgn, CreateRectRgn, CreateRectRgnIndirect
  558. ;
  559. ; notes                 If the selected object is a region and the function succeeds, the return value is one of the following value
  560. ;
  561. ; SIMPLEREGION          = 2 Region consists of a single rectangle
  562. ; COMPLEXREGION         = 3 Region consists of more than one rectangle
  563. ; NULLREGION            = 1 Region is empty
  564.  
  565. SelectObject(hdc, hgdiobj)
  566. {
  567.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  568.    
  569.     return DllCall("SelectObject", Ptr, hdc, Ptr, hgdiobj)
  570. }
  571.  
  572. ;#####################################################################################
  573.  
  574. ; Function              DeleteObject
  575. ; Description           This function deletes a logical pen, brush, font, bitmap, region, or palette, freeing all system resources associated with the object
  576. ;                       After the object is deleted, the specified handle is no longer valid
  577. ;
  578. ; hObject               Handle to a logical pen, brush, font, bitmap, region, or palette to delete
  579. ;
  580. ; return                Nonzero indicates success. Zero indicates that the specified handle is not valid or that the handle is currently selected into a device context
  581.  
  582. DeleteObject(hObject)
  583. {
  584.    return DllCall("DeleteObject", A_PtrSize ? "UPtr" : "UInt", hObject)
  585. }
  586.  
  587. ;#####################################################################################
  588.  
  589. ; Function              GetDC
  590. ; Description           This function retrieves a handle to a display device context (DC) for the client area of the specified window.
  591. ;                       The display device context can be used in subsequent graphics display interface (GDI) functions to draw in the client area of the window.
  592. ;
  593. ; hwnd                  Handle to the window whose device context is to be retrieved. If this value is NULL, GetDC retrieves the device context for the entire screen                  
  594. ;
  595. ; return                The handle the device context for the specified window's client area indicates success. NULL indicates failure
  596.  
  597. GetDC(hwnd=0)
  598. {
  599.     return DllCall("GetDC", A_PtrSize ? "UPtr" : "UInt", hwnd)
  600. }
  601.  
  602. ;#####################################################################################
  603.  
  604. ; DCX_CACHE = 0x2
  605. ; DCX_CLIPCHILDREN = 0x8
  606. ; DCX_CLIPSIBLINGS = 0x10
  607. ; DCX_EXCLUDERGN = 0x40
  608. ; DCX_EXCLUDEUPDATE = 0x100
  609. ; DCX_INTERSECTRGN = 0x80
  610. ; DCX_INTERSECTUPDATE = 0x200
  611. ; DCX_LOCKWINDOWUPDATE = 0x400
  612. ; DCX_NORECOMPUTE = 0x100000
  613. ; DCX_NORESETATTRS = 0x4
  614. ; DCX_PARENTCLIP = 0x20
  615. ; DCX_VALIDATE = 0x200000
  616. ; DCX_WINDOW = 0x1
  617.  
  618. GetDCEx(hwnd, flags=0, hrgnClip=0)
  619. {
  620.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  621.    
  622.     return DllCall("GetDCEx", Ptr, hwnd, Ptr, hrgnClip, "int", flags)
  623. }
  624.  
  625. ;#####################################################################################
  626.  
  627. ; Function              ReleaseDC
  628. ; Description           This function releases a device context (DC), freeing it for use by other applications. The effect of ReleaseDC depends on the type of device context
  629. ;
  630. ; hdc                   Handle to the device context to be released
  631. ; hwnd                  Handle to the window whose device context is to be released
  632. ;
  633. ; return                1 = released
  634. ;                       0 = not released
  635. ;
  636. ; notes                 The application must call the ReleaseDC function for each call to the GetWindowDC function and for each call to the GetDC function that retrieves a common device context
  637. ;                       An application cannot use the ReleaseDC function to release a device context that was created by calling the CreateDC function; instead, it must use the DeleteDC function.
  638.  
  639. ReleaseDC(hdc, hwnd=0)
  640. {
  641.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  642.    
  643.     return DllCall("ReleaseDC", Ptr, hwnd, Ptr, hdc)
  644. }
  645.  
  646. ;#####################################################################################
  647.  
  648. ; Function              DeleteDC
  649. ; Description           The DeleteDC function deletes the specified device context (DC)
  650. ;
  651. ; hdc                   A handle to the device context
  652. ;
  653. ; return                If the function succeeds, the return value is nonzero
  654. ;
  655. ; notes                 An application must not delete a DC whose handle was obtained by calling the GetDC function. Instead, it must call the ReleaseDC function to free the DC
  656.  
  657. DeleteDC(hdc)
  658. {
  659.    return DllCall("DeleteDC", A_PtrSize ? "UPtr" : "UInt", hdc)
  660. }
  661. ;#####################################################################################
  662.  
  663. ; Function              Gdip_LibraryVersion
  664. ; Description           Get the current library version
  665. ;
  666. ; return                the library version
  667. ;
  668. ; notes                 This is useful for non compiled programs to ensure that a person doesn't run an old version when testing your scripts
  669.  
  670. Gdip_LibraryVersion()
  671. {
  672.     return 1.45
  673. }
  674.  
  675. ;#####################################################################################
  676.  
  677. ; Function              Gdip_LibrarySubVersion
  678. ; Description           Get the current library sub version
  679. ;
  680. ; return                the library sub version
  681. ;
  682. ; notes                 This is the sub-version currently maintained by Rseding91
  683. Gdip_LibrarySubVersion()
  684. {
  685.     return 1.47
  686. }
  687.  
  688. ;#####################################################################################
  689.  
  690. ; Function:             Gdip_BitmapFromBRA
  691. ; Description:          Gets a pointer to a gdi+ bitmap from a BRA file
  692. ;
  693. ; BRAFromMemIn          The variable for a BRA file read to memory
  694. ; File                  The name of the file, or its number that you would like (This depends on alternate parameter)
  695. ; Alternate             Changes whether the File parameter is the file name or its number
  696. ;
  697. ; return                If the function succeeds, the return value is a pointer to a gdi+ bitmap
  698. ;                       -1 = The BRA variable is empty
  699. ;                       -2 = The BRA has an incorrect header
  700. ;                       -3 = The BRA has information missing
  701. ;                       -4 = Could not find file inside the BRA
  702.  
  703. Gdip_BitmapFromBRA(ByRef BRAFromMemIn, File, Alternate=0)
  704. {
  705.     Static FName = "ObjRelease"
  706.    
  707.     if !BRAFromMemIn
  708.         return -1
  709.     Loop, Parse, BRAFromMemIn, `n
  710.     {
  711.         if (A_Index = 1)
  712.         {
  713.             StringSplit, Header, A_LoopField, |
  714.             if (Header0 != 4 || Header2 != "BRA!")
  715.                 return -2
  716.         }
  717.         else if (A_Index = 2)
  718.         {
  719.             StringSplit, Info, A_LoopField, |
  720.             if (Info0 != 3)
  721.                 return -3
  722.         }
  723.         else
  724.             break
  725.     }
  726.     if !Alternate
  727.         StringReplace, File, File, \, \\, All
  728.     RegExMatch(BRAFromMemIn, "mi`n)^" (Alternate ? File "\|.+?\|(\d+)\|(\d+)" : "\d+\|" File "\|(\d+)\|(\d+)") "$", FileInfo)
  729.     if !FileInfo
  730.         return -4
  731.    
  732.     hData := DllCall("GlobalAlloc", "uint", 2, Ptr, FileInfo2, Ptr)
  733.     pData := DllCall("GlobalLock", Ptr, hData, Ptr)
  734.     DllCall("RtlMoveMemory", Ptr, pData, Ptr, &BRAFromMemIn+Info2+FileInfo1, Ptr, FileInfo2)
  735.     DllCall("GlobalUnlock", Ptr, hData)
  736.     DllCall("ole32\CreateStreamOnHGlobal", Ptr, hData, "int", 1, A_PtrSize ? "UPtr*" : "UInt*", pStream)
  737.     DllCall("gdiplus\GdipCreateBitmapFromStream", Ptr, pStream, A_PtrSize ? "UPtr*" : "UInt*", pBitmap)
  738.     If (A_PtrSize)
  739.         %FName%(pStream)
  740.     Else
  741.         DllCall(NumGet(NumGet(1*pStream)+8), "uint", pStream)
  742.     return pBitmap
  743. }
  744.  
  745. ;#####################################################################################
  746.  
  747. ; Function              Gdip_DrawRectangle
  748. ; Description           This function uses a pen to draw the outline of a rectangle into the Graphics of a bitmap
  749. ;
  750. ; pGraphics             Pointer to the Graphics of a bitmap
  751. ; pPen                  Pointer to a pen
  752. ; x                     x-coordinate of the top left of the rectangle
  753. ; y                     y-coordinate of the top left of the rectangle
  754. ; w                     width of the rectanlge
  755. ; h                     height of the rectangle
  756. ;
  757. ; return                status enumeration. 0 = success
  758. ;
  759. ; notes                 as all coordinates are taken from the top left of each pixel, then the entire width/height should be specified as subtracting the pen width
  760.  
  761. Gdip_DrawRectangle(pGraphics, pPen, x, y, w, h)
  762. {
  763.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  764.    
  765.     return DllCall("gdiplus\GdipDrawRectangle", Ptr, pGraphics, Ptr, pPen, "float", x, "float", y, "float", w, "float", h)
  766. }
  767.  
  768. ;#####################################################################################
  769.  
  770. ; Function              Gdip_DrawRoundedRectangle
  771. ; Description           This function uses a pen to draw the outline of a rounded rectangle into the Graphics of a bitmap
  772. ;
  773. ; pGraphics             Pointer to the Graphics of a bitmap
  774. ; pPen                  Pointer to a pen
  775. ; x                     x-coordinate of the top left of the rounded rectangle
  776. ; y                     y-coordinate of the top left of the rounded rectangle
  777. ; w                     width of the rectanlge
  778. ; h                     height of the rectangle
  779. ; r                     radius of the rounded corners
  780. ;
  781. ; return                status enumeration. 0 = success
  782. ;
  783. ; notes                 as all coordinates are taken from the top left of each pixel, then the entire width/height should be specified as subtracting the pen width
  784.  
  785. Gdip_DrawRoundedRectangle(pGraphics, pPen, x, y, w, h, r)
  786. {
  787.     Gdip_SetClipRect(pGraphics, x-r, y-r, 2*r, 2*r, 4)
  788.     Gdip_SetClipRect(pGraphics, x+w-r, y-r, 2*r, 2*r, 4)
  789.     Gdip_SetClipRect(pGraphics, x-r, y+h-r, 2*r, 2*r, 4)
  790.     Gdip_SetClipRect(pGraphics, x+w-r, y+h-r, 2*r, 2*r, 4)
  791.     E := Gdip_DrawRectangle(pGraphics, pPen, x, y, w, h)
  792.     Gdip_ResetClip(pGraphics)
  793.     Gdip_SetClipRect(pGraphics, x-(2*r), y+r, w+(4*r), h-(2*r), 4)
  794.     Gdip_SetClipRect(pGraphics, x+r, y-(2*r), w-(2*r), h+(4*r), 4)
  795.     Gdip_DrawEllipse(pGraphics, pPen, x, y, 2*r, 2*r)
  796.     Gdip_DrawEllipse(pGraphics, pPen, x+w-(2*r), y, 2*r, 2*r)
  797.     Gdip_DrawEllipse(pGraphics, pPen, x, y+h-(2*r), 2*r, 2*r)
  798.     Gdip_DrawEllipse(pGraphics, pPen, x+w-(2*r), y+h-(2*r), 2*r, 2*r)
  799.     Gdip_ResetClip(pGraphics)
  800.     return E
  801. }
  802.  
  803. ;#####################################################################################
  804.  
  805. ; Function              Gdip_DrawEllipse
  806. ; Description           This function uses a pen to draw the outline of an ellipse into the Graphics of a bitmap
  807. ;
  808. ; pGraphics             Pointer to the Graphics of a bitmap
  809. ; pPen                  Pointer to a pen
  810. ; x                     x-coordinate of the top left of the rectangle the ellipse will be drawn into
  811. ; y                     y-coordinate of the top left of the rectangle the ellipse will be drawn into
  812. ; w                     width of the ellipse
  813. ; h                     height of the ellipse
  814. ;
  815. ; return                status enumeration. 0 = success
  816. ;
  817. ; notes                 as all coordinates are taken from the top left of each pixel, then the entire width/height should be specified as subtracting the pen width
  818.  
  819. Gdip_DrawEllipse(pGraphics, pPen, x, y, w, h)
  820. {
  821.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  822.    
  823.     return DllCall("gdiplus\GdipDrawEllipse", Ptr, pGraphics, Ptr, pPen, "float", x, "float", y, "float", w, "float", h)
  824. }
  825.  
  826. ;#####################################################################################
  827.  
  828. ; Function              Gdip_DrawBezier
  829. ; Description           This function uses a pen to draw the outline of a bezier (a weighted curve) into the Graphics of a bitmap
  830. ;
  831. ; pGraphics             Pointer to the Graphics of a bitmap
  832. ; pPen                  Pointer to a pen
  833. ; x1                    x-coordinate of the start of the bezier
  834. ; y1                    y-coordinate of the start of the bezier
  835. ; x2                    x-coordinate of the first arc of the bezier
  836. ; y2                    y-coordinate of the first arc of the bezier
  837. ; x3                    x-coordinate of the second arc of the bezier
  838. ; y3                    y-coordinate of the second arc of the bezier
  839. ; x4                    x-coordinate of the end of the bezier
  840. ; y4                    y-coordinate of the end of the bezier
  841. ;
  842. ; return                status enumeration. 0 = success
  843. ;
  844. ; notes                 as all coordinates are taken from the top left of each pixel, then the entire width/height should be specified as subtracting the pen width
  845.  
  846. Gdip_DrawBezier(pGraphics, pPen, x1, y1, x2, y2, x3, y3, x4, y4)
  847. {
  848.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  849.    
  850.     return DllCall("gdiplus\GdipDrawBezier"
  851.                     , Ptr, pgraphics
  852.                     , Ptr, pPen
  853.                     , "float", x1
  854.                     , "float", y1
  855.                     , "float", x2
  856.                     , "float", y2
  857.                     , "float", x3
  858.                     , "float", y3
  859.                     , "float", x4
  860.                     , "float", y4)
  861. }
  862.  
  863. ;#####################################################################################
  864.  
  865. ; Function              Gdip_DrawArc
  866. ; Description           This function uses a pen to draw the outline of an arc into the Graphics of a bitmap
  867. ;
  868. ; pGraphics             Pointer to the Graphics of a bitmap
  869. ; pPen                  Pointer to a pen
  870. ; x                     x-coordinate of the start of the arc
  871. ; y                     y-coordinate of the start of the arc
  872. ; w                     width of the arc
  873. ; h                     height of the arc
  874. ; StartAngle            specifies the angle between the x-axis and the starting point of the arc
  875. ; SweepAngle            specifies the angle between the starting and ending points of the arc
  876. ;
  877. ; return                status enumeration. 0 = success
  878. ;
  879. ; notes                 as all coordinates are taken from the top left of each pixel, then the entire width/height should be specified as subtracting the pen width
  880.  
  881. Gdip_DrawArc(pGraphics, pPen, x, y, w, h, StartAngle, SweepAngle)
  882. {
  883.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  884.    
  885.     return DllCall("gdiplus\GdipDrawArc"
  886.                     , Ptr, pGraphics
  887.                     , Ptr, pPen
  888.                     , "float", x
  889.                     , "float", y
  890.                     , "float", w
  891.                     , "float", h
  892.                     , "float", StartAngle
  893.                     , "float", SweepAngle)
  894. }
  895.  
  896. ;#####################################################################################
  897.  
  898. ; Function              Gdip_DrawPie
  899. ; Description           This function uses a pen to draw the outline of a pie into the Graphics of a bitmap
  900. ;
  901. ; pGraphics             Pointer to the Graphics of a bitmap
  902. ; pPen                  Pointer to a pen
  903. ; x                     x-coordinate of the start of the pie
  904. ; y                     y-coordinate of the start of the pie
  905. ; w                     width of the pie
  906. ; h                     height of the pie
  907. ; StartAngle            specifies the angle between the x-axis and the starting point of the pie
  908. ; SweepAngle            specifies the angle between the starting and ending points of the pie
  909. ;
  910. ; return                status enumeration. 0 = success
  911. ;
  912. ; notes                 as all coordinates are taken from the top left of each pixel, then the entire width/height should be specified as subtracting the pen width
  913.  
  914. Gdip_DrawPie(pGraphics, pPen, x, y, w, h, StartAngle, SweepAngle)
  915. {
  916.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  917.    
  918.     return DllCall("gdiplus\GdipDrawPie", Ptr, pGraphics, Ptr, pPen, "float", x, "float", y, "float", w, "float", h, "float", StartAngle, "float", SweepAngle)
  919. }
  920.  
  921. ;#####################################################################################
  922.  
  923. ; Function              Gdip_DrawLine
  924. ; Description           This function uses a pen to draw a line into the Graphics of a bitmap
  925. ;
  926. ; pGraphics             Pointer to the Graphics of a bitmap
  927. ; pPen                  Pointer to a pen
  928. ; x1                    x-coordinate of the start of the line
  929. ; y1                    y-coordinate of the start of the line
  930. ; x2                    x-coordinate of the end of the line
  931. ; y2                    y-coordinate of the end of the line
  932. ;
  933. ; return                status enumeration. 0 = success    
  934.  
  935. Gdip_DrawLine(pGraphics, pPen, x1, y1, x2, y2)
  936. {
  937.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  938.    
  939.     return DllCall("gdiplus\GdipDrawLine"
  940.                     , Ptr, pGraphics
  941.                     , Ptr, pPen
  942.                     , "float", x1
  943.                     , "float", y1
  944.                     , "float", x2
  945.                     , "float", y2)
  946. }
  947.  
  948. ;#####################################################################################
  949.  
  950. ; Function              Gdip_DrawLines
  951. ; Description           This function uses a pen to draw a series of joined lines into the Graphics of a bitmap
  952. ;
  953. ; pGraphics             Pointer to the Graphics of a bitmap
  954. ; pPen                  Pointer to a pen
  955. ; Points                the coordinates of all the points passed as x1,y1|x2,y2|x3,y3.....
  956. ;
  957. ; return                status enumeration. 0 = success            
  958.  
  959. Gdip_DrawLines(pGraphics, pPen, Points)
  960. {
  961.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  962.     StringSplit, Points, Points, |
  963.     VarSetCapacity(PointF, 8*Points0)  
  964.     Loop, %Points0%
  965.     {
  966.         StringSplit, Coord, Points%A_Index%, `,
  967.         NumPut(Coord1, PointF, 8*(A_Index-1), "float"), NumPut(Coord2, PointF, (8*(A_Index-1))+4, "float")
  968.     }
  969.     return DllCall("gdiplus\GdipDrawLines", Ptr, pGraphics, Ptr, pPen, Ptr, &PointF, "int", Points0)
  970. }
  971.  
  972. ;#####################################################################################
  973.  
  974. ; Function              Gdip_FillRectangle
  975. ; Description           This function uses a brush to fill a rectangle in the Graphics of a bitmap
  976. ;
  977. ; pGraphics             Pointer to the Graphics of a bitmap
  978. ; pBrush                Pointer to a brush
  979. ; x                     x-coordinate of the top left of the rectangle
  980. ; y                     y-coordinate of the top left of the rectangle
  981. ; w                     width of the rectanlge
  982. ; h                     height of the rectangle
  983. ;
  984. ; return                status enumeration. 0 = success
  985.  
  986. Gdip_FillRectangle(pGraphics, pBrush, x, y, w, h)
  987. {
  988.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  989.    
  990.     return DllCall("gdiplus\GdipFillRectangle"
  991.                     , Ptr, pGraphics
  992.                     , Ptr, pBrush
  993.                     , "float", x
  994.                     , "float", y
  995.                     , "float", w
  996.                     , "float", h)
  997. }
  998.  
  999. ;#####################################################################################
  1000.  
  1001. ; Function              Gdip_FillRoundedRectangle
  1002. ; Description           This function uses a brush to fill a rounded rectangle in the Graphics of a bitmap
  1003. ;
  1004. ; pGraphics             Pointer to the Graphics of a bitmap
  1005. ; pBrush                Pointer to a brush
  1006. ; x                     x-coordinate of the top left of the rounded rectangle
  1007. ; y                     y-coordinate of the top left of the rounded rectangle
  1008. ; w                     width of the rectanlge
  1009. ; h                     height of the rectangle
  1010. ; r                     radius of the rounded corners
  1011. ;
  1012. ; return                status enumeration. 0 = success
  1013.  
  1014. Gdip_FillRoundedRectangle(pGraphics, pBrush, x, y, w, h, r)
  1015. {
  1016.     Region := Gdip_GetClipRegion(pGraphics)
  1017.     Gdip_SetClipRect(pGraphics, x-r, y-r, 2*r, 2*r, 4)
  1018.     Gdip_SetClipRect(pGraphics, x+w-r, y-r, 2*r, 2*r, 4)
  1019.     Gdip_SetClipRect(pGraphics, x-r, y+h-r, 2*r, 2*r, 4)
  1020.     Gdip_SetClipRect(pGraphics, x+w-r, y+h-r, 2*r, 2*r, 4)
  1021.     E := Gdip_FillRectangle(pGraphics, pBrush, x, y, w, h)
  1022.     Gdip_SetClipRegion(pGraphics, Region, 0)
  1023.     Gdip_SetClipRect(pGraphics, x-(2*r), y+r, w+(4*r), h-(2*r), 4)
  1024.     Gdip_SetClipRect(pGraphics, x+r, y-(2*r), w-(2*r), h+(4*r), 4)
  1025.     Gdip_FillEllipse(pGraphics, pBrush, x, y, 2*r, 2*r)
  1026.     Gdip_FillEllipse(pGraphics, pBrush, x+w-(2*r), y, 2*r, 2*r)
  1027.     Gdip_FillEllipse(pGraphics, pBrush, x, y+h-(2*r), 2*r, 2*r)
  1028.     Gdip_FillEllipse(pGraphics, pBrush, x+w-(2*r), y+h-(2*r), 2*r, 2*r)
  1029.     Gdip_SetClipRegion(pGraphics, Region, 0)
  1030.     Gdip_DeleteRegion(Region)
  1031.     return E
  1032. }
  1033.  
  1034. ;#####################################################################################
  1035.  
  1036. ; Function              Gdip_FillPolygon
  1037. ; Description           This function uses a brush to fill a polygon in the Graphics of a bitmap
  1038. ;
  1039. ; pGraphics             Pointer to the Graphics of a bitmap
  1040. ; pBrush                Pointer to a brush
  1041. ; Points                the coordinates of all the points passed as x1,y1|x2,y2|x3,y3.....
  1042. ;
  1043. ; return                status enumeration. 0 = success
  1044. ;
  1045. ; notes                 Alternate will fill the polygon as a whole, wheras winding will fill each new "segment"
  1046. ; Alternate             = 0
  1047. ; Winding               = 1
  1048.  
  1049. Gdip_FillPolygon(pGraphics, pBrush, Points, FillMode=0)
  1050. {
  1051.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  1052.    
  1053.     StringSplit, Points, Points, |
  1054.     VarSetCapacity(PointF, 8*Points0)  
  1055.     Loop, %Points0%
  1056.     {
  1057.         StringSplit, Coord, Points%A_Index%, `,
  1058.         NumPut(Coord1, PointF, 8*(A_Index-1), "float"), NumPut(Coord2, PointF, (8*(A_Index-1))+4, "float")
  1059.     }  
  1060.     return DllCall("gdiplus\GdipFillPolygon", Ptr, pGraphics, Ptr, pBrush, Ptr, &PointF, "int", Points0, "int", FillMode)
  1061. }
  1062.  
  1063. ;#####################################################################################
  1064.  
  1065. ; Function              Gdip_FillPie
  1066. ; Description           This function uses a brush to fill a pie in the Graphics of a bitmap
  1067. ;
  1068. ; pGraphics             Pointer to the Graphics of a bitmap
  1069. ; pBrush                Pointer to a brush
  1070. ; x                     x-coordinate of the top left of the pie
  1071. ; y                     y-coordinate of the top left of the pie
  1072. ; w                     width of the pie
  1073. ; h                     height of the pie
  1074. ; StartAngle            specifies the angle between the x-axis and the starting point of the pie
  1075. ; SweepAngle            specifies the angle between the starting and ending points of the pie
  1076. ;
  1077. ; return                status enumeration. 0 = success
  1078.  
  1079. Gdip_FillPie(pGraphics, pBrush, x, y, w, h, StartAngle, SweepAngle)
  1080. {
  1081.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  1082.    
  1083.     return DllCall("gdiplus\GdipFillPie"
  1084.                     , Ptr, pGraphics
  1085.                     , Ptr, pBrush
  1086.                     , "float", x
  1087.                     , "float", y
  1088.                     , "float", w
  1089.                     , "float", h
  1090.                     , "float", StartAngle
  1091.                     , "float", SweepAngle)
  1092. }
  1093.  
  1094. ;#####################################################################################
  1095.  
  1096. ; Function              Gdip_FillEllipse
  1097. ; Description           This function uses a brush to fill an ellipse in the Graphics of a bitmap
  1098. ;
  1099. ; pGraphics             Pointer to the Graphics of a bitmap
  1100. ; pBrush                Pointer to a brush
  1101. ; x                     x-coordinate of the top left of the ellipse
  1102. ; y                     y-coordinate of the top left of the ellipse
  1103. ; w                     width of the ellipse
  1104. ; h                     height of the ellipse
  1105. ;
  1106. ; return                status enumeration. 0 = success
  1107.  
  1108. Gdip_FillEllipse(pGraphics, pBrush, x, y, w, h)
  1109. {
  1110.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  1111.    
  1112.     return DllCall("gdiplus\GdipFillEllipse", Ptr, pGraphics, Ptr, pBrush, "float", x, "float", y, "float", w, "float", h)
  1113. }
  1114.  
  1115. ;#####################################################################################
  1116.  
  1117. ; Function              Gdip_FillRegion
  1118. ; Description           This function uses a brush to fill a region in the Graphics of a bitmap
  1119. ;
  1120. ; pGraphics             Pointer to the Graphics of a bitmap
  1121. ; pBrush                Pointer to a brush
  1122. ; Region                Pointer to a Region
  1123. ;
  1124. ; return                status enumeration. 0 = success
  1125. ;
  1126. ; notes                 You can create a region Gdip_CreateRegion() and then add to this
  1127.  
  1128. Gdip_FillRegion(pGraphics, pBrush, Region)
  1129. {
  1130.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  1131.    
  1132.     return DllCall("gdiplus\GdipFillRegion", Ptr, pGraphics, Ptr, pBrush, Ptr, Region)
  1133. }
  1134.  
  1135. ;#####################################################################################
  1136.  
  1137. ; Function              Gdip_FillPath
  1138. ; Description           This function uses a brush to fill a path in the Graphics of a bitmap
  1139. ;
  1140. ; pGraphics             Pointer to the Graphics of a bitmap
  1141. ; pBrush                Pointer to a brush
  1142. ; Region                Pointer to a Path
  1143. ;
  1144. ; return                status enumeration. 0 = success
  1145.  
  1146. Gdip_FillPath(pGraphics, pBrush, Path)
  1147. {
  1148.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  1149.    
  1150.     return DllCall("gdiplus\GdipFillPath", Ptr, pGraphics, Ptr, pBrush, Ptr, Path)
  1151. }
  1152.  
  1153. ;#####################################################################################
  1154.  
  1155. ; Function              Gdip_DrawImagePointsRect
  1156. ; Description           This function draws a bitmap into the Graphics of another bitmap and skews it
  1157. ;
  1158. ; pGraphics             Pointer to the Graphics of a bitmap
  1159. ; pBitmap               Pointer to a bitmap to be drawn
  1160. ; Points                Points passed as x1,y1|x2,y2|x3,y3 (3 points: top left, top right, bottom left) describing the drawing of the bitmap
  1161. ; sx                    x-coordinate of source upper-left corner
  1162. ; sy                    y-coordinate of source upper-left corner
  1163. ; sw                    width of source rectangle
  1164. ; sh                    height of source rectangle
  1165. ; Matrix                a matrix used to alter image attributes when drawing
  1166. ;
  1167. ; return                status enumeration. 0 = success
  1168. ;
  1169. ; notes                 if sx,sy,sw,sh are missed then the entire source bitmap will be used
  1170. ;                       Matrix can be omitted to just draw with no alteration to ARGB
  1171. ;                       Matrix may be passed as a digit from 0 - 1 to change just transparency
  1172. ;                       Matrix can be passed as a matrix with any delimiter
  1173.  
  1174. Gdip_DrawImagePointsRect(pGraphics, pBitmap, Points, sx="", sy="", sw="", sh="", Matrix=1)
  1175. {
  1176.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  1177.    
  1178.     StringSplit, Points, Points, |
  1179.     VarSetCapacity(PointF, 8*Points0)  
  1180.     Loop, %Points0%
  1181.     {
  1182.         StringSplit, Coord, Points%A_Index%, `,
  1183.         NumPut(Coord1, PointF, 8*(A_Index-1), "float"), NumPut(Coord2, PointF, (8*(A_Index-1))+4, "float")
  1184.     }
  1185.  
  1186.     if (Matrix&1 = "")
  1187.         ImageAttr := Gdip_SetImageAttributesColorMatrix(Matrix)
  1188.     else if (Matrix != 1)
  1189.         ImageAttr := Gdip_SetImageAttributesColorMatrix("1|0|0|0|0|0|1|0|0|0|0|0|1|0|0|0|0|0|" Matrix "|0|0|0|0|0|1")
  1190.        
  1191.     if (sx = "" && sy = "" && sw = "" && sh = "")
  1192.     {
  1193.         sx := 0, sy := 0
  1194.         sw := Gdip_GetImageWidth(pBitmap)
  1195.         sh := Gdip_GetImageHeight(pBitmap)
  1196.     }
  1197.  
  1198.     E := DllCall("gdiplus\GdipDrawImagePointsRect"
  1199.                 , Ptr, pGraphics
  1200.                 , Ptr, pBitmap
  1201.                 , Ptr, &PointF
  1202.                 , "int", Points0
  1203.                 , "float", sx
  1204.                 , "float", sy
  1205.                 , "float", sw
  1206.                 , "float", sh
  1207.                 , "int", 2
  1208.                 , Ptr, ImageAttr
  1209.                 , Ptr, 0
  1210.                 , Ptr, 0)
  1211.     if ImageAttr
  1212.         Gdip_DisposeImageAttributes(ImageAttr)
  1213.     return E
  1214. }
  1215.  
  1216. ;#####################################################################################
  1217.  
  1218. ; Function              Gdip_DrawImage
  1219. ; Description           This function draws a bitmap into the Graphics of another bitmap
  1220. ;
  1221. ; pGraphics             Pointer to the Graphics of a bitmap
  1222. ; pBitmap               Pointer to a bitmap to be drawn
  1223. ; dx                    x-coord of destination upper-left corner
  1224. ; dy                    y-coord of destination upper-left corner
  1225. ; dw                    width of destination image
  1226. ; dh                    height of destination image
  1227. ; sx                    x-coordinate of source upper-left corner
  1228. ; sy                    y-coordinate of source upper-left corner
  1229. ; sw                    width of source image
  1230. ; sh                    height of source image
  1231. ; Matrix                a matrix used to alter image attributes when drawing
  1232. ;
  1233. ; return                status enumeration. 0 = success
  1234. ;
  1235. ; notes                 if sx,sy,sw,sh are missed then the entire source bitmap will be used
  1236. ;                       Gdip_DrawImage performs faster
  1237. ;                       Matrix can be omitted to just draw with no alteration to ARGB
  1238. ;                       Matrix may be passed as a digit from 0 - 1 to change just transparency
  1239. ;                       Matrix can be passed as a matrix with any delimiter. For example:
  1240. ;                       MatrixBright=
  1241. ;                       (
  1242. ;                       1.5     |0      |0      |0      |0
  1243. ;                       0       |1.5    |0      |0      |0
  1244. ;                       0       |0      |1.5    |0      |0
  1245. ;                       0       |0      |0      |1      |0
  1246. ;                       0.05    |0.05   |0.05   |0      |1
  1247. ;                       )
  1248. ;
  1249. ; notes                 MatrixBright = 1.5|0|0|0|0|0|1.5|0|0|0|0|0|1.5|0|0|0|0|0|1|0|0.05|0.05|0.05|0|1
  1250. ;                       MatrixGreyScale = 0.299|0.299|0.299|0|0|0.587|0.587|0.587|0|0|0.114|0.114|0.114|0|0|0|0|0|1|0|0|0|0|0|1
  1251. ;                       MatrixNegative = -1|0|0|0|0|0|-1|0|0|0|0|0|-1|0|0|0|0|0|1|0|0|0|0|0|1
  1252.  
  1253. Gdip_DrawImage(pGraphics, pBitmap, dx="", dy="", dw="", dh="", sx="", sy="", sw="", sh="", Matrix=1)
  1254. {
  1255.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  1256.    
  1257.     if (Matrix&1 = "")
  1258.         ImageAttr := Gdip_SetImageAttributesColorMatrix(Matrix)
  1259.     else if (Matrix != 1)
  1260.         ImageAttr := Gdip_SetImageAttributesColorMatrix("1|0|0|0|0|0|1|0|0|0|0|0|1|0|0|0|0|0|" Matrix "|0|0|0|0|0|1")
  1261.  
  1262.     if (sx = "" && sy = "" && sw = "" && sh = "")
  1263.     {
  1264.         if (dx = "" && dy = "" && dw = "" && dh = "")
  1265.         {
  1266.             sx := dx := 0, sy := dy := 0
  1267.             sw := dw := Gdip_GetImageWidth(pBitmap)
  1268.             sh := dh := Gdip_GetImageHeight(pBitmap)
  1269.         }
  1270.         else
  1271.         {
  1272.             sx := sy := 0
  1273.             sw := Gdip_GetImageWidth(pBitmap)
  1274.             sh := Gdip_GetImageHeight(pBitmap)
  1275.         }
  1276.     }
  1277.  
  1278.     E := DllCall("gdiplus\GdipDrawImageRectRect"
  1279.                 , Ptr, pGraphics
  1280.                 , Ptr, pBitmap
  1281.                 , "float", dx
  1282.                 , "float", dy
  1283.                 , "float", dw
  1284.                 , "float", dh
  1285.                 , "float", sx
  1286.                 , "float", sy
  1287.                 , "float", sw
  1288.                 , "float", sh
  1289.                 , "int", 2
  1290.                 , Ptr, ImageAttr
  1291.                 , Ptr, 0
  1292.                 , Ptr, 0)
  1293.     if ImageAttr
  1294.         Gdip_DisposeImageAttributes(ImageAttr)
  1295.     return E
  1296. }
  1297.  
  1298. ;#####################################################################################
  1299.  
  1300. ; Function              Gdip_SetImageAttributesColorMatrix
  1301. ; Description           This function creates an image matrix ready for drawing
  1302. ;
  1303. ; Matrix                a matrix used to alter image attributes when drawing
  1304. ;                       passed with any delimeter
  1305. ;
  1306. ; return                returns an image matrix on sucess or 0 if it fails
  1307. ;
  1308. ; notes                 MatrixBright = 1.5|0|0|0|0|0|1.5|0|0|0|0|0|1.5|0|0|0|0|0|1|0|0.05|0.05|0.05|0|1
  1309. ;                       MatrixGreyScale = 0.299|0.299|0.299|0|0|0.587|0.587|0.587|0|0|0.114|0.114|0.114|0|0|0|0|0|1|0|0|0|0|0|1
  1310. ;                       MatrixNegative = -1|0|0|0|0|0|-1|0|0|0|0|0|-1|0|0|0|0|0|1|0|0|0|0|0|1
  1311.  
  1312. Gdip_SetImageAttributesColorMatrix(Matrix)
  1313. {
  1314.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  1315.    
  1316.     VarSetCapacity(ColourMatrix, 100, 0)
  1317.     Matrix := RegExReplace(RegExReplace(Matrix, "^[^\d-\.]+([\d\.])", "$1", "", 1), "[^\d-\.]+", "|")
  1318.     StringSplit, Matrix, Matrix, |
  1319.     Loop, 25
  1320.     {
  1321.         Matrix := (Matrix%A_Index% != "") ? Matrix%A_Index% : Mod(A_Index-1, 6) ? 0 : 1
  1322.         NumPut(Matrix, ColourMatrix, (A_Index-1)*4, "float")
  1323.     }
  1324.     DllCall("gdiplus\GdipCreateImageAttributes", A_PtrSize ? "UPtr*" : "uint*", ImageAttr)
  1325.     DllCall("gdiplus\GdipSetImageAttributesColorMatrix", Ptr, ImageAttr, "int", 1, "int", 1, Ptr, &ColourMatrix, Ptr, 0, "int", 0)
  1326.     return ImageAttr
  1327. }
  1328.  
  1329. ;#####################################################################################
  1330.  
  1331. ; Function              Gdip_GraphicsFromImage
  1332. ; Description           This function gets the graphics for a bitmap used for drawing functions
  1333. ;
  1334. ; pBitmap               Pointer to a bitmap to get the pointer to its graphics
  1335. ;
  1336. ; return                returns a pointer to the graphics of a bitmap
  1337. ;
  1338. ; notes                 a bitmap can be drawn into the graphics of another bitmap
  1339.  
  1340. Gdip_GraphicsFromImage(pBitmap)
  1341. {
  1342.     DllCall("gdiplus\GdipGetImageGraphicsContext", A_PtrSize ? "UPtr" : "UInt", pBitmap, A_PtrSize ? "UPtr*" : "UInt*", pGraphics)
  1343.     return pGraphics
  1344. }
  1345.  
  1346. ;#####################################################################################
  1347.  
  1348. ; Function              Gdip_GraphicsFromHDC
  1349. ; Description           This function gets the graphics from the handle to a device context
  1350. ;
  1351. ; hdc                   This is the handle to the device context
  1352. ;
  1353. ; return                returns a pointer to the graphics of a bitmap
  1354. ;
  1355. ; notes                 You can draw a bitmap into the graphics of another bitmap
  1356.  
  1357. Gdip_GraphicsFromHDC(hdc)
  1358. {
  1359.     DllCall("gdiplus\GdipCreateFromHDC", A_PtrSize ? "UPtr" : "UInt", hdc, A_PtrSize ? "UPtr*" : "UInt*", pGraphics)
  1360.     return pGraphics
  1361. }
  1362.  
  1363. ;#####################################################################################
  1364.  
  1365. ; Function              Gdip_GetDC
  1366. ; Description           This function gets the device context of the passed Graphics
  1367. ;
  1368. ; hdc                   This is the handle to the device context
  1369. ;
  1370. ; return                returns the device context for the graphics of a bitmap
  1371.  
  1372. Gdip_GetDC(pGraphics)
  1373. {
  1374.     DllCall("gdiplus\GdipGetDC", A_PtrSize ? "UPtr" : "UInt", pGraphics, A_PtrSize ? "UPtr*" : "UInt*", hdc)
  1375.     return hdc
  1376. }
  1377.  
  1378. ;#####################################################################################
  1379.  
  1380. ; Function              Gdip_ReleaseDC
  1381. ; Description           This function releases a device context from use for further use
  1382. ;
  1383. ; pGraphics             Pointer to the graphics of a bitmap
  1384. ; hdc                   This is the handle to the device context
  1385. ;
  1386. ; return                status enumeration. 0 = success
  1387.  
  1388. Gdip_ReleaseDC(pGraphics, hdc)
  1389. {
  1390.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  1391.    
  1392.     return DllCall("gdiplus\GdipReleaseDC", Ptr, pGraphics, Ptr, hdc)
  1393. }
  1394.  
  1395. ;#####################################################################################
  1396.  
  1397. ; Function              Gdip_GraphicsClear
  1398. ; Description           Clears the graphics of a bitmap ready for further drawing
  1399. ;
  1400. ; pGraphics             Pointer to the graphics of a bitmap
  1401. ; ARGB                  The colour to clear the graphics to
  1402. ;
  1403. ; return                status enumeration. 0 = success
  1404. ;
  1405. ; notes                 By default this will make the background invisible
  1406. ;                       Using clipping regions you can clear a particular area on the graphics rather than clearing the entire graphics
  1407.  
  1408. Gdip_GraphicsClear(pGraphics, ARGB=0x00ffffff)
  1409. {
  1410.     return DllCall("gdiplus\GdipGraphicsClear", A_PtrSize ? "UPtr" : "UInt", pGraphics, "int", ARGB)
  1411. }
  1412.  
  1413. ;#####################################################################################
  1414.  
  1415. ; Function              Gdip_BlurBitmap
  1416. ; Description           Gives a pointer to a blurred bitmap from a pointer to a bitmap
  1417. ;
  1418. ; pBitmap               Pointer to a bitmap to be blurred
  1419. ; Blur                  The Amount to blur a bitmap by from 1 (least blur) to 100 (most blur)
  1420. ;
  1421. ; return                If the function succeeds, the return value is a pointer to the new blurred bitmap
  1422. ;                       -1 = The blur parameter is outside the range 1-100
  1423. ;
  1424. ; notes                 This function will not dispose of the original bitmap
  1425.  
  1426. Gdip_BlurBitmap(pBitmap, Blur)
  1427. {
  1428.     if (Blur > 100) || (Blur < 1)
  1429.         return -1  
  1430.    
  1431.     sWidth := Gdip_GetImageWidth(pBitmap), sHeight := Gdip_GetImageHeight(pBitmap)
  1432.     dWidth := sWidth//Blur, dHeight := sHeight//Blur
  1433.  
  1434.     pBitmap1 := Gdip_CreateBitmap(dWidth, dHeight)
  1435.     G1 := Gdip_GraphicsFromImage(pBitmap1)
  1436.     Gdip_SetInterpolationMode(G1, 7)
  1437.     Gdip_DrawImage(G1, pBitmap, 0, 0, dWidth, dHeight, 0, 0, sWidth, sHeight)
  1438.  
  1439.     Gdip_DeleteGraphics(G1)
  1440.  
  1441.     pBitmap2 := Gdip_CreateBitmap(sWidth, sHeight)
  1442.     G2 := Gdip_GraphicsFromImage(pBitmap2)
  1443.     Gdip_SetInterpolationMode(G2, 7)
  1444.     Gdip_DrawImage(G2, pBitmap1, 0, 0, sWidth, sHeight, 0, 0, dWidth, dHeight)
  1445.  
  1446.     Gdip_DeleteGraphics(G2)
  1447.     Gdip_DisposeImage(pBitmap1)
  1448.     return pBitmap2
  1449. }
  1450.  
  1451. ;#####################################################################################
  1452.  
  1453. ; Function:             Gdip_SaveBitmapToFile
  1454. ; Description:          Saves a bitmap to a file in any supported format onto disk
  1455. ;  
  1456. ; pBitmap               Pointer to a bitmap
  1457. ; sOutput               The name of the file that the bitmap will be saved to. Supported extensions are: .BMP,.DIB,.RLE,.JPG,.JPEG,.JPE,.JFIF,.GIF,.TIF,.TIFF,.PNG
  1458. ; Quality               If saving as jpg (.JPG,.JPEG,.JPE,.JFIF) then quality can be 1-100 with default at maximum quality
  1459. ;
  1460. ; return                If the function succeeds, the return value is zero, otherwise:
  1461. ;                       -1 = Extension supplied is not a supported file format
  1462. ;                       -2 = Could not get a list of encoders on system
  1463. ;                       -3 = Could not find matching encoder for specified file format
  1464. ;                       -4 = Could not get WideChar name of output file
  1465. ;                       -5 = Could not save file to disk
  1466. ;
  1467. ; notes                 This function will use the extension supplied from the sOutput parameter to determine the output format
  1468.  
  1469. Gdip_SaveBitmapToFile(pBitmap, sOutput, Quality=75)
  1470. {
  1471.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  1472.    
  1473.     SplitPath, sOutput,,, Extension
  1474.     if Extension not in BMP,DIB,RLE,JPG,JPEG,JPE,JFIF,GIF,TIF,TIFF,PNG
  1475.         return -1
  1476.     Extension := "." Extension
  1477.  
  1478.     DllCall("gdiplus\GdipGetImageEncodersSize", "uint*", nCount, "uint*", nSize)
  1479.     VarSetCapacity(ci, nSize)
  1480.     DllCall("gdiplus\GdipGetImageEncoders", "uint", nCount, "uint", nSize, Ptr, &ci)
  1481.     if !(nCount && nSize)
  1482.         return -2
  1483.    
  1484.     If (A_IsUnicode){
  1485.         StrGet_Name := "StrGet"
  1486.         Loop, %nCount%
  1487.         {
  1488.             sString := %StrGet_Name%(NumGet(ci, (idx := (48+7*A_PtrSize)*(A_Index-1))+32+3*A_PtrSize), "UTF-16")
  1489.             if !InStr(sString, "*" Extension)
  1490.                 continue
  1491.            
  1492.             pCodec := &ci+idx
  1493.             break
  1494.         }
  1495.     } else {
  1496.         Loop, %nCount%
  1497.         {
  1498.             Location := NumGet(ci, 76*(A_Index-1)+44)
  1499.             nSize := DllCall("WideCharToMultiByte", "uint", 0, "uint", 0, "uint", Location, "int", -1, "uint", 0, "int",  0, "uint", 0, "uint", 0)
  1500.             VarSetCapacity(sString, nSize)
  1501.             DllCall("WideCharToMultiByte", "uint", 0, "uint", 0, "uint", Location, "int", -1, "str", sString, "int", nSize, "uint", 0, "uint", 0)
  1502.             if !InStr(sString, "*" Extension)
  1503.                 continue
  1504.            
  1505.             pCodec := &ci+76*(A_Index-1)
  1506.             break
  1507.         }
  1508.     }
  1509.    
  1510.     if !pCodec
  1511.         return -3
  1512.  
  1513.     if (Quality != 75)
  1514.     {
  1515.         Quality := (Quality < 0) ? 0 : (Quality > 100) ? 100 : Quality
  1516.         if Extension in .JPG,.JPEG,.JPE,.JFIF
  1517.         {
  1518.             DllCall("gdiplus\GdipGetEncoderParameterListSize", Ptr, pBitmap, Ptr, pCodec, "uint*", nSize)
  1519.             VarSetCapacity(EncoderParameters, nSize, 0)
  1520.             DllCall("gdiplus\GdipGetEncoderParameterList", Ptr, pBitmap, Ptr, pCodec, "uint", nSize, Ptr, &EncoderParameters)
  1521.             Loop, % NumGet(EncoderParameters, "UInt")      ;%
  1522.             {
  1523.                 elem := (24+(A_PtrSize ? A_PtrSize : 4))*(A_Index-1) + 4 + (pad := A_PtrSize = 8 ? 4 : 0)
  1524.                 if (NumGet(EncoderParameters, elem+16, "UInt") = 1) && (NumGet(EncoderParameters, elem+20, "UInt") = 6)
  1525.                 {
  1526.                     p := elem+&EncoderParameters-pad-4
  1527.                     NumPut(Quality, NumGet(NumPut(4, NumPut(1, p+0)+20, "UInt")), "UInt")
  1528.                     break
  1529.                 }
  1530.             }      
  1531.         }
  1532.     }
  1533.  
  1534.     if (!A_IsUnicode)
  1535.     {
  1536.         nSize := DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &sOutput, "int", -1, Ptr, 0, "int", 0)
  1537.         VarSetCapacity(wOutput, nSize*2)
  1538.         DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &sOutput, "int", -1, Ptr, &wOutput, "int", nSize)
  1539.         VarSetCapacity(wOutput, -1)
  1540.         if !VarSetCapacity(wOutput)
  1541.             return -4
  1542.         E := DllCall("gdiplus\GdipSaveImageToFile", Ptr, pBitmap, Ptr, &wOutput, Ptr, pCodec, "uint", p ? p : 0)
  1543.     }
  1544.     else
  1545.         E := DllCall("gdiplus\GdipSaveImageToFile", Ptr, pBitmap, Ptr, &sOutput, Ptr, pCodec, "uint", p ? p : 0)
  1546.     return E ? -5 : 0
  1547. }
  1548.  
  1549. ;#####################################################################################
  1550.  
  1551. ; Function              Gdip_GetPixel
  1552. ; Description           Gets the ARGB of a pixel in a bitmap
  1553. ;
  1554. ; pBitmap               Pointer to a bitmap
  1555. ; x                     x-coordinate of the pixel
  1556. ; y                     y-coordinate of the pixel
  1557. ;
  1558. ; return                Returns the ARGB value of the pixel
  1559.  
  1560. Gdip_GetPixel(pBitmap, x, y)
  1561. {
  1562.     DllCall("gdiplus\GdipBitmapGetPixel", A_PtrSize ? "UPtr" : "UInt", pBitmap, "int", x, "int", y, "uint*", ARGB)
  1563.     return ARGB
  1564. }
  1565.  
  1566. ;#####################################################################################
  1567.  
  1568. ; Function              Gdip_SetPixel
  1569. ; Description           Sets the ARGB of a pixel in a bitmap
  1570. ;
  1571. ; pBitmap               Pointer to a bitmap
  1572. ; x                     x-coordinate of the pixel
  1573. ; y                     y-coordinate of the pixel
  1574. ;
  1575. ; return                status enumeration. 0 = success
  1576.  
  1577. Gdip_SetPixel(pBitmap, x, y, ARGB)
  1578. {
  1579.    return DllCall("gdiplus\GdipBitmapSetPixel", A_PtrSize ? "UPtr" : "UInt", pBitmap, "int", x, "int", y, "int", ARGB)
  1580. }
  1581.  
  1582. ;#####################################################################################
  1583.  
  1584. ; Function              Gdip_GetImageWidth
  1585. ; Description           Gives the width of a bitmap
  1586. ;
  1587. ; pBitmap               Pointer to a bitmap
  1588. ;
  1589. ; return                Returns the width in pixels of the supplied bitmap
  1590.  
  1591. Gdip_GetImageWidth(pBitmap)
  1592. {
  1593.    DllCall("gdiplus\GdipGetImageWidth", A_PtrSize ? "UPtr" : "UInt", pBitmap, "uint*", Width)
  1594.    return Width
  1595. }
  1596.  
  1597. ;#####################################################################################
  1598.  
  1599. ; Function              Gdip_GetImageHeight
  1600. ; Description           Gives the height of a bitmap
  1601. ;
  1602. ; pBitmap               Pointer to a bitmap
  1603. ;
  1604. ; return                Returns the height in pixels of the supplied bitmap
  1605.  
  1606. Gdip_GetImageHeight(pBitmap)
  1607. {
  1608.    DllCall("gdiplus\GdipGetImageHeight", A_PtrSize ? "UPtr" : "UInt", pBitmap, "uint*", Height)
  1609.    return Height
  1610. }
  1611.  
  1612. ;#####################################################################################
  1613.  
  1614. ; Function              Gdip_GetDimensions
  1615. ; Description           Gives the width and height of a bitmap
  1616. ;
  1617. ; pBitmap               Pointer to a bitmap
  1618. ; Width                 ByRef variable. This variable will be set to the width of the bitmap
  1619. ; Height                ByRef variable. This variable will be set to the height of the bitmap
  1620. ;
  1621. ; return                No return value
  1622. ;                       Gdip_GetDimensions(pBitmap, ThisWidth, ThisHeight) will set ThisWidth to the width and ThisHeight to the height
  1623.  
  1624. Gdip_GetImageDimensions(pBitmap, ByRef Width, ByRef Height)
  1625. {
  1626.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  1627.     DllCall("gdiplus\GdipGetImageWidth", Ptr, pBitmap, "uint*", Width)
  1628.     DllCall("gdiplus\GdipGetImageHeight", Ptr, pBitmap, "uint*", Height)
  1629. }
  1630.  
  1631. ;#####################################################################################
  1632.  
  1633. Gdip_GetDimensions(pBitmap, ByRef Width, ByRef Height)
  1634. {
  1635.     Gdip_GetImageDimensions(pBitmap, Width, Height)
  1636. }
  1637.  
  1638. ;#####################################################################################
  1639.  
  1640. Gdip_GetImagePixelFormat(pBitmap)
  1641. {
  1642.     DllCall("gdiplus\GdipGetImagePixelFormat", A_PtrSize ? "UPtr" : "UInt", pBitmap, A_PtrSize ? "UPtr*" : "UInt*", Format)
  1643.     return Format
  1644. }
  1645.  
  1646. ;#####################################################################################
  1647.  
  1648. ; Function              Gdip_GetDpiX
  1649. ; Description           Gives the horizontal dots per inch of the graphics of a bitmap
  1650. ;
  1651. ; pBitmap               Pointer to a bitmap
  1652. ; Width                 ByRef variable. This variable will be set to the width of the bitmap
  1653. ; Height                ByRef variable. This variable will be set to the height of the bitmap
  1654. ;
  1655. ; return                No return value
  1656. ;                       Gdip_GetDimensions(pBitmap, ThisWidth, ThisHeight) will set ThisWidth to the width and ThisHeight to the height
  1657.  
  1658. Gdip_GetDpiX(pGraphics)
  1659. {
  1660.     DllCall("gdiplus\GdipGetDpiX", A_PtrSize ? "UPtr" : "uint", pGraphics, "float*", dpix)
  1661.     return Round(dpix)
  1662. }
  1663.  
  1664. ;#####################################################################################
  1665.  
  1666. Gdip_GetDpiY(pGraphics)
  1667. {
  1668.     DllCall("gdiplus\GdipGetDpiY", A_PtrSize ? "UPtr" : "uint", pGraphics, "float*", dpiy)
  1669.     return Round(dpiy)
  1670. }
  1671.  
  1672. ;#####################################################################################
  1673.  
  1674. Gdip_GetImageHorizontalResolution(pBitmap)
  1675. {
  1676.     DllCall("gdiplus\GdipGetImageHorizontalResolution", A_PtrSize ? "UPtr" : "uint", pBitmap, "float*", dpix)
  1677.     return Round(dpix)
  1678. }
  1679.  
  1680. ;#####################################################################################
  1681.  
  1682. Gdip_GetImageVerticalResolution(pBitmap)
  1683. {
  1684.     DllCall("gdiplus\GdipGetImageVerticalResolution", A_PtrSize ? "UPtr" : "uint", pBitmap, "float*", dpiy)
  1685.     return Round(dpiy)
  1686. }
  1687.  
  1688. ;#####################################################################################
  1689.  
  1690. Gdip_BitmapSetResolution(pBitmap, dpix, dpiy)
  1691. {
  1692.     return DllCall("gdiplus\GdipBitmapSetResolution", A_PtrSize ? "UPtr" : "uint", pBitmap, "float", dpix, "float", dpiy)
  1693. }
  1694.  
  1695. ;#####################################################################################
  1696.  
  1697. Gdip_CreateBitmapFromFile(sFile, IconNumber=1, IconSize="")
  1698. {
  1699.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  1700.     , PtrA := A_PtrSize ? "UPtr*" : "UInt*"
  1701.    
  1702.     SplitPath, sFile,,, ext
  1703.     if ext in exe,dll
  1704.     {
  1705.         Sizes := IconSize ? IconSize : 256 "|" 128 "|" 64 "|" 48 "|" 32 "|" 16
  1706.         BufSize := 16 + (2*(A_PtrSize ? A_PtrSize : 4))
  1707.        
  1708.         VarSetCapacity(buf, BufSize, 0)
  1709.         Loop, Parse, Sizes, |
  1710.         {
  1711.             DllCall("PrivateExtractIcons", "str", sFile, "int", IconNumber-1, "int", A_LoopField, "int", A_LoopField, PtrA, hIcon, PtrA, 0, "uint", 1, "uint", 0)
  1712.            
  1713.             if !hIcon
  1714.                 continue
  1715.  
  1716.             if !DllCall("GetIconInfo", Ptr, hIcon, Ptr, &buf)
  1717.             {
  1718.                 DestroyIcon(hIcon)
  1719.                 continue
  1720.             }
  1721.            
  1722.             hbmMask  := NumGet(buf, 12 + ((A_PtrSize ? A_PtrSize : 4) - 4))
  1723.             hbmColor := NumGet(buf, 12 + ((A_PtrSize ? A_PtrSize : 4) - 4) + (A_PtrSize ? A_PtrSize : 4))
  1724.             if !(hbmColor && DllCall("GetObject", Ptr, hbmColor, "int", BufSize, Ptr, &buf))
  1725.             {
  1726.                 DestroyIcon(hIcon)
  1727.                 continue
  1728.             }
  1729.             break
  1730.         }
  1731.         if !hIcon
  1732.             return -1
  1733.  
  1734.         Width := NumGet(buf, 4, "int"), Height := NumGet(buf, 8, "int")
  1735.         hbm := CreateDIBSection(Width, -Height), hdc := CreateCompatibleDC(), obm := SelectObject(hdc, hbm)
  1736.         if !DllCall("DrawIconEx", Ptr, hdc, "int", 0, "int", 0, Ptr, hIcon, "uint", Width, "uint", Height, "uint", 0, Ptr, 0, "uint", 3)
  1737.         {
  1738.             DestroyIcon(hIcon)
  1739.             return -2
  1740.         }
  1741.        
  1742.         VarSetCapacity(dib, 104)
  1743.         DllCall("GetObject", Ptr, hbm, "int", A_PtrSize = 8 ? 104 : 84, Ptr, &dib) ; sizeof(DIBSECTION) = 76+2*(A_PtrSize=8?4:0)+2*A_PtrSize
  1744.         Stride := NumGet(dib, 12, "Int"), Bits := NumGet(dib, 20 + (A_PtrSize = 8 ? 4 : 0)) ; padding
  1745.         DllCall("gdiplus\GdipCreateBitmapFromScan0", "int", Width, "int", Height, "int", Stride, "int", 0x26200A, Ptr, Bits, PtrA, pBitmapOld)
  1746.         pBitmap := Gdip_CreateBitmap(Width, Height)
  1747.         G := Gdip_GraphicsFromImage(pBitmap)
  1748.         , Gdip_DrawImage(G, pBitmapOld, 0, 0, Width, Height, 0, 0, Width, Height)
  1749.         SelectObject(hdc, obm), DeleteObject(hbm), DeleteDC(hdc)
  1750.         Gdip_DeleteGraphics(G), Gdip_DisposeImage(pBitmapOld)
  1751.         DestroyIcon(hIcon)
  1752.     }
  1753.     else
  1754.     {
  1755.         if (!A_IsUnicode)
  1756.         {
  1757.             VarSetCapacity(wFile, 1024)
  1758.             DllCall("kernel32\MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &sFile, "int", -1, Ptr, &wFile, "int", 512)
  1759.             DllCall("gdiplus\GdipCreateBitmapFromFile", Ptr, &wFile, PtrA, pBitmap)
  1760.         }
  1761.         else
  1762.             DllCall("gdiplus\GdipCreateBitmapFromFile", Ptr, &sFile, PtrA, pBitmap)
  1763.     }
  1764.    
  1765.     return pBitmap
  1766. }
  1767.  
  1768. ;#####################################################################################
  1769.  
  1770. Gdip_CreateBitmapFromHBITMAP(hBitmap, Palette=0)
  1771. {
  1772.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  1773.    
  1774.     DllCall("gdiplus\GdipCreateBitmapFromHBITMAP", Ptr, hBitmap, Ptr, Palette, A_PtrSize ? "UPtr*" : "uint*", pBitmap)
  1775.     return pBitmap
  1776. }
  1777.  
  1778. ;#####################################################################################
  1779.  
  1780. Gdip_CreateHBITMAPFromBitmap(pBitmap, Background=0xffffffff)
  1781. {
  1782.     DllCall("gdiplus\GdipCreateHBITMAPFromBitmap", A_PtrSize ? "UPtr" : "UInt", pBitmap, A_PtrSize ? "UPtr*" : "uint*", hbm, "int", Background)
  1783.     return hbm
  1784. }
  1785.  
  1786. ;#####################################################################################
  1787.  
  1788. Gdip_CreateBitmapFromHICON(hIcon)
  1789. {
  1790.     DllCall("gdiplus\GdipCreateBitmapFromHICON", A_PtrSize ? "UPtr" : "UInt", hIcon, A_PtrSize ? "UPtr*" : "uint*", pBitmap)
  1791.     return pBitmap
  1792. }
  1793.  
  1794. ;#####################################################################################
  1795.  
  1796. Gdip_CreateHICONFromBitmap(pBitmap)
  1797. {
  1798.     DllCall("gdiplus\GdipCreateHICONFromBitmap", A_PtrSize ? "UPtr" : "UInt", pBitmap, A_PtrSize ? "UPtr*" : "uint*", hIcon)
  1799.     return hIcon
  1800. }
  1801.  
  1802. ;#####################################################################################
  1803.  
  1804. Gdip_CreateBitmap(Width, Height, Format=0x26200A)
  1805. {
  1806.     DllCall("gdiplus\GdipCreateBitmapFromScan0", "int", Width, "int", Height, "int", 0, "int", Format, A_PtrSize ? "UPtr" : "UInt", 0, A_PtrSize ? "UPtr*" : "uint*", pBitmap)
  1807.     Return pBitmap
  1808. }
  1809.  
  1810. ;#####################################################################################
  1811.  
  1812. Gdip_CreateBitmapFromClipboard()
  1813. {
  1814.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  1815.    
  1816.     if !DllCall("OpenClipboard", Ptr, 0)
  1817.         return -1
  1818.     if !DllCall("IsClipboardFormatAvailable", "uint", 8)
  1819.         return -2
  1820.     if !hBitmap := DllCall("GetClipboardData", "uint", 2, Ptr)
  1821.         return -3
  1822.     if !pBitmap := Gdip_CreateBitmapFromHBITMAP(hBitmap)
  1823.         return -4
  1824.     if !DllCall("CloseClipboard")
  1825.         return -5
  1826.     DeleteObject(hBitmap)
  1827.     return pBitmap
  1828. }
  1829.  
  1830. ;#####################################################################################
  1831.  
  1832. Gdip_SetBitmapToClipboard(pBitmap)
  1833. {
  1834.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  1835.     off1 := A_PtrSize = 8 ? 52 : 44, off2 := A_PtrSize = 8 ? 32 : 24
  1836.     hBitmap := Gdip_CreateHBITMAPFromBitmap(pBitmap)
  1837.     DllCall("GetObject", Ptr, hBitmap, "int", VarSetCapacity(oi, A_PtrSize = 8 ? 104 : 84, 0), Ptr, &oi)
  1838.     hdib := DllCall("GlobalAlloc", "uint", 2, Ptr, 40+NumGet(oi, off1, "UInt"), Ptr)
  1839.     pdib := DllCall("GlobalLock", Ptr, hdib, Ptr)
  1840.     DllCall("RtlMoveMemory", Ptr, pdib, Ptr, &oi+off2, Ptr, 40)
  1841.     DllCall("RtlMoveMemory", Ptr, pdib+40, Ptr, NumGet(oi, off2 - (A_PtrSize ? A_PtrSize : 4), Ptr), Ptr, NumGet(oi, off1, "UInt"))
  1842.     DllCall("GlobalUnlock", Ptr, hdib)
  1843.     DllCall("DeleteObject", Ptr, hBitmap)
  1844.     DllCall("OpenClipboard", Ptr, 0)
  1845.     DllCall("EmptyClipboard")
  1846.     DllCall("SetClipboardData", "uint", 8, Ptr, hdib)
  1847.     DllCall("CloseClipboard")
  1848. }
  1849.  
  1850. ;#####################################################################################
  1851.  
  1852. Gdip_CloneBitmapArea(pBitmap, x, y, w, h, Format=0x26200A)
  1853. {
  1854.     DllCall("gdiplus\GdipCloneBitmapArea"
  1855.                     , "float", x
  1856.                     , "float", y
  1857.                     , "float", w
  1858.                     , "float", h
  1859.                     , "int", Format
  1860.                     , A_PtrSize ? "UPtr" : "UInt", pBitmap
  1861.                     , A_PtrSize ? "UPtr*" : "UInt*", pBitmapDest)
  1862.     return pBitmapDest
  1863. }
  1864.  
  1865. ;#####################################################################################
  1866. ; Create resources
  1867. ;#####################################################################################
  1868.  
  1869. Gdip_CreatePen(ARGB, w)
  1870. {
  1871.    DllCall("gdiplus\GdipCreatePen1", "UInt", ARGB, "float", w, "int", 2, A_PtrSize ? "UPtr*" : "UInt*", pPen)
  1872.    return pPen
  1873. }
  1874.  
  1875. ;#####################################################################################
  1876.  
  1877. Gdip_CreatePenFromBrush(pBrush, w)
  1878. {
  1879.     DllCall("gdiplus\GdipCreatePen2", A_PtrSize ? "UPtr" : "UInt", pBrush, "float", w, "int", 2, A_PtrSize ? "UPtr*" : "UInt*", pPen)
  1880.     return pPen
  1881. }
  1882.  
  1883. ;#####################################################################################
  1884.  
  1885. Gdip_BrushCreateSolid(ARGB=0xff000000)
  1886. {
  1887.     DllCall("gdiplus\GdipCreateSolidFill", "UInt", ARGB, A_PtrSize ? "UPtr*" : "UInt*", pBrush)
  1888.     return pBrush
  1889. }
  1890.  
  1891. ;#####################################################################################
  1892.  
  1893. ; HatchStyleHorizontal = 0
  1894. ; HatchStyleVertical = 1
  1895. ; HatchStyleForwardDiagonal = 2
  1896. ; HatchStyleBackwardDiagonal = 3
  1897. ; HatchStyleCross = 4
  1898. ; HatchStyleDiagonalCross = 5
  1899. ; HatchStyle05Percent = 6
  1900. ; HatchStyle10Percent = 7
  1901. ; HatchStyle20Percent = 8
  1902. ; HatchStyle25Percent = 9
  1903. ; HatchStyle30Percent = 10
  1904. ; HatchStyle40Percent = 11
  1905. ; HatchStyle50Percent = 12
  1906. ; HatchStyle60Percent = 13
  1907. ; HatchStyle70Percent = 14
  1908. ; HatchStyle75Percent = 15
  1909. ; HatchStyle80Percent = 16
  1910. ; HatchStyle90Percent = 17
  1911. ; HatchStyleLightDownwardDiagonal = 18
  1912. ; HatchStyleLightUpwardDiagonal = 19
  1913. ; HatchStyleDarkDownwardDiagonal = 20
  1914. ; HatchStyleDarkUpwardDiagonal = 21
  1915. ; HatchStyleWideDownwardDiagonal = 22
  1916. ; HatchStyleWideUpwardDiagonal = 23
  1917. ; HatchStyleLightVertical = 24
  1918. ; HatchStyleLightHorizontal = 25
  1919. ; HatchStyleNarrowVertical = 26
  1920. ; HatchStyleNarrowHorizontal = 27
  1921. ; HatchStyleDarkVertical = 28
  1922. ; HatchStyleDarkHorizontal = 29
  1923. ; HatchStyleDashedDownwardDiagonal = 30
  1924. ; HatchStyleDashedUpwardDiagonal = 31
  1925. ; HatchStyleDashedHorizontal = 32
  1926. ; HatchStyleDashedVertical = 33
  1927. ; HatchStyleSmallConfetti = 34
  1928. ; HatchStyleLargeConfetti = 35
  1929. ; HatchStyleZigZag = 36
  1930. ; HatchStyleWave = 37
  1931. ; HatchStyleDiagonalBrick = 38
  1932. ; HatchStyleHorizontalBrick = 39
  1933. ; HatchStyleWeave = 40
  1934. ; HatchStylePlaid = 41
  1935. ; HatchStyleDivot = 42
  1936. ; HatchStyleDottedGrid = 43
  1937. ; HatchStyleDottedDiamond = 44
  1938. ; HatchStyleShingle = 45
  1939. ; HatchStyleTrellis = 46
  1940. ; HatchStyleSphere = 47
  1941. ; HatchStyleSmallGrid = 48
  1942. ; HatchStyleSmallCheckerBoard = 49
  1943. ; HatchStyleLargeCheckerBoard = 50
  1944. ; HatchStyleOutlinedDiamond = 51
  1945. ; HatchStyleSolidDiamond = 52
  1946. ; HatchStyleTotal = 53
  1947. Gdip_BrushCreateHatch(ARGBfront, ARGBback, HatchStyle=0)
  1948. {
  1949.     DllCall("gdiplus\GdipCreateHatchBrush", "int", HatchStyle, "UInt", ARGBfront, "UInt", ARGBback, A_PtrSize ? "UPtr*" : "UInt*", pBrush)
  1950.     return pBrush
  1951. }
  1952.  
  1953. ;#####################################################################################
  1954.  
  1955. Gdip_CreateTextureBrush(pBitmap, WrapMode=1, x=0, y=0, w="", h="")
  1956. {
  1957.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  1958.     , PtrA := A_PtrSize ? "UPtr*" : "UInt*"
  1959.    
  1960.     if !(w && h)
  1961.         DllCall("gdiplus\GdipCreateTexture", Ptr, pBitmap, "int", WrapMode, PtrA, pBrush)
  1962.     else
  1963.         DllCall("gdiplus\GdipCreateTexture2", Ptr, pBitmap, "int", WrapMode, "float", x, "float", y, "float", w, "float", h, PtrA, pBrush)
  1964.     return pBrush
  1965. }
  1966.  
  1967. ;#####################################################################################
  1968.  
  1969. ; WrapModeTile = 0
  1970. ; WrapModeTileFlipX = 1
  1971. ; WrapModeTileFlipY = 2
  1972. ; WrapModeTileFlipXY = 3
  1973. ; WrapModeClamp = 4
  1974. Gdip_CreateLineBrush(x1, y1, x2, y2, ARGB1, ARGB2, WrapMode=1)
  1975. {
  1976.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  1977.    
  1978.     CreatePointF(PointF1, x1, y1), CreatePointF(PointF2, x2, y2)
  1979.     DllCall("gdiplus\GdipCreateLineBrush", Ptr, &PointF1, Ptr, &PointF2, "Uint", ARGB1, "Uint", ARGB2, "int", WrapMode, A_PtrSize ? "UPtr*" : "UInt*", LGpBrush)
  1980.     return LGpBrush
  1981. }
  1982.  
  1983. ;#####################################################################################
  1984.  
  1985. ; LinearGradientModeHorizontal = 0
  1986. ; LinearGradientModeVertical = 1
  1987. ; LinearGradientModeForwardDiagonal = 2
  1988. ; LinearGradientModeBackwardDiagonal = 3
  1989. Gdip_CreateLineBrushFromRect(x, y, w, h, ARGB1, ARGB2, LinearGradientMode=1, WrapMode=1)
  1990. {
  1991.     CreateRectF(RectF, x, y, w, h)
  1992.     DllCall("gdiplus\GdipCreateLineBrushFromRect", A_PtrSize ? "UPtr" : "UInt", &RectF, "int", ARGB1, "int", ARGB2, "int", LinearGradientMode, "int", WrapMode, A_PtrSize ? "UPtr*" : "UInt*", LGpBrush)
  1993.     return LGpBrush
  1994. }
  1995.  
  1996. ;#####################################################################################
  1997.  
  1998. Gdip_CloneBrush(pBrush)
  1999. {
  2000.     DllCall("gdiplus\GdipCloneBrush", A_PtrSize ? "UPtr" : "UInt", pBrush, A_PtrSize ? "UPtr*" : "UInt*", pBrushClone)
  2001.     return pBrushClone
  2002. }
  2003.  
  2004. ;#####################################################################################
  2005. ; Delete resources
  2006. ;#####################################################################################
  2007.  
  2008. Gdip_DeletePen(pPen)
  2009. {
  2010.    return DllCall("gdiplus\GdipDeletePen", A_PtrSize ? "UPtr" : "UInt", pPen)
  2011. }
  2012.  
  2013. ;#####################################################################################
  2014.  
  2015. Gdip_DeleteBrush(pBrush)
  2016. {
  2017.    return DllCall("gdiplus\GdipDeleteBrush", A_PtrSize ? "UPtr" : "UInt", pBrush)
  2018. }
  2019.  
  2020. ;#####################################################################################
  2021.  
  2022. Gdip_DisposeImage(pBitmap)
  2023. {
  2024.    return DllCall("gdiplus\GdipDisposeImage", A_PtrSize ? "UPtr" : "UInt", pBitmap)
  2025. }
  2026.  
  2027. ;#####################################################################################
  2028.  
  2029. Gdip_DeleteGraphics(pGraphics)
  2030. {
  2031.    return DllCall("gdiplus\GdipDeleteGraphics", A_PtrSize ? "UPtr" : "UInt", pGraphics)
  2032. }
  2033.  
  2034. ;#####################################################################################
  2035.  
  2036. Gdip_DisposeImageAttributes(ImageAttr)
  2037. {
  2038.     return DllCall("gdiplus\GdipDisposeImageAttributes", A_PtrSize ? "UPtr" : "UInt", ImageAttr)
  2039. }
  2040.  
  2041. ;#####################################################################################
  2042.  
  2043. Gdip_DeleteFont(hFont)
  2044. {
  2045.    return DllCall("gdiplus\GdipDeleteFont", A_PtrSize ? "UPtr" : "UInt", hFont)
  2046. }
  2047.  
  2048. ;#####################################################################################
  2049.  
  2050. Gdip_DeleteStringFormat(hFormat)
  2051. {
  2052.    return DllCall("gdiplus\GdipDeleteStringFormat", A_PtrSize ? "UPtr" : "UInt", hFormat)
  2053. }
  2054.  
  2055. ;#####################################################################################
  2056.  
  2057. Gdip_DeleteFontFamily(hFamily)
  2058. {
  2059.    return DllCall("gdiplus\GdipDeleteFontFamily", A_PtrSize ? "UPtr" : "UInt", hFamily)
  2060. }
  2061.  
  2062. ;#####################################################################################
  2063.  
  2064. Gdip_DeleteMatrix(Matrix)
  2065. {
  2066.    return DllCall("gdiplus\GdipDeleteMatrix", A_PtrSize ? "UPtr" : "UInt", Matrix)
  2067. }
  2068.  
  2069. ;#####################################################################################
  2070. ; Text functions
  2071. ;#####################################################################################
  2072.  
  2073. Gdip_TextToGraphics(pGraphics, Text, Options, Font="Arial", Width="", Height="", Measure=0)
  2074. {
  2075.     IWidth := Width, IHeight:= Height
  2076.    
  2077.     RegExMatch(Options, "i)X([\-\d\.]+)(p*)", xpos)
  2078.     RegExMatch(Options, "i)Y([\-\d\.]+)(p*)", ypos)
  2079.     RegExMatch(Options, "i)W([\-\d\.]+)(p*)", Width)
  2080.     RegExMatch(Options, "i)H([\-\d\.]+)(p*)", Height)
  2081.     RegExMatch(Options, "i)C(?!(entre|enter))([a-f\d]+)", Colour)
  2082.     RegExMatch(Options, "i)Top|Up|Bottom|Down|vCentre|vCenter", vPos)
  2083.     RegExMatch(Options, "i)NoWrap", NoWrap)
  2084.     RegExMatch(Options, "i)R(\d)", Rendering)
  2085.     RegExMatch(Options, "i)S(\d+)(p*)", Size)
  2086.  
  2087.     if !Gdip_DeleteBrush(Gdip_CloneBrush(Colour2))
  2088.         PassBrush := 1, pBrush := Colour2
  2089.    
  2090.     if !(IWidth && IHeight) && (xpos2 || ypos2 || Width2 || Height2 || Size2)
  2091.         return -1
  2092.  
  2093.     Style := 0, Styles := "Regular|Bold|Italic|BoldItalic|Underline|Strikeout"
  2094.     Loop, Parse, Styles, |
  2095.     {
  2096.         if RegExMatch(Options, "\b" A_loopField)
  2097.         Style |= (A_LoopField != "StrikeOut") ? (A_Index-1) : 8
  2098.     }
  2099.  
  2100.     Align := 0, Alignments := "Near|Left|Centre|Center|Far|Right"
  2101.     Loop, Parse, Alignments, |
  2102.     {
  2103.         if RegExMatch(Options, "\b" A_loopField)
  2104.             Align |= A_Index//2.1      ; 0|0|1|1|2|2
  2105.     }
  2106.  
  2107.     xpos := (xpos1 != "") ? xpos2 ? IWidth*(xpos1/100) : xpos1 : 0
  2108.     ypos := (ypos1 != "") ? ypos2 ? IHeight*(ypos1/100) : ypos1 : 0
  2109.     Width := Width1 ? Width2 ? IWidth*(Width1/100) : Width1 : IWidth
  2110.     Height := Height1 ? Height2 ? IHeight*(Height1/100) : Height1 : IHeight
  2111.     if !PassBrush
  2112.         Colour := "0x" (Colour2 ? Colour2 : "ff000000")
  2113.     Rendering := ((Rendering1 >= 0) && (Rendering1 <= 5)) ? Rendering1 : 4
  2114.     Size := (Size1 > 0) ? Size2 ? IHeight*(Size1/100) : Size1 : 12
  2115.  
  2116.     hFamily := Gdip_FontFamilyCreate(Font)
  2117.     hFont := Gdip_FontCreate(hFamily, Size, Style)
  2118.     FormatStyle := NoWrap ? 0x4000 | 0x1000 : 0x4000
  2119.     hFormat := Gdip_StringFormatCreate(FormatStyle)
  2120.     pBrush := PassBrush ? pBrush : Gdip_BrushCreateSolid(Colour)
  2121.     if !(hFamily && hFont && hFormat && pBrush && pGraphics)
  2122.         return !pGraphics ? -2 : !hFamily ? -3 : !hFont ? -4 : !hFormat ? -5 : !pBrush ? -6 : 0
  2123.    
  2124.     CreateRectF(RC, xpos, ypos, Width, Height)
  2125.     Gdip_SetStringFormatAlign(hFormat, Align)
  2126.     Gdip_SetTextRenderingHint(pGraphics, Rendering)
  2127.     ReturnRC := Gdip_MeasureString(pGraphics, Text, hFont, hFormat, RC)
  2128.  
  2129.     if vPos
  2130.     {
  2131.         StringSplit, ReturnRC, ReturnRC, |
  2132.        
  2133.         if (vPos = "vCentre") || (vPos = "vCenter")
  2134.             ypos += (Height-ReturnRC4)//2
  2135.         else if (vPos = "Top") || (vPos = "Up")
  2136.             ypos := 0
  2137.         else if (vPos = "Bottom") || (vPos = "Down")
  2138.             ypos := Height-ReturnRC4
  2139.        
  2140.         CreateRectF(RC, xpos, ypos, Width, ReturnRC4)
  2141.         ReturnRC := Gdip_MeasureString(pGraphics, Text, hFont, hFormat, RC)
  2142.     }
  2143.  
  2144.     if !Measure
  2145.         E := Gdip_DrawString(pGraphics, Text, hFont, hFormat, pBrush, RC)
  2146.  
  2147.     if !PassBrush
  2148.         Gdip_DeleteBrush(pBrush)
  2149.     Gdip_DeleteStringFormat(hFormat)  
  2150.     Gdip_DeleteFont(hFont)
  2151.     Gdip_DeleteFontFamily(hFamily)
  2152.     return E ? E : ReturnRC
  2153. }
  2154.  
  2155. ;#####################################################################################
  2156.  
  2157. Gdip_DrawString(pGraphics, sString, hFont, hFormat, pBrush, ByRef RectF)
  2158. {
  2159.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  2160.    
  2161.     if (!A_IsUnicode)
  2162.     {
  2163.         nSize := DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &sString, "int", -1, Ptr, 0, "int", 0)
  2164.         VarSetCapacity(wString, nSize*2)
  2165.         DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &sString, "int", -1, Ptr, &wString, "int", nSize)
  2166.     }
  2167.    
  2168.     return DllCall("gdiplus\GdipDrawString"
  2169.                     , Ptr, pGraphics
  2170.                     , Ptr, A_IsUnicode ? &sString : &wString
  2171.                     , "int", -1
  2172.                     , Ptr, hFont
  2173.                     , Ptr, &RectF
  2174.                     , Ptr, hFormat
  2175.                     , Ptr, pBrush)
  2176. }
  2177.  
  2178. ;#####################################################################################
  2179.  
  2180. Gdip_MeasureString(pGraphics, sString, hFont, hFormat, ByRef RectF)
  2181. {
  2182.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  2183.    
  2184.     VarSetCapacity(RC, 16)
  2185.     if !A_IsUnicode
  2186.     {
  2187.         nSize := DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &sString, "int", -1, "uint", 0, "int", 0)
  2188.         VarSetCapacity(wString, nSize*2)  
  2189.         DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &sString, "int", -1, Ptr, &wString, "int", nSize)
  2190.     }
  2191.    
  2192.     DllCall("gdiplus\GdipMeasureString"
  2193.                     , Ptr, pGraphics
  2194.                     , Ptr, A_IsUnicode ? &sString : &wString
  2195.                     , "int", -1
  2196.                     , Ptr, hFont
  2197.                     , Ptr, &RectF
  2198.                     , Ptr, hFormat
  2199.                     , Ptr, &RC
  2200.                     , "uint*", Chars
  2201.                     , "uint*", Lines)
  2202.    
  2203.     return &RC ? NumGet(RC, 0, "float") "|" NumGet(RC, 4, "float") "|" NumGet(RC, 8, "float") "|" NumGet(RC, 12, "float") "|" Chars "|" Lines : 0
  2204. }
  2205.  
  2206. ; Near = 0
  2207. ; Center = 1
  2208. ; Far = 2
  2209. Gdip_SetStringFormatAlign(hFormat, Align)
  2210. {
  2211.    return DllCall("gdiplus\GdipSetStringFormatAlign", A_PtrSize ? "UPtr" : "UInt", hFormat, "int", Align)
  2212. }
  2213.  
  2214. ; StringFormatFlagsDirectionRightToLeft    = 0x00000001
  2215. ; StringFormatFlagsDirectionVertical       = 0x00000002
  2216. ; StringFormatFlagsNoFitBlackBox           = 0x00000004
  2217. ; StringFormatFlagsDisplayFormatControl    = 0x00000020
  2218. ; StringFormatFlagsNoFontFallback          = 0x00000400
  2219. ; StringFormatFlagsMeasureTrailingSpaces   = 0x00000800
  2220. ; StringFormatFlagsNoWrap                  = 0x00001000
  2221. ; StringFormatFlagsLineLimit               = 0x00002000
  2222. ; StringFormatFlagsNoClip                  = 0x00004000
  2223. Gdip_StringFormatCreate(Format=0, Lang=0)
  2224. {
  2225.    DllCall("gdiplus\GdipCreateStringFormat", "int", Format, "int", Lang, A_PtrSize ? "UPtr*" : "UInt*", hFormat)
  2226.    return hFormat
  2227. }
  2228.  
  2229. ; Regular = 0
  2230. ; Bold = 1
  2231. ; Italic = 2
  2232. ; BoldItalic = 3
  2233. ; Underline = 4
  2234. ; Strikeout = 8
  2235. Gdip_FontCreate(hFamily, Size, Style=0)
  2236. {
  2237.    DllCall("gdiplus\GdipCreateFont", A_PtrSize ? "UPtr" : "UInt", hFamily, "float", Size, "int", Style, "int", 0, A_PtrSize ? "UPtr*" : "UInt*", hFont)
  2238.    return hFont
  2239. }
  2240.  
  2241. Gdip_FontFamilyCreate(Font)
  2242. {
  2243.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  2244.    
  2245.     if (!A_IsUnicode)
  2246.     {
  2247.         nSize := DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &Font, "int", -1, "uint", 0, "int", 0)
  2248.         VarSetCapacity(wFont, nSize*2)
  2249.         DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &Font, "int", -1, Ptr, &wFont, "int", nSize)
  2250.     }
  2251.    
  2252.     DllCall("gdiplus\GdipCreateFontFamilyFromName"
  2253.                     , Ptr, A_IsUnicode ? &Font : &wFont
  2254.                     , "uint", 0
  2255.                     , A_PtrSize ? "UPtr*" : "UInt*", hFamily)
  2256.    
  2257.     return hFamily
  2258. }
  2259.  
  2260. ;#####################################################################################
  2261. ; Matrix functions
  2262. ;#####################################################################################
  2263.  
  2264. Gdip_CreateAffineMatrix(m11, m12, m21, m22, x, y)
  2265. {
  2266.    DllCall("gdiplus\GdipCreateMatrix2", "float", m11, "float", m12, "float", m21, "float", m22, "float", x, "float", y, A_PtrSize ? "UPtr*" : "UInt*", Matrix)
  2267.    return Matrix
  2268. }
  2269.  
  2270. Gdip_CreateMatrix()
  2271. {
  2272.    DllCall("gdiplus\GdipCreateMatrix", A_PtrSize ? "UPtr*" : "UInt*", Matrix)
  2273.    return Matrix
  2274. }
  2275.  
  2276. ;#####################################################################################
  2277. ; GraphicsPath functions
  2278. ;#####################################################################################
  2279.  
  2280. ; Alternate = 0
  2281. ; Winding = 1
  2282. Gdip_CreatePath(BrushMode=0)
  2283. {
  2284.     DllCall("gdiplus\GdipCreatePath", "int", BrushMode, A_PtrSize ? "UPtr*" : "UInt*", Path)
  2285.     return Path
  2286. }
  2287.  
  2288. Gdip_AddPathEllipse(Path, x, y, w, h)
  2289. {
  2290.     return DllCall("gdiplus\GdipAddPathEllipse", A_PtrSize ? "UPtr" : "UInt", Path, "float", x, "float", y, "float", w, "float", h)
  2291. }
  2292.  
  2293. Gdip_AddPathPolygon(Path, Points)
  2294. {
  2295.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  2296.    
  2297.     StringSplit, Points, Points, |
  2298.     VarSetCapacity(PointF, 8*Points0)  
  2299.     Loop, %Points0%
  2300.     {
  2301.         StringSplit, Coord, Points%A_Index%, `,
  2302.         NumPut(Coord1, PointF, 8*(A_Index-1), "float"), NumPut(Coord2, PointF, (8*(A_Index-1))+4, "float")
  2303.     }  
  2304.  
  2305.     return DllCall("gdiplus\GdipAddPathPolygon", Ptr, Path, Ptr, &PointF, "int", Points0)
  2306. }
  2307.  
  2308. Gdip_DeletePath(Path)
  2309. {
  2310.     return DllCall("gdiplus\GdipDeletePath", A_PtrSize ? "UPtr" : "UInt", Path)
  2311. }
  2312.  
  2313. ;#####################################################################################
  2314. ; Quality functions
  2315. ;#####################################################################################
  2316.  
  2317. ; SystemDefault = 0
  2318. ; SingleBitPerPixelGridFit = 1
  2319. ; SingleBitPerPixel = 2
  2320. ; AntiAliasGridFit = 3
  2321. ; AntiAlias = 4
  2322. Gdip_SetTextRenderingHint(pGraphics, RenderingHint)
  2323. {
  2324.     return DllCall("gdiplus\GdipSetTextRenderingHint", A_PtrSize ? "UPtr" : "UInt", pGraphics, "int", RenderingHint)
  2325. }
  2326.  
  2327. ; Default = 0
  2328. ; LowQuality = 1
  2329. ; HighQuality = 2
  2330. ; Bilinear = 3
  2331. ; Bicubic = 4
  2332. ; NearestNeighbor = 5
  2333. ; HighQualityBilinear = 6
  2334. ; HighQualityBicubic = 7
  2335. Gdip_SetInterpolationMode(pGraphics, InterpolationMode)
  2336. {
  2337.    return DllCall("gdiplus\GdipSetInterpolationMode", A_PtrSize ? "UPtr" : "UInt", pGraphics, "int", InterpolationMode)
  2338. }
  2339.  
  2340. ; Default = 0
  2341. ; HighSpeed = 1
  2342. ; HighQuality = 2
  2343. ; None = 3
  2344. ; AntiAlias = 4
  2345. Gdip_SetSmoothingMode(pGraphics, SmoothingMode)
  2346. {
  2347.    return DllCall("gdiplus\GdipSetSmoothingMode", A_PtrSize ? "UPtr" : "UInt", pGraphics, "int", SmoothingMode)
  2348. }
  2349.  
  2350. ; CompositingModeSourceOver = 0 (blended)
  2351. ; CompositingModeSourceCopy = 1 (overwrite)
  2352. Gdip_SetCompositingMode(pGraphics, CompositingMode=0)
  2353. {
  2354.    return DllCall("gdiplus\GdipSetCompositingMode", A_PtrSize ? "UPtr" : "UInt", pGraphics, "int", CompositingMode)
  2355. }
  2356.  
  2357. ;#####################################################################################
  2358. ; Extra functions
  2359. ;#####################################################################################
  2360.  
  2361. Gdip_Startup()
  2362. {
  2363.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  2364.    
  2365.     if !DllCall("GetModuleHandle", "str", "gdiplus", Ptr)
  2366.         DllCall("LoadLibrary", "str", "gdiplus")
  2367.     VarSetCapacity(si, A_PtrSize = 8 ? 24 : 16, 0), si := Chr(1)
  2368.     DllCall("gdiplus\GdiplusStartup", A_PtrSize ? "UPtr*" : "uint*", pToken, Ptr, &si, Ptr, 0)
  2369.     return pToken
  2370. }
  2371.  
  2372. Gdip_Shutdown(pToken)
  2373. {
  2374.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  2375.    
  2376.     DllCall("gdiplus\GdiplusShutdown", Ptr, pToken)
  2377.     if hModule := DllCall("GetModuleHandle", "str", "gdiplus", Ptr)
  2378.         DllCall("FreeLibrary", Ptr, hModule)
  2379.     return 0
  2380. }
  2381.  
  2382. ; Prepend = 0; The new operation is applied before the old operation.
  2383. ; Append = 1; The new operation is applied after the old operation.
  2384. Gdip_RotateWorldTransform(pGraphics, Angle, MatrixOrder=0)
  2385. {
  2386.     return DllCall("gdiplus\GdipRotateWorldTransform", A_PtrSize ? "UPtr" : "UInt", pGraphics, "float", Angle, "int", MatrixOrder)
  2387. }
  2388.  
  2389. Gdip_ScaleWorldTransform(pGraphics, x, y, MatrixOrder=0)
  2390. {
  2391.     return DllCall("gdiplus\GdipScaleWorldTransform", A_PtrSize ? "UPtr" : "UInt", pGraphics, "float", x, "float", y, "int", MatrixOrder)
  2392. }
  2393.  
  2394. Gdip_TranslateWorldTransform(pGraphics, x, y, MatrixOrder=0)
  2395. {
  2396.     return DllCall("gdiplus\GdipTranslateWorldTransform", A_PtrSize ? "UPtr" : "UInt", pGraphics, "float", x, "float", y, "int", MatrixOrder)
  2397. }
  2398.  
  2399. Gdip_ResetWorldTransform(pGraphics)
  2400. {
  2401.     return DllCall("gdiplus\GdipResetWorldTransform", A_PtrSize ? "UPtr" : "UInt", pGraphics)
  2402. }
  2403.  
  2404. Gdip_GetRotatedTranslation(Width, Height, Angle, ByRef xTranslation, ByRef yTranslation)
  2405. {
  2406.     pi := 3.14159, TAngle := Angle*(pi/180)
  2407.  
  2408.     Bound := (Angle >= 0) ? Mod(Angle, 360) : 360-Mod(-Angle, -360)
  2409.     if ((Bound >= 0) && (Bound <= 90))
  2410.         xTranslation := Height*Sin(TAngle), yTranslation := 0
  2411.     else if ((Bound > 90) && (Bound <= 180))
  2412.         xTranslation := (Height*Sin(TAngle))-(Width*Cos(TAngle)), yTranslation := -Height*Cos(TAngle)
  2413.     else if ((Bound > 180) && (Bound <= 270))
  2414.         xTranslation := -(Width*Cos(TAngle)), yTranslation := -(Height*Cos(TAngle))-(Width*Sin(TAngle))
  2415.     else if ((Bound > 270) && (Bound <= 360))
  2416.         xTranslation := 0, yTranslation := -Width*Sin(TAngle)
  2417. }
  2418.  
  2419. Gdip_GetRotatedDimensions(Width, Height, Angle, ByRef RWidth, ByRef RHeight)
  2420. {
  2421.     pi := 3.14159, TAngle := Angle*(pi/180)
  2422.     if !(Width && Height)
  2423.         return -1
  2424.     RWidth := Ceil(Abs(Width*Cos(TAngle))+Abs(Height*Sin(TAngle)))
  2425.     RHeight := Ceil(Abs(Width*Sin(TAngle))+Abs(Height*Cos(Tangle)))
  2426. }
  2427.  
  2428. ; RotateNoneFlipNone   = 0
  2429. ; Rotate90FlipNone     = 1
  2430. ; Rotate180FlipNone    = 2
  2431. ; Rotate270FlipNone    = 3
  2432. ; RotateNoneFlipX      = 4
  2433. ; Rotate90FlipX        = 5
  2434. ; Rotate180FlipX       = 6
  2435. ; Rotate270FlipX       = 7
  2436. ; RotateNoneFlipY      = Rotate180FlipX
  2437. ; Rotate90FlipY        = Rotate270FlipX
  2438. ; Rotate180FlipY       = RotateNoneFlipX
  2439. ; Rotate270FlipY       = Rotate90FlipX
  2440. ; RotateNoneFlipXY     = Rotate180FlipNone
  2441. ; Rotate90FlipXY       = Rotate270FlipNone
  2442. ; Rotate180FlipXY      = RotateNoneFlipNone
  2443. ; Rotate270FlipXY      = Rotate90FlipNone
  2444.  
  2445. Gdip_ImageRotateFlip(pBitmap, RotateFlipType=1)
  2446. {
  2447.     return DllCall("gdiplus\GdipImageRotateFlip", A_PtrSize ? "UPtr" : "UInt", pBitmap, "int", RotateFlipType)
  2448. }
  2449.  
  2450. ; Replace = 0
  2451. ; Intersect = 1
  2452. ; Union = 2
  2453. ; Xor = 3
  2454. ; Exclude = 4
  2455. ; Complement = 5
  2456. Gdip_SetClipRect(pGraphics, x, y, w, h, CombineMode=0)
  2457. {
  2458.    return DllCall("gdiplus\GdipSetClipRect",  A_PtrSize ? "UPtr" : "UInt", pGraphics, "float", x, "float", y, "float", w, "float", h, "int", CombineMode)
  2459. }
  2460.  
  2461. Gdip_SetClipPath(pGraphics, Path, CombineMode=0)
  2462. {
  2463.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  2464.     return DllCall("gdiplus\GdipSetClipPath", Ptr, pGraphics, Ptr, Path, "int", CombineMode)
  2465. }
  2466.  
  2467. Gdip_ResetClip(pGraphics)
  2468. {
  2469.    return DllCall("gdiplus\GdipResetClip", A_PtrSize ? "UPtr" : "UInt", pGraphics)
  2470. }
  2471.  
  2472. Gdip_GetClipRegion(pGraphics)
  2473. {
  2474.     Region := Gdip_CreateRegion()
  2475.     DllCall("gdiplus\GdipGetClip", A_PtrSize ? "UPtr" : "UInt", pGraphics, "UInt*", Region)
  2476.     return Region
  2477. }
  2478.  
  2479. Gdip_SetClipRegion(pGraphics, Region, CombineMode=0)
  2480. {
  2481.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  2482.    
  2483.     return DllCall("gdiplus\GdipSetClipRegion", Ptr, pGraphics, Ptr, Region, "int", CombineMode)
  2484. }
  2485.  
  2486. Gdip_CreateRegion()
  2487. {
  2488.     DllCall("gdiplus\GdipCreateRegion", "UInt*", Region)
  2489.     return Region
  2490. }
  2491.  
  2492. Gdip_DeleteRegion(Region)
  2493. {
  2494.     return DllCall("gdiplus\GdipDeleteRegion", A_PtrSize ? "UPtr" : "UInt", Region)
  2495. }
  2496.  
  2497. ;#####################################################################################
  2498. ; BitmapLockBits
  2499. ;#####################################################################################
  2500.  
  2501. Gdip_LockBits(pBitmap, x, y, w, h, ByRef Stride, ByRef Scan0, ByRef BitmapData, LockMode = 3, PixelFormat = 0x26200a)
  2502. {
  2503.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  2504.    
  2505.     CreateRect(Rect, x, y, w, h)
  2506.     VarSetCapacity(BitmapData, 16+2*(A_PtrSize ? A_PtrSize : 4), 0)
  2507.     E := DllCall("Gdiplus\GdipBitmapLockBits", Ptr, pBitmap, Ptr, &Rect, "uint", LockMode, "int", PixelFormat, Ptr, &BitmapData)
  2508.     Stride := NumGet(BitmapData, 8, "Int")
  2509.     Scan0 := NumGet(BitmapData, 16, Ptr)
  2510.     return E
  2511. }
  2512.  
  2513. ;#####################################################################################
  2514.  
  2515. Gdip_UnlockBits(pBitmap, ByRef BitmapData)
  2516. {
  2517.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  2518.    
  2519.     return DllCall("Gdiplus\GdipBitmapUnlockBits", Ptr, pBitmap, Ptr, &BitmapData)
  2520. }
  2521.  
  2522. ;#####################################################################################
  2523.  
  2524. Gdip_SetLockBitPixel(ARGB, Scan0, x, y, Stride)
  2525. {
  2526.     Numput(ARGB, Scan0+0, (x*4)+(y*Stride), "UInt")
  2527. }
  2528.  
  2529. ;#####################################################################################
  2530.  
  2531. Gdip_GetLockBitPixel(Scan0, x, y, Stride)
  2532. {
  2533.     return NumGet(Scan0+0, (x*4)+(y*Stride), "UInt")
  2534. }
  2535.  
  2536. ;#####################################################################################
  2537.  
  2538. Gdip_PixelateBitmap(pBitmap, ByRef pBitmapOut, BlockSize)
  2539. {
  2540.     static PixelateBitmap
  2541.    
  2542.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  2543.    
  2544.     if (!PixelateBitmap)
  2545.     {
  2546.         if A_PtrSize != 8 ; x86 machine code
  2547.         MCode_PixelateBitmap =
  2548.         (LTrim Join
  2549.         558BEC83EC3C8B4514538B5D1C99F7FB56578BC88955EC894DD885C90F8E830200008B451099F7FB8365DC008365E000894DC88955F08945E833FF897DD4
  2550.         397DE80F8E160100008BCB0FAFCB894DCC33C08945F88945FC89451C8945143BD87E608B45088D50028BC82BCA8BF02BF2418945F48B45E02955F4894DC4
  2551.         8D0CB80FAFCB03CA895DD08BD1895DE40FB64416030145140FB60201451C8B45C40FB604100145FC8B45F40FB604020145F883C204FF4DE475D6034D18FF
  2552.         4DD075C98B4DCC8B451499F7F98945148B451C99F7F989451C8B45FC99F7F98945FC8B45F899F7F98945F885DB7E648B450C8D50028BC82BCA83C103894D
  2553.         C48BC82BCA41894DF48B4DD48945E48B45E02955E48D0C880FAFCB03CA895DD08BD18BF38A45148B7DC48804178A451C8B7DF488028A45FC8804178A45F8
  2554.         8B7DE488043A83C2044E75DA034D18FF4DD075CE8B4DCC8B7DD447897DD43B7DE80F8CF2FEFFFF837DF0000F842C01000033C08945F88945FC89451C8945
  2555.         148945E43BD87E65837DF0007E578B4DDC034DE48B75E80FAF4D180FAFF38B45088D500203CA8D0CB18BF08BF88945F48B45F02BF22BFA2955F48945CC0F
  2556.         B6440E030145140FB60101451C0FB6440F010145FC8B45F40FB604010145F883C104FF4DCC75D8FF45E4395DE47C9B8B4DF00FAFCB85C9740B8B451499F7
  2557.         F9894514EB048365140033F63BCE740B8B451C99F7F989451CEB0389751C3BCE740B8B45FC99F7F98945FCEB038975FC3BCE740B8B45F899F7F98945F8EB
  2558.         038975F88975E43BDE7E5A837DF0007E4C8B4DDC034DE48B75E80FAF4D180FAFF38B450C8D500203CA8D0CB18BF08BF82BF22BFA2BC28B55F08955CC8A55
  2559.         1488540E038A551C88118A55FC88540F018A55F888140183C104FF4DCC75DFFF45E4395DE47CA68B45180145E0015DDCFF4DC80F8594FDFFFF8B451099F7
  2560.         FB8955F08945E885C00F8E450100008B45EC0FAFC38365DC008945D48B45E88945CC33C08945F88945FC89451C8945148945103945EC7E6085DB7E518B4D
  2561.         D88B45080FAFCB034D108D50020FAF4D18034DDC8BF08BF88945F403CA2BF22BFA2955F4895DC80FB6440E030145140FB60101451C0FB6440F010145FC8B
  2562.         45F40FB604080145F883C104FF4DC875D8FF45108B45103B45EC7CA08B4DD485C9740B8B451499F7F9894514EB048365140033F63BCE740B8B451C99F7F9
  2563.         89451CEB0389751C3BCE740B8B45FC99F7F98945FCEB038975FC3BCE740B8B45F899F7F98945F8EB038975F88975103975EC7E5585DB7E468B4DD88B450C
  2564.         0FAFCB034D108D50020FAF4D18034DDC8BF08BF803CA2BF22BFA2BC2895DC88A551488540E038A551C88118A55FC88540F018A55F888140183C104FF4DC8
  2565.         75DFFF45108B45103B45EC7CAB8BC3C1E0020145DCFF4DCC0F85CEFEFFFF8B4DEC33C08945F88945FC89451C8945148945103BC87E6C3945F07E5C8B4DD8
  2566.         8B75E80FAFCB034D100FAFF30FAF4D188B45088D500203CA8D0CB18BF08BF88945F48B45F02BF22BFA2955F48945C80FB6440E030145140FB60101451C0F
  2567.         B6440F010145FC8B45F40FB604010145F883C104FF4DC875D833C0FF45108B4DEC394D107C940FAF4DF03BC874068B451499F7F933F68945143BCE740B8B
  2568.         451C99F7F989451CEB0389751C3BCE740B8B45FC99F7F98945FCEB038975FC3BCE740B8B45F899F7F98945F8EB038975F88975083975EC7E63EB0233F639
  2569.         75F07E4F8B4DD88B75E80FAFCB034D080FAFF30FAF4D188B450C8D500203CA8D0CB18BF08BF82BF22BFA2BC28B55F08955108A551488540E038A551C8811
  2570.         8A55FC88540F018A55F888140883C104FF4D1075DFFF45088B45083B45EC7C9F5F5E33C05BC9C21800
  2571.         )
  2572.         else ; x64 machine code
  2573.         MCode_PixelateBitmap =
  2574.         (LTrim Join
  2575.         4489442418488954241048894C24085355565741544155415641574883EC28418BC1448B8C24980000004C8BDA99488BD941F7F9448BD0448BFA8954240C
  2576.         448994248800000085C00F8E9D020000418BC04533E4458BF299448924244C8954241041F7F933C9898C24980000008BEA89542404448BE889442408EB05
  2577.         4C8B5C24784585ED0F8E1A010000458BF1418BFD48897C2418450FAFF14533D233F633ED4533E44533ED4585C97E5B4C63BC2490000000418D040A410FAF
  2578.         C148984C8D441802498BD9498BD04D8BD90FB642010FB64AFF4403E80FB60203E90FB64AFE4883C2044403E003F149FFCB75DE4D03C748FFCB75D0488B7C
  2579.         24188B8C24980000004C8B5C2478418BC59941F7FE448BE8418BC49941F7FE448BE08BC59941F7FE8BE88BC69941F7FE8BF04585C97E4048639C24900000
  2580.         004103CA4D8BC1410FAFC94863C94A8D541902488BCA498BC144886901448821408869FF408871FE4883C10448FFC875E84803D349FFC875DA8B8C249800
  2581.         0000488B5C24704C8B5C24784183C20448FFCF48897C24180F850AFFFFFF8B6C2404448B2424448B6C24084C8B74241085ED0F840A01000033FF33DB4533
  2582.         DB4533D24533C04585C97E53488B74247085ED7E42438D0C04418BC50FAF8C2490000000410FAFC18D04814863C8488D5431028BCD0FB642014403D00FB6
  2583.         024883C2044403D80FB642FB03D80FB642FA03F848FFC975DE41FFC0453BC17CB28BCD410FAFC985C9740A418BC299F7F98BF0EB0233F685C9740B418BC3
  2584.         99F7F9448BD8EB034533DB85C9740A8BC399F7F9448BD0EB034533D285C9740A8BC799F7F9448BC0EB034533C033D24585C97E4D4C8B74247885ED7E3841
  2585.         8D0C14418BC50FAF8C2490000000410FAFC18D04814863C84A8D4431028BCD40887001448818448850FF448840FE4883C00448FFC975E8FFC2413BD17CBD
  2586.         4C8B7424108B8C2498000000038C2490000000488B5C24704503E149FFCE44892424898C24980000004C897424100F859EFDFFFF448B7C240C448B842480
  2587.         000000418BC09941F7F98BE8448BEA89942498000000896C240C85C00F8E3B010000448BAC2488000000418BCF448BF5410FAFC9898C248000000033FF33
  2588.         ED33F64533DB4533D24533C04585FF7E524585C97E40418BC5410FAFC14103C00FAF84249000000003C74898488D541802498BD90FB642014403D00FB602
  2589.         4883C2044403D80FB642FB03F00FB642FA03E848FFCB75DE488B5C247041FFC0453BC77CAE85C9740B418BC299F7F9448BE0EB034533E485C9740A418BC3
  2590.         99F7F98BD8EB0233DB85C9740A8BC699F7F9448BD8EB034533DB85C9740A8BC599F7F9448BD0EB034533D24533C04585FF7E4E488B4C24784585C97E3541
  2591.         8BC5410FAFC14103C00FAF84249000000003C74898488D540802498BC144886201881A44885AFF448852FE4883C20448FFC875E941FFC0453BC77CBE8B8C
  2592.         2480000000488B5C2470418BC1C1E00203F849FFCE0F85ECFEFFFF448BAC24980000008B6C240C448BA4248800000033FF33DB4533DB4533D24533C04585
  2593.         FF7E5A488B7424704585ED7E48418BCC8BC5410FAFC94103C80FAF8C2490000000410FAFC18D04814863C8488D543102418BCD0FB642014403D00FB60248
  2594.         83C2044403D80FB642FB03D80FB642FA03F848FFC975DE41FFC0453BC77CAB418BCF410FAFCD85C9740A418BC299F7F98BF0EB0233F685C9740B418BC399
  2595.         F7F9448BD8EB034533DB85C9740A8BC399F7F9448BD0EB034533D285C9740A8BC799F7F9448BC0EB034533C033D24585FF7E4E4585ED7E42418BCC8BC541
  2596.         0FAFC903CA0FAF8C2490000000410FAFC18D04814863C8488B442478488D440102418BCD40887001448818448850FF448840FE4883C00448FFC975E8FFC2
  2597.         413BD77CB233C04883C428415F415E415D415C5F5E5D5BC3
  2598.         )
  2599.        
  2600.         VarSetCapacity(PixelateBitmap, StrLen(MCode_PixelateBitmap)//2)
  2601.         Loop % StrLen(MCode_PixelateBitmap)//2      ;%
  2602.             NumPut("0x" SubStr(MCode_PixelateBitmap, (2*A_Index)-1, 2), PixelateBitmap, A_Index-1, "UChar")
  2603.         DllCall("VirtualProtect", Ptr, &PixelateBitmap, Ptr, VarSetCapacity(PixelateBitmap), "uint", 0x40, A_PtrSize ? "UPtr*" : "UInt*", 0)
  2604.     }
  2605.  
  2606.     Gdip_GetImageDimensions(pBitmap, Width, Height)
  2607.    
  2608.     if (Width != Gdip_GetImageWidth(pBitmapOut) || Height != Gdip_GetImageHeight(pBitmapOut))
  2609.         return -1
  2610.     if (BlockSize > Width || BlockSize > Height)
  2611.         return -2
  2612.  
  2613.     E1 := Gdip_LockBits(pBitmap, 0, 0, Width, Height, Stride1, Scan01, BitmapData1)
  2614.     E2 := Gdip_LockBits(pBitmapOut, 0, 0, Width, Height, Stride2, Scan02, BitmapData2)
  2615.     if (E1 || E2)
  2616.         return -3
  2617.  
  2618.     E := DllCall(&PixelateBitmap, Ptr, Scan01, Ptr, Scan02, "int", Width, "int", Height, "int", Stride1, "int", BlockSize)
  2619.    
  2620.     Gdip_UnlockBits(pBitmap, BitmapData1), Gdip_UnlockBits(pBitmapOut, BitmapData2)
  2621.     return 0
  2622. }
  2623.  
  2624. ;#####################################################################################
  2625.  
  2626. Gdip_ToARGB(A, R, G, B)
  2627. {
  2628.     return (A << 24) | (R << 16) | (G << 8) | B
  2629. }
  2630.  
  2631. ;#####################################################################################
  2632.  
  2633. Gdip_FromARGB(ARGB, ByRef A, ByRef R, ByRef G, ByRef B)
  2634. {
  2635.     A := (0xff000000 & ARGB) >> 24
  2636.     R := (0x00ff0000 & ARGB) >> 16
  2637.     G := (0x0000ff00 & ARGB) >> 8
  2638.     B := 0x000000ff & ARGB
  2639. }
  2640.  
  2641. ;#####################################################################################
  2642.  
  2643. Gdip_AFromARGB(ARGB)
  2644. {
  2645.     return (0xff000000 & ARGB) >> 24
  2646. }
  2647.  
  2648. ;#####################################################################################
  2649.  
  2650. Gdip_RFromARGB(ARGB)
  2651. {
  2652.     return (0x00ff0000 & ARGB) >> 16
  2653. }
  2654.  
  2655. ;#####################################################################################
  2656.  
  2657. Gdip_GFromARGB(ARGB)
  2658. {
  2659.     return (0x0000ff00 & ARGB) >> 8
  2660. }
  2661.  
  2662. ;#####################################################################################
  2663.  
  2664. Gdip_BFromARGB(ARGB)
  2665. {
  2666.     return 0x000000ff & ARGB
  2667. }
  2668.  
  2669. ;#####################################################################################
  2670.  
  2671. StrGetB(Address, Length=-1, Encoding=0)
  2672. {
  2673.     ; Flexible parameter handling:
  2674.     if Length is not integer
  2675.     Encoding := Length,  Length := -1
  2676.  
  2677.     ; Check for obvious errors.
  2678.     if (Address+0 < 1024)
  2679.         return
  2680.  
  2681.     ; Ensure 'Encoding' contains a numeric identifier.
  2682.     if Encoding = UTF-16
  2683.         Encoding = 1200
  2684.     else if Encoding = UTF-8
  2685.         Encoding = 65001
  2686.     else if SubStr(Encoding,1,2)="CP"
  2687.         Encoding := SubStr(Encoding,3)
  2688.  
  2689.     if !Encoding ; "" or 0
  2690.     {
  2691.         ; No conversion necessary, but we might not want the whole string.
  2692.         if (Length == -1)
  2693.             Length := DllCall("lstrlen", "uint", Address)
  2694.         VarSetCapacity(String, Length)
  2695.         DllCall("lstrcpyn", "str", String, "uint", Address, "int", Length + 1)
  2696.     }
  2697.     else if Encoding = 1200 ; UTF-16
  2698.     {
  2699.         char_count := DllCall("WideCharToMultiByte", "uint", 0, "uint", 0x400, "uint", Address, "int", Length, "uint", 0, "uint", 0, "uint", 0, "uint", 0)
  2700.         VarSetCapacity(String, char_count)
  2701.         DllCall("WideCharToMultiByte", "uint", 0, "uint", 0x400, "uint", Address, "int", Length, "str", String, "int", char_count, "uint", 0, "uint", 0)
  2702.     }
  2703.     else if Encoding is integer
  2704.     {
  2705.         ; Convert from target encoding to UTF-16 then to the active code page.
  2706.         char_count := DllCall("MultiByteToWideChar", "uint", Encoding, "uint", 0, "uint", Address, "int", Length, "uint", 0, "int", 0)
  2707.         VarSetCapacity(String, char_count * 2)
  2708.         char_count := DllCall("MultiByteToWideChar", "uint", Encoding, "uint", 0, "uint", Address, "int", Length, "uint", &String, "int", char_count * 2)
  2709.         String := StrGetB(&String, char_count, 1200)
  2710.     }
  2711.    
  2712.     return String
  2713. }
Add Comment
Please, Sign In to add comment