Jump to content
Sign in to follow this  
Yashied

Merging the icons using AutoIt. Any ideas.

Recommended Posts

Yashied

There is a need to merge the two icons. I wrote a function that combines two icons into one, and draws them to the specified background. For my purposes the function works fine for all types of icons (1-bit, 4-bit, etc. and any combinations of them), but would like to see a combined icon had a transparent background.

If you have any ideas on this, please reply.

:)

#include <Constants.au3>
#include <GDIPlus.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

func _DrawCombineBkIcon($iBackground, $sIconBack, $iIndexBack, $sIconFront, $iIndexFront, $iLeft, $iTop, $iWidth, $iHeight, $iStyle = -1, $iExStyle = -1)
    
;    const $DI_NORMAL = 3
    const $STM_SETIMAGE = 0x0172

    local $iIcon, $tIcon, $tID, $hDC, $hBackDC, $hBackSv, $hBitmap, $hImage, $hBackIcon, $hFrontIcon, $aCombineIcon
    local $bError = false

    $tIcon = DllStructCreate('hwnd')
    $tID = DllStructCreate('hwnd')
    DllCall('user32.dll', 'int', 'PrivateExtractIcons', 'str', $sIconFront, 'int', $iIndexFront, 'int', $iWidth, 'int', $iHeight, 'ptr', DllStructGetPtr($tIcon), 'ptr', DllStructGetPtr($tID), 'int', 1, 'int', 0)
    if @error then
        $bError = 1
    endif
    $hFrontIcon = DllStructGetData($tIcon, 1)
    DllCall('user32.dll', 'int', 'PrivateExtractIcons', 'str', $sIconBack, 'int', $iIndexBack, 'int', $iWidth, 'int', $iHeight, 'ptr', DllStructGetPtr($tIcon), 'ptr', DllStructGetPtr($tID), 'int', 1, 'int', 0)
    if @error then
        $bError = 1
    endif
    $hBackIcon = DllStructGetData($tIcon, 1)
    $tIcon = 0
    $tID = 0

    if ($bError) or (not IsPtr($hFrontIcon)) or (not IsPtr($hBackIcon)) then
        return SetError(1, 0, 0)
    endif
    
    $hDC = _WinAPI_GetDC(0)
    $hBackDC = _WinAPI_CreateCompatibleDC($hDC)
    $hBitmap = _WinAPI_CreateSolidBitmap(0, $iBackground, $iWidth, $iHeight)
    $hBackSv = _WinAPI_SelectObject($hBackDC, $hBitmap)
    _WinAPI_DrawIconEx($hBackDC, 0, 0, $hBackIcon, 0, 0, 0, 0, $DI_NORMAL)
    _WinAPI_DrawIconEx($hBackDC, 0, 0, $hFrontIcon, 0, 0, 0, 0, $DI_NORMAL)
    
    _GDIPlus_Startup()
    
    $hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap)
    $aCombineIcon = DllCall($ghGDIPDll, 'int', 'GdipCreateHICONFromBitmap', 'hWnd', $hImage, 'int*', 0)
    _GDIPlus_ImageDispose($hImage)
    
    $iIcon = GUICtrlCreateIcon('', 0, $iLeft, $iTop, $iWidth, $iHeight, $iStyle, $iExStyle)
    if $iIcon > 0 then
        GUICtrlSendMsg($iIcon, $STM_SETIMAGE, $IMAGE_ICON, _WinAPI_CopyIcon($aCombineIcon[2]))
    endif
    
    _GDIPlus_Shutdown()
    
    _WinAPI_SelectObject($hBackDC, $hBackSv)
    _WinAPI_DeleteDC($hBackDC)
    _WinAPI_ReleaseDC(0, $hDC)
    _WinAPI_DeleteObject($aCombineIcon[2])
    _WinAPI_DeleteObject($hBitmap)
    _WinAPI_DeleteObject($hFrontIcon)
    _WinAPI_DeleteObject($hBackIcon)

    return SetError(0, 0, $iIcon)
endfunc; _DrawCombineBkIcon

DrawCombineBkIcon.au3

Edited by Yashied

Share this post


Link to post
Share on other sites
Yashied

If you are interested, here is a simple example for _DrawCombineBkIcon function.

:)

#include <GUIConstantsEx.au3>
#include <DrawCombineBkIcon.au3>

local $Form

$Form = GUICreate('Test', 128, 128)
GUISetState()
_DrawCombineBkIcon(_GUIGetBkColor($Form), 'shell32.dll', 0, 'shell32.dll', 28, 48, 48, 32, 32)

do
    Sleep(10)
until GUIGetMsg() = $GUI_EVENT_CLOSE

func _GUIGetBkColor($hWnd)
    
    local $hDC, $BkClr
    
    $hDC = _WinAPI_GetDC($hWnd)
    $BkClr = _WinAPI_SetBkColor($hDC, 0)
    _WinAPI_ReleaseDC($hWnd, $hDC)
    return $BkClr
endfunc; _GUIGetBkColor
Edited by Yashied

Share this post


Link to post
Share on other sites
ResNullius

Very nice Yashied!

Looks like you're contributing some great scripts.

Welcome to the community.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.