/lib/CPictureControl.ahk

http://github.com/Skiouros/Macro · AutoHotKey · 116 lines · 73 code · 5 blank · 38 comment · 15 complexity · 296c9ed389703cad5ff3627ce09ebe3f MD5 · raw file

  1. /*
  2. Class: CPictureControl
  3. A picture control.
  4. This control extends <CControl>. All basic properties and functions are implemented and documented in this class.
  5. */
  6. Class CPictureControl Extends CControl
  7. {
  8. __New(Name, Options, Text, GUINum)
  9. {
  10. base.__New(Name, Options, Text, GUINum)
  11. this.Type := "Picture"
  12. this._.Picture := Text
  13. this._.Insert("Picture", "Text")
  14. this._.Insert("ControlStyles", {Center : 0x200, ResizeImage : 0x40})
  15. this._.Insert("Events", ["Click", "DoubleClick"])
  16. }
  17. /*
  18. Variable: Picture
  19. The picture can be changed by assigning a filename to this property.
  20. If the picture was set by providing a hBitmap in <SetImageFromHBitmap>, this variable will be empty.
  21. Variable: PictureWidth
  22. The width of the currently displayed picture in pixels.
  23. Variable: PictureHeight
  24. The height of the currently displayed picture in pixels.
  25. */
  26. __Get(Name)
  27. {
  28. ;~ global CGUI
  29. if(Name != "GUINum" && !CGUI.GUIList[this.GUINum].IsDestroyed)
  30. {
  31. DetectHidden := A_DetectHiddenWindows
  32. DetectHiddenWindows, On
  33. if(Name = "Picture")
  34. Value := this._.Picture
  35. else if(Name = "PictureWidth" || Name = "PictureHeight")
  36. {
  37. if(!this._.HasKey(Name))
  38. {
  39. pBitmap := Gdip_CreateBitmapFromFile(this._.Picture) ;Note: This does not work if the user specifies icon number or other special properties in picture path since these are handled as separate parameters here.
  40. Gdip_GetImageDimensions(pBitmap, w, h)
  41. this._.PictureWidth := w
  42. this._.PictureHeight := h
  43. Gdip_DisposeImage(pBitmap)
  44. }
  45. Value := this._[Name]
  46. }
  47. if(!DetectHidden)
  48. DetectHiddenWindows, Off
  49. if(Value != "")
  50. return Value
  51. }
  52. }
  53. __Set(Name, Value)
  54. {
  55. ;~ global CGUI
  56. if(!CGUI.GUIList[this.GUINum].IsDestroyed)
  57. {
  58. DetectHidden := A_DetectHiddenWindows
  59. DetectHiddenWindows, On
  60. Handled := true
  61. if(Name = "Picture")
  62. {
  63. Gui, % this.GUINum ":Default"
  64. GuiControl,, % this.ClassNN, %Value%
  65. this._.Picture := Value
  66. this._.Remove("PictureWidth")
  67. this._.Remove("PictureHeight")
  68. }
  69. else
  70. Handled := false
  71. if(!DetectHidden)
  72. DetectHiddenWindows, Off
  73. if(Handled)
  74. return Value
  75. }
  76. }
  77. /*
  78. Function: SetImageFromHBitmap
  79. Sets the image of this control.
  80. Parameters:
  81. hBitamp - The bitmap handle to which the picture of this control is set
  82. */
  83. SetImageFromHBitmap(hBitmap, Path = "")
  84. {
  85. SendMessage, 0x172, 0x0, hBitmap,, % "ahk_id " this.hwnd
  86. DllCall("gdi32\DeleteObject", "PTR", ErrorLevel)
  87. this._.Remove("PictureWidth")
  88. this._.Remove("PictureHeight")
  89. this._.Picture := Path
  90. }
  91. /*
  92. Event: Introduction
  93. To handle control events you need to create a function with this naming scheme in your window class: ControlName_EventName(params)
  94. The parameters depend on the event and there may not be params at all in some cases.
  95. Additionally it is required to create a label with this naming scheme: GUIName_ControlName
  96. GUIName is the name of the window class that extends CGUI. The label simply needs to call CGUI.HandleEvent().
  97. For better readability labels may be chained since they all execute the same code.
  98. Instead of using ControlName_EventName() you may also call <CControl.RegisterEvent> on a control instance to register a different event function name.
  99. Event: Click()
  100. Invoked when the user clicked on the control.
  101. */
  102. HandleEvent(Event)
  103. {
  104. this.CallEvent(Event.GUIEvent = "DoubleClick" ? "DoubleClick" : "Click")
  105. }
  106. }