Advertisement
pastamaker

ballz v1.1

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