I wrote a more productive function to create 1 bpp AND bitmask DIB from the specified 32 bpp bitmap.
#Include <GDIPlus.au3>
#Include <WinAPIEx.au3>
If _WinAPI
_GetUDFVersion
() < '3.4' Then
MsgBox(16, 'Error', 'Require WinAPIEx UDF v3.4 or later.')
Exit
EndIf
$sPng = RegRead('HKLM\SOFTWARE\AutoIt v3\AutoIt', 'InstallDir') & '\Examples\GUI\Torus.png'
If @error Then
Exit
EndIf
Dim $Pic[2], $hBitmap[2]
_GDIPlus_Startup()
$hPng = _GDIPlus_ImageLoadFromFile($sPng)
$hBitmap[0] = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hPng)
$Width = _GDIPlus_ImageGetWidth($hPng)
$Height = _GDIPlus_ImageGetHeight($hPng)
_GDIPlus_ImageDispose($hPng)
_GDIPlus_Shutdown()
$hBitmap[1] = __WinAPI
_CreateANDBitmap
($hBitmap[0])
$hForm = GUICreate('MyGUI', $Width * 2, $Height)
GUICtrlCreatePic('', 0, 0, $Width, $Height)
GUICtrlSendMsg(-1, 0x0172, 0, $hBitmap[0])
GUICtrlCreatePic('', $Width, 0, $Width, $Height)
GUICtrlSendMsg(-1, 0x0172, 0, $hBitmap[1])
GUISetState()
$sIcon = FileSaveDialog('Save Icon', @ScriptDir, 'Icon Files (*.ico)', 2 + 16, _WinAPI
_PathStripPath
(_WinAPI
_PathRenameExtension
($sPng, '.ico')), $hForm)
If $sIcon Then
$hIcon = _WinAPI
_CreateIconIndirect
($hBitmap[0], $hBitmap[1])
ConsoleWrite($hIcon & @CR)
If Not @error Then
_WinAPI
_SaveHICONToFile
($sIcon, $hIcon)
_WinAPI_DestroyIcon($hIcon)
EndIf
EndIf
Do
Until GUIGetMsg() = -3
; #FUNCTION# ====================================================================================================================
; Name...........: _WinAPI_CreateANDBitmap
; Description....: Creates an AND bitmask device-independent bitmap (DIB) from the specified bitmap.
; Syntax.........: _WinAPI_CreateANDBitmap ( $hBitmap )
; Parameters.....: $hBitmap - Handle to the bitmap from which to create an AND bitmask DIB.
; Return values..: Success - Handle to the DIB.
; Failure - 0 and sets the @error flag to non-zero.
; Author.........: Yashied
; Modified.......:
; Remarks........: The _WinAPI_CreateANDBitmap() creates a 1 bits-per-pixel AND bitmask DIB from the 32 bits-per-pixel DIB or DDB.
; If the source bitmap is not a 32 bits-per-pixel bitmap, the function fails.
;
; You can use the bitmap returned by this function to create icon by using the _WinAPI_CreateIconIndirect().
;
; When you are finished using the bitmap, destroy it using the _WinAPI_DeleteObject() function.
; Related........:
; Link...........: None
; Example........: Yes
; ===============================================================================================================================
Func _WinAPI
_CreateANDBitmap
($hBitmap)
Local $Ret, $Error, $bProc, $tProc, $tBITMAP, $tDIB, $hDIB = 0, $hTemp = 0
$hBitmap = _WinAPI
_CopyImage
($hBitmap, 0, 0, 0, 0x2000)
If @error Then
Return SetError(1, 0, 0)
EndIf
Do
$Error = 1
$tBITMAP = DllStructCreate($tagBITMAP)
If (Not _WinAPI_GetObject($hBitmap, DllStructGetSize($tBITMAP), DllStructGetPtr($tBITMAP))) Or (DllStructGetData($tBITMAP, 'bmPlanes') <> 1) Or (DllStructGetData($tBITMAP, 'bmBitsPixel') <> 32) Then
ExitLoop
EndIf
$hTemp = _WinAPI_CreateBitmap(DllStructGetData($tBITMAP, 'bmWidth'), DllStructGetData($tBITMAP, 'bmHeight'), 1, 1)
$hDIB = _WinAPI
_CopyImage
($hTemp, 0, 0, 0, 0x2000)
$tDIB = DllStructCreate($tagBITMAP)
If Not _WinAPI_GetObject($hDIB, DllStructGetSize($tDIB), DllStructGetPtr($tDIB)) Then
ExitLoop
EndIf
$bProc = __ANDProc
()
$tProc = DllStructCreate('byte[' & BinaryLen($bProc) & ']')
DllStructSetData($tProc, 1, $bProc)
$Ret = DllCall('user32.dll', 'int', 'CallWindowProc', 'ptr', DllStructGetPtr($tProc), 'ptr', 0, 'uint', 0, 'ptr', DllStructGetPtr($tBITMAP), 'ptr', DllStructGetPtr($tDIB))
If (@error) Or (Not $Ret[0]) Then
ExitLoop
EndIf
$Error = 0
Until 1
If $hTemp Then
_WinAPI_DeleteObject($hTemp)
EndIf
_WinAPI_DeleteObject($hBitmap)
If $Error Then
If $hDIB Then
_WinAPI_DeleteObject($hDIB)
EndIf
$hDIB = 0
EndIf
Return SetError($Error, 0, $hDIB)
EndFunc ;==>_WinAPI_CreateANDBitmap
Func __ANDProc
()
Local $bData = _
'0x555357BA0900000083EC04C70424000000004A75F3837C243C00740431C0EB05' & _
'B80100000021C07562837C244000740431C0EB05B80100000021C0753F8B6C24' & _
'3C837D1400740431C0EB05B80100000021C075198B6C2440837D1400740431C0' & _
'EB05B80100000021C07502EB07B801000000EB0231C021C07502EB07B8010000' & _
'00EB0231C021C07502EB07B801000000EB0231C021C0740731C0E9BA0100008B' & _
'6C24408B5D0489D8BB0800000099F7FB89D3891C24833C240074268B6C24408B' & _
'5D0C8B6C24408B7D042B3C2489F8B90800000099F7F989C729FB4B895C2404EB' & _
'208B6C24408B5D0C8B6C24408B7D0489F8B90800000099F7F989C729FB895C24' & _
'04C744240800000000C744240C00000000C744241000000000C7442414000000' & _
'00C7442418000000008B6C243C8B5D048B6C243C0FAF5D08C1E3024B3B5C2418' & _
'0F8C0A0100008B6C243C8B5D14035C241883C303895C241C8B6C241C807D0000' & _
'741C8B5C24148B7C240CF7DF83C70789F9B801000000D3E009C3895C2414FF44' & _
'240C8B5C240C83FB077E328B6C24408B5D14035C2408895C241C8B5C2414F7D3' & _
'538B6C242058884500FF442408C744240C00000000C744241400000000FF4424' & _
'108B5C24108B6C24408B7D044F39FB7E74837C240C0074328B6C24408B5D1403' & _
'5C2408895C241C8B5C2414F7D3538B6C242058884500FF442408C744240C0000' & _
'0000C744241400000000C7442420010000008B4424043B4424207C218B6C2440' & _
'8B5D14035C2408895C241C8B6C241CC64500FFFF442408FF44242071D5C74424' & _
'100000000083442418040F81D9FEFFFFB801000000EB0231C083C4245F5B5DC2' & _
'1000'
Return Binary($bData)
EndFunc ;==>__ANDProc