/ahkstdlib/samp/gdip_demo.ahk

https://github.com/kirbybear/ahk-libs · AutoHotKey · 93 lines · 34 code · 27 blank · 32 comment · 0 complexity · 92d25acf232fb506f3ced485489e0e6d MD5 · raw file

  1. ; gdi+ ahk tutorial 1 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
  5. #SingleInstance, Force
  6. #NoEnv
  7. SetBatchLines, -1
  8. ; Uncomment if Gdip.ahk is not in your standard library
  9. ;#Include, Gdip.ahk
  10. ; Start gdi+
  11. If !pToken := Gdip_Startup()
  12. {
  13. MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
  14. ExitApp
  15. }
  16. OnExit, Exit
  17. ; Set the width and height we want as our drawing area, to draw everything in. This will be the dimensions of our bitmap
  18. Width := 600, Height := 400
  19. ; Create a layered window (+E0x80000 : must be used for UpdateLayeredWindow to work!) that is always on top (+AlwaysOnTop), has no taskbar entry or caption
  20. Gui, 1: -Caption +E0x80000 +LastFound +OwnDialogs +Owner +AlwaysOnTop
  21. ; Show the window
  22. Gui, 1: Show, NA
  23. ; Get a handle to this window we have created in order to update it later
  24. hwnd1 := WinExist()
  25. ; 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
  26. hbm := CreateDIBSection(Width, Height)
  27. ; Get a device context compatible with the screen
  28. hdc := CreateCompatibleDC()
  29. ; Select the bitmap into the device context
  30. obm := SelectObject(hdc, hbm)
  31. ; Get a pointer to the graphics of the bitmap, for use with drawing functions
  32. G := Gdip_GraphicsFromHDC(hdc)
  33. ; Set the smoothing mode to antialias = 4 to make shapes appear smother (only used for vector drawing and filling)
  34. Gdip_SetSmoothingMode(G, 4)
  35. ; Create a fully opaque red brush (ARGB = Transparency, red, green, blue) to draw a circle
  36. pBrush := Gdip_BrushCreateSolid(0xffff0000)
  37. ; Fill the graphics of the bitmap with an ellipse using the brush created
  38. ; Filling from coordinates (100,50) an ellipse of 200x300
  39. Gdip_FillEllipse(G, pBrush, 100, 50, 200, 300)
  40. ; Delete the brush as it is no longer needed and wastes memory
  41. Gdip_DeleteBrush(pBrush)
  42. ; Create a slightly transparent (66) blue brush (ARGB = Transparency, red, green, blue) to draw a rectangle
  43. pBrush := Gdip_BrushCreateSolid(0x660000ff)
  44. ; Fill the graphics of the bitmap with a rectangle using the brush created
  45. ; Filling from coordinates (250,80) a rectangle of 300x200
  46. Gdip_FillRectangle(G, pBrush, 250, 80, 300, 200)
  47. ; Delete the brush as it is no longer needed and wastes memory
  48. Gdip_DeleteBrush(pBrush)
  49. ; 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
  50. ; So this will position our gui at (0,0) with the Width and Height specified earlier
  51. UpdateLayeredWindow(hwnd1, hdc, 0, 0, Width, Height)
  52. ; Select the object back into the hdc
  53. SelectObject(hdc, obm)
  54. ; Now the bitmap may be deleted
  55. DeleteObject(hbm)
  56. ; Also the device context related to the bitmap may be deleted
  57. DeleteDC(hdc)
  58. ; The graphics may now be deleted
  59. Gdip_DeleteGraphics(G)
  60. Return
  61. ;#######################################################################
  62. Exit:
  63. ; gdi+ may now be shutdown on exiting the program
  64. Gdip_Shutdown(pToken)
  65. ExitApp
  66. Return