Advertisement
Guest User

shapes

a guest
May 22nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. ; gdi+ ahk tutorial 2 written by tic (Tariq Porter)
  2. ; Requires Gdip.ahk either in your Lib folder as standard library or using #Include
  3. ;
  4. ; Tutorial to draw a single ellipse and rectangle to the screen, but just the outlines of these shapes
  5.  
  6. #SingleInstance, Force
  7. #NoEnv
  8. SetBatchLines, -1
  9.  
  10. ; Uncomment if Gdip.ahk is not in your standard library
  11. #Include, Gdip.ahk
  12.  
  13. ; Start gdi+
  14. If !pToken := Gdip_Startup()
  15. {
  16. MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
  17. ExitApp
  18. }
  19. OnExit, Exit
  20.  
  21. ; Set the width and height we want as our drawing area, to draw everything in. This will be the dimensions of our bitmap
  22. Width := 600, Height := 400
  23.  
  24. ; Create a layered window (+E0x80000 : must be used for UpdateLayeredWindow to work!) that is always on top (+AlwaysOnTop), has no taskbar entry or caption
  25. Gui, 1: -Caption +E0x80000 +LastFound +AlwaysOnTop +ToolWindow +OwnDialogs
  26.  
  27. ; Show the window
  28. Gui, 1: Show, NA
  29.  
  30. ; Get a handle to this window we have created in order to update it later
  31. hwnd1 := WinExist()
  32.  
  33. ; Create a gdi bitmap with width and height of what we are going to draw into it. This is the entire drawing area for everything
  34. hbm := CreateDIBSection(Width, Height)
  35.  
  36. ; Get a device context compatible with the screen
  37. hdc := CreateCompatibleDC()
  38.  
  39. ; Select the bitmap into the device context
  40. obm := SelectObject(hdc, hbm)
  41.  
  42. ; Get a pointer to the graphics of the bitmap, for use with drawing functions
  43. G := Gdip_GraphicsFromHDC(hdc)
  44.  
  45. ; Set the smoothing mode to antialias = 4 to make shapes appear smother (only used for vector drawing and filling)
  46. Gdip_SetSmoothingMode(G, 4)
  47.  
  48. ; Create a fully opaque red pen (ARGB = Transparency, red, green, blue) of width 3 (the thickness the pen will draw at) to draw a circle
  49. pPen := Gdip_CreatePen(0xffff0000, 3)
  50.  
  51. ; Draw an ellipse into the graphics of the bitmap (this being only the outline of the shape) using the pen created
  52. ; This pen has a width of 3, and is drawing from coordinates (100,50) an ellipse of 200x300
  53. Gdip_DrawEllipse(G, pPen, 100, 50, 200, 300)
  54.  
  55. ; Delete the pen as it is no longer needed and wastes memory
  56. Gdip_DeletePen(pPen)
  57.  
  58. ; Create a slightly transparent (66) blue pen (ARGB = Transparency, red, green, blue) to draw a rectangle
  59. ; This pen is wider than the last one, with a thickness of 10
  60. pPen := Gdip_CreatePen(0x660000ff, 10)
  61.  
  62. ; Draw a rectangle onto the graphics of the bitmap using the pen just created
  63. ; Draws the rectangle from coordinates (250,80) a rectangle of 300x200 and outline width of 10 (specified when creating the pen)
  64. Gdip_DrawRectangle(G, pPen, 250, 80, 300, 200)
  65.  
  66. ; Delete the brush as it is no longer needed and wastes memory
  67. Gdip_DeletePen(pPen)
  68.  
  69. ; Update the specified window we have created (hwnd1) with a handle to our bitmap (hdc), specifying the x,y,w,h we want it positioned on our screen
  70. ; So this will position our gui at (0,0) with the Width and Height specified earlier
  71. UpdateLayeredWindow(hwnd1, hdc, 0, 0, Width, Height)
  72.  
  73.  
  74. ; Select the object back into the hdc
  75. SelectObject(hdc, obm)
  76.  
  77. ; Now the bitmap may be deleted
  78. DeleteObject(hbm)
  79.  
  80. ; Also the device context related to the bitmap may be deleted
  81. DeleteDC(hdc)
  82.  
  83. ; The graphics may now be deleted
  84. Gdip_DeleteGraphics(G)
  85. Return
  86.  
  87. ;#######################################################################
  88.  
  89. Exit:
  90. ; gdi+ may now be shutdown on exiting the program
  91. Gdip_Shutdown(pToken)
  92. ExitApp
  93. Return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement