Jump to content

Extract Icon From Window


 Share

Recommended Posts

How I Can Save The $hIcon To File as .ico

#include <WindowsConstants.au3>

Global $GCL_HICONSM = -34
Global $GCL_HICON = -14
Run("Notepad.exe")
$hNotepad = WinGetHandle("[Class:Notepad]")
$hIcon = DllCall("user32.dll", "int", "GetClassLong", "hwnd", $hNotepad, "int", $GCL_HICON)
$hIcon = $hIcon[0]
If $hIcon = 0 Then
    $hIcon = DllCall("user32.dll", "int", "GetClassLong", "hwnd", $hNotepad, "int", $GCL_HICONSM)
    $hIcon = $hIcon[0]
EndIf
DllCall("user32.dll", "int", "SendMessage", "hwnd", $hGUI, "int", $WM_SETICON, "int", 1, "int", $hIcon)
DllCall("User32.dll", "int", "DestroyIcon", "hwnd", $hIcon)

1 £0\\/3 |-|3® $0 |\\/|µ(|-|

Link to comment
Share on other sites

Thanx rasim

But This Didn't Work

Simply Because $hIcon = 27919261

What I Don't Understand What Is This Number Or How To Extract It To Icon File

I am Sure It Is Not Icon ID In The Exe File :)

Please don't send message to PM, post your answers on forum.

Example:

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

Run("Notepad.exe")
WinWait("[Class:Notepad]")
$hNotepad = WinGetHandle("[Class:Notepad]")

$hIcon = _ExtractIconFromWindow($hNotepad)
If $hIcon = 0 Then Exit

_SaveIconAsFile($hIcon, @ScriptDir & "\new_Icon.bmp")

Func _ExtractIconFromWindow($hWnd)
    If Not WinExists($hWnd) Then Return SetError(1, 0, 0)
    
    Local $GCL_HICONSM = -34
    Local $GCL_HICON = -14
    
    Local $hIcon = DllCall("user32.dll", "int", "GetClassLong", "hwnd", $hWnd, "int", $GCL_HICON)

    If $hIcon[0] = 0 Then
        $hIcon = DllCall("user32.dll", "int", "GetClassLong", "hwnd", $hWnd, "int", $GCL_HICONSM)
    EndIf
    
    Return $hIcon[0]
EndFunc   ;==>_ExtractIconFromWindow

Func _SaveIconAsFile($hIcon, $sPath)
    _GDIPlus_Startup()
    
    $pBITMAP = DllStructCreate("hwnd")
    DllCall($ghGDIPDll, "int", "GdipCreateBitmapFromHICON", "hwnd", $hIcon, "ptr", DllStructGetPtr($pBITMAP))
    $hSourceIMG = DllStructGetData($pBITMAP, 1)

    $hBitmap = _CreateBMP(32, 32)
    $hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap)

    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)

    _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hSourceIMG, 0, 0, 32, 32, 0, 0, 32, 32)

    _GDIPlus_ImageSaveToFile($hImage, $sPath)

    _WinAPI_DeleteObject($hBitmap)
    _WinAPI_DeleteObject($hImage)
    _GDIPlus_GraphicsDispose($hGraphic)
    _WinAPI_DestroyIcon($hIcon)
    _GDIPlus_Shutdown()
EndFunc   ;==>_SaveIconAsFile

Func _CreateBMP($sWidth, $sHeight)
    Local $hWnd, $hDC, $hBMP
    
    $hWnd = _WinAPI_GetDesktopWindow()
    $hDC = _WinAPI_GetDC($hWnd)
    $hBMP = _WinAPI_CreateCompatibleBitmap($hDC, $sWidth, $sHeight)
    _WinAPI_ReleaseDC($hWnd, $hDC)
    
    Return $hBMP
EndFunc   ;==>_CreateBMP

And please, don't ask me how to convert bmp image to ico.

Link to comment
Share on other sites

  • 7 months later...

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...