Jump to content

Icon Background


Recommended Posts

I am trying to make an icon that looks like its floating. I am using Yashied's Icons.au3. The idea is to retrieve the image from a file (always an icon, at least the way I am doing it). I am currently combining the icons into a single icon so I don't have as many controls to handle. The icons that are extracted from the file have a white background, not a transparent one. Is there a way to make this background transparent, even after combining the icons?

Floating Icon.au3

Edited by dantay9
Link to comment
Share on other sites

If the both icons that you want to combine have an alpha channel, you can do the following trick.

#Include <WinAPI.au3>
#Include <WindowsConstants.au3>

Global Const $AC_SRC_ALPHA = 1

$hBitmap = _GetCombineIconAlpha(@SystemDir & '\shell32.dll', 130, @SystemDir & '\shell32.dll', 29, 48, 48)
_SetBitmap($hForm, $hBitmap, 255)

Until GUIGetMsg() = -3

Func _GetCombineIconAlpha($sIcon1, $iIndex1, $sIcon2, $iIndex2, $iWidth, $iHeight)

    Local $hIcon1 = _WinAPI_ShellExtractIcons($sIcon1, $iIndex1, $iWidth, $iHeight)
    Local $hIcon2 = _WinAPI_ShellExtractIcons($sIcon2, $iIndex2, $iWidth, $iHeight)

    If (Not $hIcon1) Or (Not $hIcon1) Then
        Return SetError(1, 0, 0)

    Local $hBitmap, $hDC, $hMem, $hSv

    $hDC = _WinAPI_GetDC(0)
    $hMem = _WinAPI_CreateCompatibleDC($hDC)
    $hBitmap = _WinAPI_CreateCompatibleBitmap($hDC, $iWidth, $iHeight)
    $hSv = _WinAPI_SelectObject($hMem, $hBitmap)
    _WinAPI_DrawIconEx($hMem, 0, 0, $hIcon1, $iWidth, $iHeight, 0, 0, 3)
    _WinAPI_DrawIconEx($hMem, 0, 0, $hIcon2, $iWidth, $iHeight, 0, 0, 3)
    _WinAPI_ReleaseDC(0, $hDC)
    _WinAPI_SelectObject($hMem, $hSv)
    Return $hBitmap
EndFunc   ;==>_GetCombineIconAlpha

Func _SetBitmap($hWnd, $hBitmap, $iOpacity)

    Local $hScrDC, $hMemDC, $hOld, $tObj, $pSize, $tSize, $pSource, $tSource, $pBlend, $tBlend

    $hScrDC = _WinAPI_GetDC(0)
    $hMemDC = _WinAPI_CreateCompatibleDC($hScrDC)
    $hOld = _WinAPI_SelectObject($hMemDC, $hBitmap)
    $tObj = DllStructCreate($tagBITMAPINFO)
    DllCall('gdi32.dll', 'int', 'GetObject', 'int', $hBitmap, 'int', DllStructGetSize($tObj), 'ptr', DllStructGetPtr($tObj))
    $tSize = DllStructCreate($tagSIZE)
    $pSize = DllStructGetPtr($tSize)
    DllStructSetData($tSize, "X", DllStructGetData($tObj, 'Width'))
    DllStructSetData($tSize, "Y", DllStructGetData($tObj, 'Height'))
    $tSource = DllStructCreate($tagPOINT)
    $pSource = DllStructGetPtr($tSource)
    $tBlend = DllStructCreate($tagBLENDFUNCTION)
    $pBlend = DllStructGetPtr($tBlend)
    DllStructSetData($tBlend, "Alpha", $iOpacity)
    DllStructSetData($tBlend, "Format", $AC_SRC_ALPHA)
    _WinAPI_UpdateLayeredWindow($hWnd, $hScrDC, 0, $pSize, $hMemDC, $pSource, 0, $pBlend, $ULW_ALPHA)
    _WinAPI_ReleaseDC(0, $hScrDC)
    _WinAPI_SelectObject($hMemDC, $hOld)
EndFunc   ;==>_SetBitmap

Func _WinAPI_ShellExtractIcons($sIcon, $iIndex, $iWidth, $iHeight)

    Local $Ret = DllCall('shell32.dll', 'int', 'SHExtractIconsW', 'wstr', $sIcon, 'int', $iIndex, 'int', $iWidth, 'int', $iHeight, 'ptr*', 0, 'ptr*', 0, 'int', 1, 'int', 0)

    If (@error) Or ($Ret[0] = 0) Or ($Ret[5] = Ptr(0)) Then
        Return SetError(1, 0, 0)
    Return $Ret[5]
EndFunc   ;==>_WinAPI_ShellExtractIcons

Func WM_NCHITTEST($hWnd, $iMsg, $iwParam, $ilParam)
    If ($hWnd = $hForm) And ($iMsg = $WM_NCHITTEST) Then
        Return $HTCAPTION
EndFunc   ;==>WM_NCHITTEST
Link to comment
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...