Problem with 32 bits per pixel images (seems) fixed. As a consequence, gdi+ option should now be fine. Special thanks to jeeswg and SKAN, see a few posts down.
Upadate (2017-08-29)
This has been updated to also work for ahk v2 (2.0-a081-cad307c). Same file works on both v1 and v2.
Also adding extended classes: hIconFromhBitmap and hCursorFromhBitmap. See next post.
Introduction
This is for loading a picture and specifying the type of the returned handle, that is, hBitmap, hIcon or hCursor.
Usage
Code: Select all
ref := new LoadPictureType(Filename [, options:="", ImageType:=0, bkColor:=0x000000, xHotspot:=0, yHotspot:=0,keepBITMAP:=false])
The parameter ImageType specifies the type of the returned handle, set it to either IMAGE_BITMAP:=0, IMAGE_ICON:=1 or IMAGE_CURSOR:=2
Most basic example:
Code: Select all
icon:= new LoadPictureType("myBitmap.bmp" ,"w55 h100", IMAGE_ICON:=1)
myIconHandle:=icon.getHandle()
For cursors, you may specify the hotspot coordinates, via the xHotspot and yHotspot parameters.
You can set the keepBITMAP parameter to true if you want to call the ref.getBitmap() method, to obtain the info of a BITMAP structure as an ahk array on the form:
Code: Select all
BITMAP := {type: bmType
,w: bmWidth
,h: bmHeight
,widthBytes: bmWidthBytes
,planes: bmPlanes
,bitsPixel: bmBitsPixel
,bits: bmBits}
Known limitations
-
Sometimes fails if the gdi+ option is present. Edit: This seems to be due to bitmaps being 32 bits per pixel vs. 24 bits. -
Edit: Only seems to work with 24 bits per pixel bitmaps. - Transparency fails on some non-bitmap images.
- This has almost zero value if you want a bitmap handle, then just use LoadPicture.
- Limited testing
I know very little about gdi, I'm sure there is a better way to do this, please tell me.
For reference: I have used the logic found here.
As far a I am concerned, you may use this code in any peaceful context you wish, but remember the above reference. The provided bitmap below is not for sale, it is owned by its author, a professional graphics artist .
github
Download
Example:
Change the Windows cursor, restored on exit (Esc).
Save image as myBmp.bmp and put in script directory.
Cheers.