CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: How to get window's icon via winapi ?

Hybrid View

  1. #1
    Join Date
    Nov 2009

    How to get window's icon via winapi ?


    I am not sure this is the right forum, but since I'm more of c# developer I've decided to give a try.
    Basicaly what I'm trying to to is to to get window's icon (pointer, bytearray...) via its handle(intptr) (needles to say that the handles are to windows created by other processes and not by mine application so form.icon won't cut it ). I've stumbled upon this winapi function

    static extern bool GetClassInfo(IntPtr hInstance, string lpClassName,
    out WNDCLASS lpWndClass);

    but couldn't get its working in .net (the main problem is that one of the parameters is some kind
    of win struct WNDCLASS which I coulnt convert to c# representation).

    So, how do I get these icons ?


  2. #2
    Join Date
    Jul 2008

    Re: How to get window's icon via winapi ?

    Try to get your file icon with this API call, it works with any file name.
    In this case I retrieve the file name of the exe itself, but you can replace
    the file name with any full path you like.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.Reflection;
    namespace GetFileIcon
        public partial class Form1 : Form
            public struct SHFILEINFO
                public IntPtr hIcon;
                public IntPtr iIcon;
                public uint dwAttributes;
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
                public string szDisplayName;
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
                public string szTypeName;
            class Win32
                public const uint SHGFI_ICON = 0x100;
                public const uint SHGFI_LARGEICON = 0x0;
                public const uint SHGFI_SMALLICON = 0x1; 
                public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
            IntPtr hImgSmall;
            SHFILEINFO shinfo = new SHFILEINFO();
            public Form1()
            private void button1_Click(object sender, EventArgs e)
                string file = Application.StartupPath + "\\" + Assembly.GetExecutingAssembly().GetName().Name + ".exe";
                hImgSmall = Win32.SHGetFileInfo(file, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON);
                Icon ico = System.Drawing.Icon.FromHandle(shinfo.hIcon);
                Image img = ico.ToBitmap();
                pictureBox1.Image = imageList1.Images[0];

  3. #3
    Join Date
    Nov 2009

    Re: How to get window's icon via winapi ?

    10x for the answer.
    I'm aware of this option , but there is a possibility that file icon differs from the corresponding window (or even doesn't exist) so this is a limited solution.
    I still rather get the icon from window.

  4. #4
    Join Date
    Jul 2008

    Re: How to get window's icon via winapi ?

    I would suggest to use resources and assign the icon you wish from to any window.
    So the trick is to assign a known icon to the window,
    instead grabbing an unkown icon from the window.

    Sth like this.
    Last edited by MNovy; November 27th, 2009 at 07:26 AM.

Posting Permissions

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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)