forked from ChrisS85/CGUI
-
Notifications
You must be signed in to change notification settings - Fork 5
/
CPictureControl.ahk
165 lines (149 loc) · 4.99 KB
/
CPictureControl.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
Class: CPictureControl
A picture control.
This control extends <CControl>. All basic properties and functions are implemented and documented in this class.
*/
Class CPictureControl Extends CControl
{
static registration := CGUI.RegisterControl("Picture", CPictureControl)
Click := new EventHandler()
DoubleClick := new EventHandler()
__New(Name, Options, Text, GUINum)
{
if(Text && DllCall("GetObjectType", "PTR", Text) != (OBJ_BITMAP := 7) && !FileExist(Text)) ;If Text is no bitmap or path, assume that it's an icon and add the matching style
Options .= " +0x3"
if(InStr(FileExist(Text), "D"))
{
Options .= " +0x3"
VarSetCapacity(Path, 260 * 2) ;MAXPATH
Path := Text
Text := DllCall("Shell32\ExtractAssociatedIcon", "Ptr", 0, "Str", Path, "UShortP", lpiIcon, "Ptr")
}
if Text is Number
base.__New(Name, Options, Text, GUINum)
else
base.__New(Name, Options, Text, GUINum)
Gdip_GetImageDimensions(pBitmap, w, h)
this.Type := "Picture"
this._.Insert("Picture", Text)
this._.Insert("ControlStyles", {Center : 0x200, ResizeImage : 0x40})
this._.Insert("Events", ["Click", "DoubleClick"])
}
PostCreate()
{
base.PostCreate()
text := this._.Picture
if text is number
{
this._.Picture := ""
this.SetImageFromHBitmap(text)
}
}
/*
Property: Picture
The picture can be changed by assigning a filename to this property.
If the picture was set by providing a hBitmap in <SetImageFromHBitmap>, this variable will be empty.
Property: PictureWidth
The width of the currently displayed picture in pixels.
Property: PictureHeight
The height of the currently displayed picture in pixels.
*/
__Get(Name)
{
if(Name != "GUINum" && !CGUI.GUIList[this.GUINum].IsDestroyed)
{
DetectHidden := A_DetectHiddenWindows
DetectHiddenWindows, On
if(Name = "Picture")
Value := this._.Picture
else if(Name = "PictureWidth" || Name = "PictureHeight")
{
if(!this._.HasKey(Name))
{
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.
Gdip_GetImageDimensions(pBitmap, w, h)
this._.PictureWidth := w
this._.PictureHeight := h
Gdip_DisposeImage(pBitmap)
}
Value := this._[Name]
}
if(!DetectHidden)
DetectHiddenWindows, Off
if(Value != "")
return Value
}
}
__Set(Name, Params*)
{
if(!CGUI.GUIList[this.GUINum].IsDestroyed)
{
;Fix completely weird __Set behavior. If one tries to assign a value to a sub item, it doesn't call __Get for each sub item but __Set with the subitems as parameters.
Value := Params.Remove()
if(Params.MaxIndex())
{
Params.Insert(1, Name)
Name := Params.Remove()
return (this[Params*])[Name] := Value
}
DetectHidden := A_DetectHiddenWindows
DetectHiddenWindows, On
Handled := true
if(Name = "Picture")
{
Gui, % this.GUINum ":Default"
GuiControl,, % this.ClassNN, %Value%
this._.Picture := Value
this._.Remove("PictureWidth")
this._.Remove("PictureHeight")
}
else
Handled := false
if(!DetectHidden)
DetectHiddenWindows, Off
if(Handled)
return Value
}
}
/*
Function: SetImageFromHBitmap
Sets the image of this control.
Parameters:
hBitamp - The bitmap handle to which the picture of this control is set. Can also be hIcon if the control has a 0x3 style on creation. This style is added automatically when a hIcon is used as Text on creation.
*/
SetImageFromHBitmap(hBitmap, Path = "")
{
if(DllCall("GetObjectType", "PTR", hBitmap) = (OBJ_BITMAP := 7))
{
SendMessage, STM_SETIMAGE := 0x0172, 0, hBitmap,, % "ahk_id " this.hwnd
DllCall("gdi32\DeleteObject", "PTR", ErrorLevel)
}
else
{
SendMessage, STM_SETICON := 0x0170, hBitmap, 0,, % "ahk_id " this.hwnd
DestroyIcon(ErrorLevel)
}
this._.Remove("PictureWidth")
this._.Remove("PictureHeight")
this._.Picture := Path
}
/*
Event: Introduction
There are currently 3 methods to handle control events:
1) Use an event handler. Simply use control.EventName.Handler := "HandlingFunction"
Instead of "HandlingFunction" it is also possible to pass a function reference or a Delegate: control.EventName.Handler := new Delegate(Object, "HandlingFunction")
If this method is used, the first parameter will contain the control object that sent this event.
2) Create a function with this naming scheme in your window class: ControlName_EventName(params)
3) Instead of using ControlName_EventName() you may also call <CControl.RegisterEvent> on a control instance to register a different event function name.
This method is deprecated since event handlers are more flexible.
The parameters depend on the event and there may not be params at all in some cases.
Event: Click()
Invoked when the user clicked on the control.
Event: DoubleClick()
Invoked when the user double-clicked on the control.
*/
HandleEvent(Event)
{
this.CallEvent(Event.GUIEvent = "DoubleClick" ? "DoubleClick" : "Click")
}
}