Sign in to follow this  
Followers 0
daywalkereg

Extract Icon From Window

8 posts in this topic

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 |\\/|µ(|-|

Share this post


Link to post
Share on other sites



It Is Just Too >_< Complicated :idiot: To WriteIt MySelf

Please Help :)


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

Share this post


Link to post
Share on other sites

bump


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

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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 :)

Edited by daywalkereg

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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Thanx rasim For Your Fast Reply

I Owe You One

Thanx Again


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

Share this post


Link to post
Share on other sites

This is pretty slick. Nice work, lads!

Is there a way to save as either png or gif and maintain transparency?

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  
Followers 0