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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
unit IcoButtons;
interface
uses windows, KOL;
const
AST_NORMAL = 0;
AST_HOVERED = 1;
AST_PRESSED = 2;
type
tGetIconProc = function(action:integer;stat:integer=AST_NORMAL):cardinal;
tActionProc = function(action:integer):integer;
type
pIcoButton = ^tIcoButton;
tIcoButton = object(TControl)
private
function GetGetIconProc:tGetIconProc;
procedure SetGetIconProc (val:tGetIconProc);
procedure SetDoActionProc(val:tActionProc);
procedure SetCheckFlag(val:boolean);
function GetCheckFlag:boolean;
procedure SetAction(val:integer);
function GetAction:integer;
function GetState:integer;
procedure myPaint(Sender: PControl; DC: HDC);
procedure myMouseDown (Sender:PControl; var Mouse:TMouseEventData);
procedure myMouseUp (Sender:PControl; var Mouse:TMouseEventData);
procedure myMouseEnter(Sender: PObj);
procedure myMouseLeave(Sender: PObj);
procedure myCtrlBtnClick(Sender: PObj);
public
procedure RefreshIcon;
property GetIconProc : tGetIconProc read GetGetIconProc write SetGetIconProc;
property DoActionProc: tActionProc write SetDoActionProc;
property AsCheckbox: boolean read GetCheckFlag write SetCheckFlag;
property Action : integer read GetAction write SetAction;
property State : integer read GetState;
end;
function CreateIcoButton(AOwner: PControl; pGetIconProc:tGetIconProc;
pActionProc:tActionProc; action:integer=0; repeattime:integer=0):pIcoButton;
function CreateIcoButtonHandle(AOwner: PControl; pActionProc:tActionProc;
ico_normal:HICON; ico_hovered:HICON=0; ico_pressed:HICON=0;
action:integer=0; repeattime:integer=0):pIcoButton;
implementation
uses messages;
type
pIcoBtnData = ^tIcoBtnData;
tIcoBtnData = record
rptvalue:cardinal;
rpttimer:cardinal;
checking: boolean;
ico_normal :PIcon;
ico_hovered:PIcon;
ico_pressed:PIcon;
active :PIcon; // one of ico_*
Action:integer;
GetIcon : tGetIconProc;
DoAction: tActionProc;
end;
function tIcoButton.GetGetIconProc:tGetIconProc;
begin
result:=pIcoBtnData(CustomData).GetIcon;
end;
procedure tIcoButton.SetGetIconProc(val:tGetIconProc);
begin
pIcoBtnData(CustomData).GetIcon:=val;
end;
procedure tIcoButton.SetDoActionProc(val:tActionProc);
begin
pIcoBtnData(CustomData).DoAction:=val;
end;
procedure tIcoButton.SetCheckFlag(val:boolean);
begin
pIcoBtnData(CustomData).checking:=val;
end;
function tIcoButton.GetCheckFlag:boolean;
begin
result:=pIcoBtnData(CustomData).checking;
end;
procedure tIcoButton.SetAction(val:integer);
begin
pIcoBtnData(CustomData).Action:=val;
end;
function tIcoButton.GetAction:integer;
begin
result:=pIcoBtnData(CustomData).Action;
end;
function tIcoButton.GetState:integer;
begin
with pIcoBtnData(CustomData)^ do
if active=ico_pressed then result:=AST_PRESSED
else if active=ico_hovered then result:=AST_HOVERED
else {if active=ico_normal then}result:=AST_NORMAL;
end;
procedure tIcoButton.myCtrlBtnClick(Sender: PObj);
var
D: PIcoBtnData;
begin
D:=PControl(Sender).CustomData;
if @D.DoAction<>nil then
D.DoAction(D.action);
end;
procedure tIcoButton.myMouseEnter(Sender: PObj);
var
D: PIcoBtnData;
begin
D:=PControl(Sender).CustomData;
if D.ico_hovered<>nil then
begin
D.active:=D.ico_hovered;
PControl(Sender).Update;
// PControl(Sender).Parent.Update; //??
end;
end;
procedure tIcoButton.myMouseLeave(Sender: PObj);
var
D: PIcoBtnData;
begin
D:=PControl(Sender).CustomData;
if D.active=D.ico_hovered then //!!!! for case when mouse button pressed and mouse moved
D.active:=D.ico_normal;
PControl(Sender).Update;
// PControl(Sender).Parent.Update; //??
end;
procedure TimerProc(wnd:HWND;uMsg:uint;idEvent:uint_ptr;dwTime:dword); stdcall;
begin
PControl(IdEvent).OnClick(PControl(IdEvent));
end;
procedure tIcoButton.myMouseDown(Sender:PControl; var Mouse:TMouseEventData);
var
D: PIcoBtnData;
begin
if Mouse.Button<>mbLeft then exit;
D:=Sender.CustomData;
if D.checking then
begin
if D.active=D.ico_pressed then
D.active:=D.ico_normal
else
D.active:=D.ico_pressed;
end
else
begin
if D.ico_pressed<>nil then
D.active:=D.ico_pressed
else
Sender.SetPosition(Sender.Position.X-2,Sender.Position.Y-2);
if D.rptvalue<>0 then
begin
D.rpttimer:=SetTimer(Sender.Handle,dword(Sender),D.rptvalue,@TimerProc);
// D.rpttimer:=SetTimer(Sender.GetWindowHandle,1,D.rptvalue,nil);
end;
end;
Sender.Update;
end;
procedure tIcoButton.myMouseUp(Sender:PControl; var Mouse:TMouseEventData);
var
D: PIcoBtnData;
tp:TPOINT;
begin
if Mouse.Button<>mbLeft then exit;
D:=Sender.CustomData;
if not D.checking then
begin
if D.rpttimer<>0 then
begin
KillTimer(Sender.Handle,D.rpttimer);
D.rpttimer:=0;
end;
if D.ico_pressed<>nil then
begin
tp.X:=Mouse.X;
tp.Y:=Mouse.Y;
// mouse still above button?
if (D.ico_hovered<>nil) and PtInRect(Sender.BoundsRect,tp) then
D.active:=D.ico_hovered
else
D.active:=D.ico_normal;
end
else
Sender.SetPosition(Sender.Position.X+2,Sender.Position.Y+2);
Sender.Update;
end;
end;
procedure Destroy(dummy:PControl;sender:PObj);
var
D: PIcoBtnData;
begin
D:=pIcoButton(sender).CustomData;
D.ico_normal.Free;
if D.ico_hovered<>nil then D.ico_hovered.Free;
if D.ico_pressed<>nil then D.ico_pressed.Free;
if D.rpttimer<>0 then
begin
KillTimer(0,D.rpttimer);
D.rpttimer:=0;
end;
end;
procedure tIcoButton.RefreshIcon;
var
D: PIcoBtnData;
begin
D:=CustomData;
if @D.GetIcon=nil then exit;
D.ico_normal.Handle:=D.GetIcon(D.action,AST_NORMAL);
D.ico_normal.ShareIcon:=true;
if D.ico_hovered<>nil then
begin
D.ico_hovered.Handle:=D.GetIcon(D.action,AST_HOVERED);
D.ico_hovered.ShareIcon:=true;
end;
if D.ico_pressed<>nil then
begin
D.ico_pressed.Handle:=D.GetIcon(D.action,AST_PRESSED);
D.ico_pressed.ShareIcon:=true;
end;
end;
procedure tIcoButton.myPaint(Sender: PControl; DC: HDC);
var
D: PIcoBtnData;
begin
D:=Sender.CustomData;
D.active.Draw(DC,0,0);
end;
function CreateIcoButton(AOwner: PControl; pGetIconProc:tGetIconProc;
pActionProc:tActionProc; action:integer=0; repeattime:integer=0):pIcoButton;
var
ico:HICON;
D: PIcoBtnData;
begin
// first, checking what icons are available
ico:=pGetIconProc(action,AST_NORMAL);
if ico=0 then
begin
result:=nil;
exit;
end;
Result:=pIcoButton(NewBitBtn(AOwner,'',[bboNoBorder,bboNoCaption],glyphOver,0,0));
if result=nil then exit;
Result.LikeSpeedButton.Flat:=true;
Result.Transparent:=true;
GetMem(D,SizeOf(TIcoBtnData));
Result.CustomData:=D;
Result.OnMouseDown :=Result.myMouseDown;
Result.OnMouseUp :=Result.myMouseUp;
Result.OnMouseEnter:=Result.myMouseEnter;
Result.OnMouseLeave:=Result.myMouseLeave;
Result.OnClick :=Result.myCtrlBtnClick;
Result.OnPaint :=Result.myPaint;
Result.AsCheckbox:=false;
Result.action:=action;
D.rptvalue:=repeattime;
D.rpttimer:=0;
Result.DoActionProc:=pActionProc;
Result.GetIconProc :=pGetIconProc;
D.ico_normal:=NewIcon;
D.ico_normal.Handle :=ico;
D.ico_normal.ShareIcon:=true;
D.active:=D.ico_normal;
ico:=D.GetIcon(action,AST_HOVERED);
if ico<>0 then
begin
D.ico_hovered:=NewIcon;
D.ico_hovered.Handle :=ico;
D.ico_hovered.ShareIcon:=true;
end
else
D.ico_hovered:=nil;
ico:=D.GetIcon(action,AST_PRESSED);
if ico<>0 then
begin
D.ico_pressed:=NewIcon;
D.ico_pressed.Handle :=ico;
D.ico_pressed.ShareIcon:=true;
end
else
D.ico_pressed:=nil;
Result.SetSize(16,16);
Result.SetPosition(0,0);
Result.OnDestroy:=TOnEvent(MakeMethod(nil,@DEstroy));
end;
function CreateIcoButtonHandle(AOwner: PControl; pActionProc:tActionProc;
ico_normal:HICON; ico_hovered:HICON=0; ico_pressed:HICON=0;
action:integer=0; repeattime:integer=0):pIcoButton;
var
D: PIcoBtnData;
begin
if ico_normal=0 then
begin
result:=nil;
exit;
end;
Result:=pIcoButton(NewBitBtn(AOwner,'',[bboNoBorder,bboNoCaption],glyphOver,0,0));
if result=nil then exit;
Result.LikeSpeedButton.Flat:=true;
Result.Transparent:=true;
GetMem(D,SizeOf(TIcoBtnData));
Result.CustomData:=D;
Result.OnMouseDown :=Result.myMouseDown;
Result.OnMouseUp :=Result.myMouseUp;
Result.OnMouseEnter:=Result.myMouseEnter;
Result.OnMouseLeave:=Result.myMouseLeave;
Result.OnClick :=Result.myCtrlBtnClick;
Result.OnPaint :=Result.myPaint;
Result.AsCheckbox:=false;
Result.action:=action;
D.rptvalue:=repeattime;
D.rpttimer:=0;
Result.GetIconProc :=nil;
Result.DoActionProc:=pActionProc;
D.ico_normal:=NewIcon;
D.ico_normal.Handle :=ico_normal;
D.ico_normal.ShareIcon:=true;
D.active:=D.ico_normal;
if ico_hovered<>0 then
begin
D.ico_hovered:=NewIcon;
D.ico_hovered.Handle :=ico_hovered;
D.ico_hovered.ShareIcon:=true;
end
else
D.ico_hovered:=nil;
if ico_pressed<>0 then
begin
D.ico_pressed:=NewIcon;
D.ico_pressed.Handle :=ico_pressed;
D.ico_pressed.ShareIcon:=true;
end
else
D.ico_pressed:=nil;
Result.SetSize(16,16);
Result.SetPosition(0,0);
Result.OnDestroy:=TOnEvent(MakeMethod(nil,@Destroy));
end;
end.
|