Jump to content

GDI Plus with an exe extracted icon


Recommended Posts

Hi there

Wondering if someone out there could point me in the right direction.

I got this code off the forum and boy it's slick. It's almost exactly what I need but am having difficulties changing the source of the icons to an exe.

How would I say, get the icon from notepad.exe and do this?

Thanks in advance for any help or direction

CODE
#include <GUIConstantsEx.au3>

#include <WindowsConstants.au3>

#include <WinApi.au3>

#include <GDIPlus.au3>

#include <Constants.au3>

$hGui = GUICreate('', 200, 200, -1, -1)

$DeleteIco = GUICtrlCreateIcon("", 0, 10, 10, 32, 32)

GUICtrlSetTip(-1, "Click icon to delete.")

GUISetState(@SW_SHOW, $hGui)

$hIcon1 = _SetTwoIconsToOneIconControl($hGui, $DeleteIco, "shell32.dll", 27, 131)

While 1

$msg = GUIGetMsg()

Switch $msg

Case $GUI_EVENT_CLOSE

If $hIcon1 Then _WinAPI_DestroyIcon($hIcon1)

Exit

Case $DeleteIco

GUICtrlDelete($DeleteIco)

_WinAPI_DestroyIcon($hIcon1)

$hIcon1 = 0

EndSwitch

WEnd

Func _SetTwoIconsToOneIconControl($hWnd, $cID, $sFile, $iName1, $iName2)

Local Const $STM_SETIMAGE = 0x0172

Local $hCID, $tL1, $pL1, $tL2, $pL2, $hImage1, $hGraphic, $hImage2, $hIcon, $hSetIcon

$hCID = GUICtrlGetHandle($cID)

$tL1 = DllStructCreate("int Handle[1]")

$pL1 = DllStructGetPtr($tL1)

$tL2 = DllStructCreate("int Handle[1]")

$pL2 = DllStructGetPtr($tL2)

_WinAPI_ExtractIconEx($sFile, $iName1, $pL1, 0, 1)

_WinAPI_ExtractIconEx($sFile, $iName2, $pL2, 0, 1)

_GDIPlus_Startup()

$hImage1 = DllCall($ghGDIPDll,"int", "GdipCreateBitmapFromHICON", "hWnd", DllStructGetData($tL1, "Handle", 1), "int*", 0)

$hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage1[2])

$hImage2 = DllCall($ghGDIPDll,"int", "GdipCreateBitmapFromHICON", "hWnd", DllStructGetData($tL2, "Handle", 1), "int*", 0)

_GDIPlus_GraphicsDrawImageRect($hGraphic, $hImage2[2], 0, 0, _GDIPlus_ImageGetWidth($hImage1[2]), _GDIPlus_ImageGetHeight($hImage1[2]))

$hIcon = DllCall($ghGDIPDll,"int", "GdipCreateHICONFromBitmap", "hWnd", $hImage1[2], "int*", 0)

_GDIPlus_ImageDispose($hImage2)

_GDIPlus_GraphicsDispose($hGraphic)

_GDIPlus_ImageDispose($hImage1)

_GDIPlus_Shutdown()

$hSetIcon = DllCall("user32.dll", "hwnd", "SendMessage", "hwnd", $hCID, "int", $STM_SETIMAGE, "int", $IMAGE_ICON, "int", $hIcon[2])

_WinAPI_DestroyIcon(DllStructGetData($tL1, "Handle", 1))

_WinAPI_DestroyIcon(DllStructGetData($tL2, "Handle", 1))

If $hSetIcon[0] <> 0 Then _WinAPI_DeleteObject($hSetIcon[0])

Return $hIcon[2]

EndFunc

Link to comment
Share on other sites

Hi there

Wondering if someone out there could point me in the right direction.

I got this code off the forum and boy it's slick. It's almost exactly what I need but am having difficulties changing the source of the icons to an exe.

How would I say, get the icon from notepad.exe and do this?

Thanks in advance for any help or direction

This may give you some ideas.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <WinApi.au3>
#include <GDIPlus.au3>
#include <Constants.au3>

$hGui = GUICreate('', 200, 200, -1, -1)
$DeleteIco = GUICtrlCreateIcon("", 0, 10, 10, 32, 32)
GUICtrlSetTip(-1, "Click icon to delete.")
GUISetState(@SW_SHOW, $hGui)

$hIcon1 = _SetTwoIconsToOneIconControl($hGui, $DeleteIco, "notepad.exe", 0)

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            If $hIcon1 Then _WinAPI_DestroyIcon($hIcon1)
            Exit
        Case $DeleteIco
            GUICtrlDelete($DeleteIco)
            _WinAPI_DestroyIcon($hIcon1)
            $hIcon1 = 0
    EndSwitch
WEnd

Func _SetTwoIconsToOneIconControl($hWnd, $cID, $sFile, $iName1)
    Local Const $STM_SETIMAGE = 0x0172
    Local $hCID, $tL1, $pL1, $tL2, $pL2, $hImage1, $hGraphic, $hImage2, $hIcon, $hSetIcon
    $hCID = GUICtrlGetHandle($cID)
    $tL1 = DllStructCreate("int Handle[1]")
    $pL1 = DllStructGetPtr($tL1)
    $tL2 = DllStructCreate("int Handle[1]")
    $pL2 = DllStructGetPtr($tL2)
    _WinAPI_ExtractIconEx($sFile, $iName1, $pL1, 0, 1)
    _WinAPI_ExtractIconEx("shell32.dll", 131, $pL2, 0, 1)
    _GDIPlus_Startup()
    $hImage1 = DllCall($ghGDIPDll, "int", "GdipCreateBitmapFromHICON", "hWnd", DllStructGetData($tL1, "Handle", 1), "int*", 0)
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage1[2])
    $hImage2 = DllCall($ghGDIPDll, "int", "GdipCreateBitmapFromHICON", "hWnd", DllStructGetData($tL2, "Handle", 1), "int*", 0)
    _GDIPlus_GraphicsDrawImageRect($hGraphic, $hImage2[2], 0, 0, _GDIPlus_ImageGetWidth($hImage1[2]), _GDIPlus_ImageGetHeight($hImage1[2]))
    $hIcon = DllCall($ghGDIPDll, "int", "GdipCreateHICONFromBitmap", "hWnd", $hImage1[2], "int*", 0)
    _GDIPlus_ImageDispose($hImage2)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_ImageDispose($hImage1)
    _GDIPlus_Shutdown()
    $hSetIcon = DllCall("user32.dll", "hwnd", "SendMessage", "hwnd", $hCID, "int", $STM_SETIMAGE, "int", $IMAGE_ICON, "int", $hIcon[2])
    _WinAPI_DestroyIcon(DllStructGetData($tL1, "Handle", 1))
    _WinAPI_DestroyIcon(DllStructGetData($tL2, "Handle", 1))
    If $hSetIcon[0] <> 0 Then _WinAPI_DeleteObject($hSetIcon[0])
    Return $hIcon[2]
EndFunc  ;==>_SetTwoIconsToOneIconControl
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
 Share

  • Recently Browsing   0 members

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