PageRenderTime 105ms CodeModel.GetById 97ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

/lib/CPictureControl.ahk

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