Results 1 to 11 of 11

Thread: [RESOLVED] Get the Icon from an open window

  1. #1

    Thread Starter
    Fanatic Member ididntdoit's Avatar
    Join Date
    Apr 2006
    Location
    :uoıʇɐɔoן
    Posts
    765

    Resolved [RESOLVED] Get the Icon from an open window

    If I have a Window open (not mine) (I have the hWnd and titlebar caption), how can I get it's icon somehow a) into an imagelist and b) into a file? (just getting a handle to the icon would be enough, I can draw and take it from there)

    I've tried using this code:

    Code:
    Private Declare Function SendMessage Lib "User32.dll" Alias "SendMessageA" ( _
       ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
    Private Declare Function DestroyIcon Lib "User32.dll" (ByVal hIcon As Long) As Long
    Private Declare Function DrawIconEx Lib "User32.dll" (ByVal hDC As Long, _
       ByVal xLeft As Long, ByVal yTop As Long, ByVal hIcon As Long, _
       ByVal cxWidth As Long, ByVal cyWidth As Long, ByVal iStepIfAniCur As Long, _
       ByVal hBrFlickerFreeDraw As Long, ByVal diFlags As Long) As Long
    Private Declare Function FindWindow Lib "User32.dll" Alias "FindWindowA" ( _
       ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    
    Private Const ICON_SMALL As Long = 0
    Private Const ICON_BIG As Long = 1
    Private Const ICON_SMALL2 As Long = 2
    Private Const WM_GETICON As Long = &H7F
    Private Const DI_NORMAL As Long = &H3
    
    Private Sub Form_Load()
         Dim fhWnd As Long
         Dim hIcon As Long
         Dim LoopMode As Long
         
         fhWnd = FindWindow(vbNullString, "Untitled - Notepad")
         
         Me.AutoRedraw = True
         hIcon = SendMessage(fhWnd, WM_GETICON, ICON_SMALL, 0)
         If Not hIcon Then
              hIcon = SendMessage(fhWnd, WM_GETICON, ICON_BIG, 0)
              DrawIconEx Me.hDC, 0, 0, hIcon, 0, 0, 0, 0, DI_NORMAL
              DestroyIcon hIcon
         End If
    End Sub
    but it only works for Internet explorer, no other programs that I can see. Is there some other constant that I should be using, or is there some better way I could do this? I don't really want to use the EXE icon specifically because many programs have multiple icons in their exe and I want the one used in that specific window.

    Any ideas?

    Thanks!
    Visit here to learn to make the VB interface fit you!.
    "I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison
    "The day Microsoft makes something that doesn't suck is probably the day they start making vacuum cleaners" -- Ernst Jan Plugge

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Get the Icon from an open window

    Here is an idea. Per MSDN documentation, it doesn't say a window must respond to WM_GETICON. So, obviously, not all do. It also suggests that a window can farm off the request to its Default Window Procedure. I tried sending the request via a call to DefWindowProc but that didn't work either. Then one more thought came to mind, maybe if WM_GETICON doesn't work you can try to get the handle from the class itself. Once you have the hWnd, call GetClassLong and pass GCL_HICON & see if that works
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Fanatic Member ididntdoit's Avatar
    Join Date
    Apr 2006
    Location
    :uoıʇɐɔoן
    Posts
    765

    Re: Get the Icon from an open window

    Would it change anything if I said I have the window subclassed? Could I do something myself if I have the program subclassed to get the icon out of the top left corner of the screen? I know that the Alt+Tab program built into XP gets an icon for ANY window, so there must be a way...
    Visit here to learn to make the VB interface fit you!.
    "I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison
    "The day Microsoft makes something that doesn't suck is probably the day they start making vacuum cleaners" -- Ernst Jan Plugge

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Get the Icon from an open window

    Is this what you want?

    I have spent almost an entire morning here so I hope that you will look at it...

    Requirements
    1 Form
    1 Commanbutton
    1 PictureBox

    When you run the project and click the commandbutton, please enter the exact name of the window from which you want to retrieve the icon...

    Hope this helps...

    Edit:
    Snapshot attached
    Attached Images Attached Images  
    Attached Files Attached Files
    Last edited by Siddharth Rout; Mar 5th, 2009 at 03:35 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Get the Icon from an open window

    Quote Originally Posted by ididntdoit
    I know that the Alt+Tab program built into XP gets an icon for ANY window, so there must be a way...
    Umm, why not use GetClassLong? That appears to be a perfect solution since it will return either the small or large icon. Using WM_GETICON would be preferred I would think in case the application actually changes its icon during runtime, but if it won't supply the icon with WM_GETICON, GetClassLong should get you something. It worked for me, for windows that didn't respond to WM_GETICON, like NotePad, Spy++, and others.

    Edit: Even if you are subclassing the window, unless you are intercepting and not forwarding WM_GETICON, then the subclassing isn't an issue in this specific scenario.
    Last edited by LaVolpe; Mar 5th, 2009 at 12:42 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Get the Icon from an open window

    Quote Originally Posted by ididntdoit
    but it only works for Internet explorer, no other programs that I can see.
    Not sure this helps, but heres a sub ripped from one of my old apps that got the the icon from a running program for me.

    Code:
    Public Sub AddTask(ByVal hwnd As Long)
        
        Dim hWndIcon     As Long
        Dim hIcon        As Long
        Dim lpdwResult   As Long
        
        hWndIcon = GetWindowLong(hwnd, GWL_HWNDPARENT)
        If hWndIcon = 0 Then hWndIcon = hwnd
        
        ' Get the standard hIcon
        If SendMessageTimeout(hWndIcon, WM_GETICON, 1, ByVal 0&, SMTO_BLOCK, 500, lpdwResult) Then
            If lpdwResult Then
                hIcon = lpdwResult
            Else
                hIcon = GetClassLong(hWndIcon, -14)
            End If
        End If
        
        
        If (hIcon) Then '
            Form1.ProgramIcon(0).Cls
            Call DrawIconEx(Form1.ProgramIcon(0).hdc, 0, 0, hIcon, 13, 13, 0, 0, 3)
            Call DestroyIcon(hIcon)
        End If
    
    End Sub

  7. #7

    Thread Starter
    Fanatic Member ididntdoit's Avatar
    Join Date
    Apr 2006
    Location
    :uoıʇɐɔoן
    Posts
    765

    Talking Re: Get the Icon from an open window

    HAHA! It worked!!! Thanks a million LaVolpe! You rock!!


    EDIT: Whoops - forgot to reload the page before my response :P I didn't see anything since my last post.

    @Coolsid: LaVolpe's suggestion works simpler, so I think I'll go with it, but I can use your code to get the EXE path of a running program (that was another thing I haden't gotten around to trying to do). Thank you for the code!

    @edgemeal: that's the code I originally had I think. As LaVolpe pointed out only certain programs return an icon for WM_GETITCON

    Thanks everyone!
    Last edited by ididntdoit; Mar 5th, 2009 at 01:17 PM.
    Visit here to learn to make the VB interface fit you!.
    "I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison
    "The day Microsoft makes something that doesn't suck is probably the day they start making vacuum cleaners" -- Ernst Jan Plugge

  8. #8
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Get the Icon from an open window

    Can you show me the better code?
    EDIT btw, was just testing my old app (uses code I posted) and noticed a couple icons look ugly, like for a running copy of VB6 the icon doesn't look as good as if I use one of the extract icon from file methods. ???
    Last edited by Edgemeal; Mar 5th, 2009 at 01:39 PM.

  9. #9

    Thread Starter
    Fanatic Member ididntdoit's Avatar
    Join Date
    Apr 2006
    Location
    :uoıʇɐɔoן
    Posts
    765

    Re: [RESOLVED] Get the Icon from an open window

    Code:
    Public Declare Function GetClassLong Lib "user32.dll" Alias "GetClassLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    
    
    Public Declare Function DrawIconEx Lib "user32.dll" (ByVal hDC As Long, ByVal xLeft As Long, ByVal yTop As Long, ByVal hIcon As Long, ByVal cxWidth As Long, ByVal cyWidth As Long, ByVal iStepIfAniCur As Long, ByVal hBrFlickerFreeDraw As Long, ByVal diFlags As Long) As Long
    
    
    private sub form_load()
         Dim i As Long
         i = GetClassLong(ByVal CLng(Mid(Node.key, 5)), GCL_HICON)
         DrawIconEx iPic.hDC, 0, 0, i, 0, 0, 0, 0, DI_NORMAL
    end sub
    Visit here to learn to make the VB interface fit you!.
    "I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison
    "The day Microsoft makes something that doesn't suck is probably the day they start making vacuum cleaners" -- Ernst Jan Plugge

  10. #10
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: [RESOLVED] Get the Icon from an open window

    Oh OK, thought you were doing something different, looks like setting the size of the icon in DrawIconEx is whats making some icons look crappy for me.

  11. #11
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: [RESOLVED] Get the Icon from an open window

    @ididntdoit: Till the time that code helped you... I'm okay with it
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width