-
Notifications
You must be signed in to change notification settings - Fork 72
/
ImageList.ahk
666 lines (546 loc) · 18 KB
/
ImageList.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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
/*
class: ImageList
wraps the *IImageList* interface and exposes methods that manipulate and interact with image lists.
Authors:
- maul.esel (https://github.com/maul-esel)
License:
- *LGPL* (http://www.gnu.org/licenses/lgpl-2.1.txt)
Documentation:
- *class documentation* (http://maul-esel.github.com/COM-Classes/master/ImageList)
- *msdn* (http://msdn.microsoft.com/en-us/library/windows/desktop/bb761490)
Requirements:
AutoHotkey - AHK v2 alpha
OS - Windows XP / Windows Server 2003 or higher
Base classes - _CCF_Error_Handler_, Unknown
Constant classes - IDC, IDI, ILIF, ILD, OBM
Structure classes - IMAGELISTDRAWPARAMS, IMAGEINFO, POINT, RECT
Other classes - CCFramework
Remarks:
- to get a HBITMAP or a HICON, use a DllCall to LoadImage, LoadBitmap, LoadIcon, LoadCursor, ...
- the HIMAGELIST (e.g. for LV_SetImageList() or IL_xxx functions) handle can be obtained using instance.ptr
*/
class ImageList extends Unknown
{
/*
Field: CLSID
This is CLSID_ImageList. It is required to create an instance.
*/
static CLSID := "{7C476BA2-02B1-48f4-8048-B24619DDC058}"
/*
Field: IID
This is IID_IImageList. It is required to create an instance.
*/
static IID := "{46EB5926-582E-4017-9FDF-E8998DAA0950}"
/*
Field: hModule
The module handle to the Comctl32 library, as returned by LoadLibrary()
*/
static hModule := DllCall("LoadLibrary", "str", "Comctl32.dll", "UPtr")
/*
Field: ThrowOnCreation
indicates that attempting to create an instance of this class without supplying a valid pointer should throw an exception.
*/
static ThrowOnCreation := true
/*
group: Constructors
Method: FromHIMAGELIST
creates a new instance for a given HIMAGELIST handle.
Parameters:
[opt] HIMAGELIST il - the handle to the image list as returned by IL_Create(). If omitted, a new image list is created.
Remarks:
You cannot create an instance using the usual way:
> myIL := new ImageList()
This throws an exception. You must create an instance from this method:
> myIL := ImageList.FromHIMAGELIST(IL_CREATE())
The given handle can be obtained using
> handle := myIL.ptr
*/
FromHIMAGELIST(il := 0)
{
local iid, mem, ptr
if (!il)
il := IL_Create()
VarSetCapacity(mem, 16, 00), iid := CCFramework.String2GUID(ImageList.IID, &mem)
DllCall("Comctl32.dll\HIMAGELIST_QueryInterface", "ptr", il, "UPtr", iid, "ptr*", ptr)
return new ImageList(ptr)
}
/*
group: IImageList
Method: Add
adds a bitmap image to an ImageList instance.
Parameters:
HBITMAP bitmap - the bitmap to add
[opt] HBITMAP maskbitmap - the bitmap to use as a mask
Returns:
INT index - the new (zero-based) index of the image
Remarks:
IImageList::Add copies the bitmap to an internal data structure.
You must use the DeleteObject function to delete bitmap and maskbitmap when you don't need them anymore:
> DllCall("Gdi32\DeleteObject", "uint", bitmap)
*/
Add(bitmap, maskbitmap := 0)
{
local int
this._Error(DllCall(NumGet(this.vt+3*A_PtrSize), "ptr", this.ptr, "uint", bitmap, "uint", maskbitmap, "int*", int))
return int
}
/*
Method: ReplaceIcon
replaces an icon in the image list or adds a new one.
Parameters:
HICON hIcon - the icon to add
[opt] INT index - the index of the icon to be replaced. Leave this empty or use -1 to append the icon to the list.
Returns:
INT index - the new image list index of the icon
*/
ReplaceIcon(hIcon, index := -1)
{
local int
this._Error(DllCall(NumGet(this.vt+4*A_PtrSize), "ptr", this.ptr, "int", index, "uint", hIcon, "int*", int))
return int
}
/*
Method: SetOverlayImage
sets the overly image for an image.
To make it visible, you must also call <Draw> and set the fStyle parameter appropriately.
Parameters:
INT image - the zero-based index of the image to work on
INT overlay - the one-based index of the image to set as overlay image
Returns:
BOOL success - true on success, false otherwise
*/
SetOverlayImage(image, overlay)
{
return this._Error(DllCall(NumGet(this.vt+5*A_PtrSize), "ptr", this.ptr, "int", image, "int", overlay))
}
/*
Method: Replace
replaces an image in the image list with a new one
Parameters:
INT index - the image to be replaced
HBITMAP bitmap - the new image
[opt] HBITMAP maskbitmap - the optional mask bitmap for the new image
Returns:
BOOL success - true on success, false otherwise
Remarks:
IImageList::Replace copies the bitmap to an internal data structure.
You must use the DeleteObject function to delete bitmap and maskbitmap when you don't need them anymore:
> DllCall("Gdi32\DeleteObject", "uint", bitmap)
*/
Replace(index, bitmap, maskbitmap := 0)
{
return this._Error(DllCall(NumGet(this.vt+6*A_PtrSize), "ptr", this.ptr, "int", index, "uint", bitmap, "uint", maskbitmap))
}
/*
Method: AddMasked
Adds an image or images to an image list, generating a mask from the specified bitmap.
Parameters:
HBITMAP bitmap - the bitmap to add
UINT color - the mask color (e.g. 0xFF0000)
Returns:
INT index - the new index of the image
Remarks:
IImageList::AddMasked copies the bitmap to an internal data structure.
You must use the DeleteObject function to delete bitmap and color when you don't need them anymore:
> DllCall("Gdi32\DeleteObject", "uint", bitmap)
*/
AddMasked(bitmap, color)
{
local int
this._Error(DllCall(NumGet(this.vt+7*A_PtrSize), "ptr", this.ptr, "uint", bitmap, "uint", color, "int*", int))
return int
}
/*
Method: Draw
Draws an image list item in the specified device context.
Parameters:
IMAGELISTDRAWPARAMS params - either a *pointer* to a valid struct or an instance of the IMAGELISTDRAWPARAMS class, specifying the options.
Returns:
BOOL success - true on success, false otherwise
*/
Draw(params)
{
if (IsObject(params))
params := params.ToStructPtr()
return this._Error(DllCall(NumGet(this.vt+8*A_PtrSize), "ptr", this.ptr, "ptr", params))
}
/*
Method: Remove
Removes an image from an image list.
Parameters:
INT index - the index of the icon to be removed
Returns:
BOOL success - true on success, false otherwise
*/
Remove(index)
{
return this._Error(DllCall(NumGet(this.vt+9*A_PtrSize), "ptr", this.ptr, "int", index))
}
/*
Method: GetIcon
Creates an icon from an image and a mask in an image list.
Parameters:
INT index - the index of the image to use
UINT flags - a combination of flags to be used. You can use the values in the IMAGELISTDRAWFLAGS class and combine them using the "|" operator
Returns:
HICON icon - the generated icon
*/
GetIcon(index, flags)
{
local hIcon
this._Error(DllCall(NumGet(this.vt+10*A_PtrSize), "ptr", this.ptr, "int", index, "uint", flags, "uint*", hIcon))
return hIcon
}
/*
Method: GetImageInfo
gets information about an image
Parameters:
INT index - the index of the image to work on
Returns:
IMAGEINFO info - an IMAGEINFO instance containing the information.
*/
GetImageInfo(index)
{
local info
VarSetCapacity(info, IMAGEINFO.GetRequiredSize(), 0)
this._Error(DllCall(NumGet(this.vt+11*A_PtrSize), "ptr", this.ptr, "int", index, "ptr", &info))
return IMAGEINFO.FromStructPtr(&info)
}
/*
Method: Copy
Copies images from a given ImageList instance.
Parameters:
INT iDest - the index the image should be copied to
INT iSrc - the index of the source image
UINT flags - a flag specifying how to copy the image. You might use the fields of the ILCF class for convenience.
Remarks:
*NOT WORKING!*
*/
Copy(iDest, iSrc, flags)
{
return this._Error(DllCall(NumGet(this.vt+12*A_PtrSize), "ptr", this.ptr, "int", iDest, "ptr", this.QueryInterface(Unknown.IID), "int", iSrc, "uint", flags))
}
/*
Method: Merge
Creates a new image by combining two existing images. This method also creates a new image list in which to store the image.
Remarks:
*NOT WORKING!*
*/
Merge(index1, index2, xoffset, yoffset, punk2)
{
local out, mem
VarSetCapacity(mem, 16, 00)
if this._Error(DllCall(NumGet(this.vt+13*A_PtrSize), "ptr", this.ptr, "int", index1, "ptr", punk2.QueryInterface(Unknown.IID), "int", index2
, "int", xoffset, "int", yoffset, "UPtr", CCFramework.String2GUID(this.IID, &mem), "ptr*", out))
return new ImageList(out)
}
/*
Method: Clone
clones an existing instance.
Returns:
ImageList IL - the new ImageList instance
Remarks:
Changes to the original image list won't be visible to the clone (and the other way round).
*/
Clone()
{
local iid, mem, out
VarSetCapacity(mem, 16, 00), iid := CCFramework.String2GUID(ImageList.IID, &mem)
this._Error(DllCall(NumGet(this.vt+14*A_PtrSize), "ptr", this.ptr, "UPtr", iid, "ptr*", out))
return new ImageList(out)
}
/*
Method: GetImageRect
Gets an image's bounding rectangle.
Parameters:
INT index - the index of the image
Returns:
RECT image - an RECT instance representing the image.
*/
GetImageRect(index)
{
local info
VarSetCapacity(info, RECT.GetRequiredSize(), 0)
this._Error(DllCall(NumGet(this.vt+15*A_PtrSize), "ptr", this.ptr, "int", index, "ptr", &info))
return RECT.FromStructPtr(&info)
}
/*
Method: GetIconSize
Gets the dimensions of images in an image list. All images in an image list have the same dimensions.
Parameters:
byref INT width - receives the width
byref INT height - receives the height
Returns:
BOOL success - true on success, false otherwise
*/
GetIconSize(ByRef width, ByRef height)
{
return this._Error(DllCall(NumGet(this.vt+16*A_PtrSize), "ptr", this.ptr, "int*", width, "int*", height))
}
/*
Method: SetIconSize
Sets the dimensions of images in an image list and removes all images from the list.
Parameters:
INT width - the new width
INT height - the new height
Returns:
BOOL success - true on success, false otherwise
*/
SetIconSize(width, height)
{
return this._Error(DllCall(NumGet(this.vt+17*A_PtrSize), "ptr", this.ptr, "int", width, "int", height))
}
/*
Method: GetImageCount
Gets the number of images in an image list.
Returns:
INT count - the count of images
*/
GetImageCount()
{
local count
this._Error(DllCall(NumGet(this.vt+18*A_PtrSize), "ptr", this.ptr, "int*", count))
return count
}
/*
Method: SetImageCount
Resizes an existing image list.
Parameters:
INT count - the new image count
Returns:
BOOL success - true on success, false otherwise
Remarks:
- if you "cut" the image list, the last icons are removed.
- if you enlarge it, the new images will be filled black.
- if you cut and re-enlarge it, the cutted images will be present again.
*/
SetImageCount(count)
{
return this._Error(DllCall(NumGet(this.vt+19*A_PtrSize), "ptr", this.ptr, "uint", count))
}
/*
Method: SetBkColor
Sets the background color for an image list.
Parameters:
UINT color - the new color (e.g. 0x00FFFF)
Returns:
UINT old - the previous background color
Remarks:
This method only functions if you add an icon to the image list or use the IImageList::AddMasked method to add a black and white bitmap.
Without a mask, the entire image draws, and the background color is not visible.
*/
SetBkColor(color)
{
local oldColor
this._Error(DllCall(NumGet(this.vt+20*A_PtrSize), "ptr", this.ptr, "uint", color, "uint*", oldColor))
return oldColor
}
/*
Method: GetBkColor
Gets the current background color for an image list.
Returns:
UINT color - the background color
*/
GetBkColor()
{
local color
this._Error(DllCall(NumGet(this.vt+21*A_PtrSize), "ptr", this.ptr, "uint*", color))
return color
}
/*
Method: BeginDrag
Begins dragging an image.
Parameters:
INT index - the image to drag
INT xHotspot - the x-component of the drag position relative to the upper-left corner of the image
INT yHotspot - the y-component of the drag position relative to the upper-left corner of the image.
Returns:
BOOL success - true on success, false otherwise
*/
BeginDrag(index, xHotspot, yHotspot)
{
return this._Error(DllCall(NumGet(this.vt+22*A_PtrSize), "ptr", this.ptr, "int", index, "int", xHotspot, "int", yHotspot))
}
/*
Method: EndDrag
Ends a drag operation.
Returns:
BOOL success - true on success, false otherwise
*/
EndDrag()
{
return this._Error(DllCall(NumGet(this.vt+23*A_PtrSize), "ptr", this.ptr))
}
/*
Method: DragEnter
Locks updates to the specified window during a drag operation and displays the drag image at the specified position within the window.
Parameters:
HWND hwnd - the window handle
INT x - The x-coordinate at which to display the drag image. The coordinate is relative to the upper-left corner of the window, not the client area.
INT y - The y-coordinate at which to display the drag image. The coordinate is relative to the upper-left corner of the window, not the client area.
Returns:
BOOL success - true on success, false otherwise
*/
DragEnter(hwnd, x, y)
{
return this._Error(DllCall(NumGet(this.vt+24*A_PtrSize), "ptr", this.ptr, "uint", hwnd, "int", x, "int", y))
}
/*
Method: DragLeave
Unlocks the specified window and hides the drag image, which enables the window to update.
Parameters:
HWND hwnd - the window handle
Returns:
BOOL success - true on success, false otherwise
*/
DragLeave(hwnd)
{
return this._Error(DllCall(NumGet(this.vt+25*A_PtrSize), "ptr", this.ptr, "uint", hwnd))
}
/*
Method: DragMove
Moves the image that is being dragged during a drag-and-drop operation.
This method is typically called in response to a WM_MOUSEMOVE message.
Parameters:
INT x - the image's new x-coordinate relative to the upper-left corner of the window
INT y - the image's new y-coordinate relative to the upper-left corner of the window
Returns:
BOOL success - true on success, false otherwise
*/
DragMove(x, y)
{
return this._Error(DllCall(NumGet(this.vt+26*A_PtrSize), "ptr", this.ptr, "int", x, "int", y))
}
/*
Method: SetDragCursorImage
Creates a new drag image by combining the specified image, which is typically a mouse cursor image, with the current drag image.
Parameters:
INT index - the index of the image
INT xHotspot - contains the x-component of the hot spot within the new image.
INT yHotspot - contains the x-component of the hot spot within the new image.
ImageList il - the ImageList that contains the specified image.
Returns:
BOOL success - true on success, false otherwise
*/
SetDragCursorImage(index, xHotspot, yHotspot, il)
{
return this._Error(DllCall(NumGet(this.vt+27*A_PtrSize), "ptr", this.ptr, "ptr", il.QueryInterface(Unknown.IID)
, "int", index, "int", xHotspot, "int", yHotspot))
}
/*
Method: DragShowNoLock
Shows or hides the image being dragged.
Parameters:
BOOL show - true to show, false to hide the image
Returns:
BOOL success - true on success, false otherwise
*/
DragShowNoLock(show)
{
return this._Error(DllCall(NumGet(this.vt+28*A_PtrSize), "ptr", this.ptr, "uint", show))
}
/*
Method: GetDragImage
Gets the temporary image list that is used for the drag image.
The method also retrieves the current drag position and the offset of the drag image relative to the drag position.
Parameters:
byref POINT dragPos - receives a POINT instance representing the current dragging position
byref POINT imagePos - receives a POINT instance representing the current image position
byref ImageList IL - receives an instance for the image list used for the drag image.
Returns:
BOOL success - true on success, false otherwise
*/
GetDragImage(byRef dragPos, byRef imagePos, byRef IL)
{
local mem, iid, pt1, pt2, out, bool
VarSetCapacity(pt1, POINT.GetRequiredSize(), 0), VarSetCapacity(pt2, POINT.GetRequiredSize(), 0)
, VarSetCapacity(mem, 16, 00), iid := CCFramework.String2GUID(this.IID, &mem)
bool := this._Error(DllCall(NumGet(this.vt+29*A_PtrSize), "ptr", this.ptr, "ptr", &pt1, "ptr", &pt2, "ptr", iid, "ptr", out))
dragPos := POINT.FromStructPtr(&pt1)
imagePos := POINT.FromStructPtr(&pt2)
IL := new ImageList(out)
return bool
}
/*
Method: GetItemFlags
Gets the flags of an image.
Parameters:
INT index - the image index
Returns:
UINT flags - the image's flags. You may use the ILIF class for convenience.
Remarks:
possible flag values:
ILIF.ALPHA - Indicates that the item in the imagelist has an alpha channel.
ILIF.LOWQUALITY - **Windows Vista and later.** Indicates that the item in the imagelist was generated via a StretchBlt method, consequently image quality may have degraded.
*/
GetItemFlags(index)
{
local flags
this._Error(DllCall(NumGet(this.vt+30*A_PtrSize), "ptr", this.ptr, "int", index, "uint*", flags))
return flags
}
/*
Method: GetOverlayImage
Retrieves a specified image from the list of images used as overlay masks.
Parameters:
INT index - the image index
Returns:
INT overlay - the one-based index of the overlay mask
*/
GetOverlayImage(index)
{
local out
this._Error(DllCall(NumGet(this.vt+31*A_PtrSize), "ptr", this.ptr, "int", index, "int*", out))
return out
}
/*
group: additional methods
Method: AddSystemBitmap
adds a system bitmap to the image list.
Parameters:
UINT bmp - the ID of a predefined system bitmap. You can use the fields of the OBM class for convenience.
Returns:
INT index - the new (zero-based) index of the image´
*/
AddSystemBitmap(bmp)
{
return this.Add(DllCall("LoadBitmapW", "uint", 0, "uint", bmp))
}
/*
Method: AddSystemIcon
adds a system icon to the image list.
Parameters:
UINT ico - the ID of a predefined system icon. You can use the fields of the IDI class for convenience.
Returns:
INT index - the new (zero-based) index of the image
*/
AddSystemIcon(ico)
{
return this.ReplaceIcon(DllCall("LoadIconW", "uint", 0, "uint", ico))
}
/*
Method: AddSystemCursor
adds a system cursor to the image list.
Parameters:
UINT cur - the ID of a predefined system cursor. You can use the fields of the IDC class for convenience.
Returns:
INT index - the new (zero-based) index of the image
*/
AddSystemCursor(cur)
{
return this.ReplaceIcon(DllCall("LoadCursorW", "uint", 0, "uint", cur))
}
/*
Method: Unload
unloads Comctl32.dll
Returns:
BOOL success - true on success, false otherwise
*/
Unload()
{
local hM
hM := ImageList.hModule
ImageList.hModule := 0
return DllCall("FreeLibrary", "UPtr", hM)
}
}