C# 获取与正在运行的应用程序关联的图标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/304109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 22:24:32  来源:igfitidea点击:

Getting the icon associated with a running application

c#winapi

提问by Brian K. Carroll

Having the window handle for an open application, I'm able to use the GetWindowText function to retrieve the text from the title bar of the app. I would like to take this a step farther and retrieve the icon associated with the same app.

有了打开应用程序的窗口句柄,我就可以使用 GetWindowText 函数从应用程序的标题栏中检索文本。我想更进一步,检索与同一应用程序关联的图标。

How might I go about doing this? I looked through what I thought would be the relevant Win32 API sections, but nothing jumped out at me.

我该怎么做呢?我查看了我认为将是相关的 Win32 API 部分的内容,但没有任何内容让我眼前一亮。

Any pointers would be appreciated.

任何指针将不胜感激。

Thanks in advance!

提前致谢!

采纳答案by Adam Davis

Icon ico = Icon.ExtractAssociatedIcon(theProcess.MainModule.FileName);

From TheSoftwareJedi

来自 TheSoftwareJedi

Initially this appears to be an exact duplicate of How can I get the icon from the executable file only having an instance of it's Process in C#but that one seems to focus largely on how to get it from within it's own self, whereas you may be asking how to get the icon using a program separate from the running process.

最初,这似乎是How can I get the icon from the executable file only have an instance of it's Process in C#的完全重复,但似乎主要集中在如何从它自己的内部获取它,而你可能是询问如何使用与正在运行的进程分开的程序来获取图标。

-Adam

-亚当

回答by tklepzig

You could do the following:

您可以执行以下操作:

[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

[DllImport("user32.dll")]
static extern IntPtr LoadIcon(IntPtr hInstance, IntPtr lpIconName);

[DllImport("user32.dll", EntryPoint = "GetClassLong")]
static extern uint GetClassLong32(IntPtr hWnd, int nIndex);

[DllImport("user32.dll", EntryPoint = "GetClassLongPtr")]
static extern IntPtr GetClassLong64(IntPtr hWnd, int nIndex);

/// <summary>
/// 64 bit version maybe loses significant 64-bit specific information
/// </summary>
static IntPtr GetClassLongPtr(IntPtr hWnd, int nIndex)
{
    if (IntPtr.Size == 4)
        return new IntPtr((long)GetClassLong32(hWnd, nIndex));
    else
        return GetClassLong64(hWnd, nIndex);
}


uint WM_GETICON = 0x007f;
IntPtr ICON_SMALL2 = new IntPtr(2);
IntPtr IDI_APPLICATION = new IntPtr(0x7F00);
int GCL_HICON = -14;

public static Image GetSmallWindowIcon(IntPtr hWnd)
{
    try
    {
        IntPtr hIcon = default(IntPtr);

        hIcon = SendMessage(hWnd, WM_GETICON, ICON_SMALL2, IntPtr.Zero);

        if (hIcon == IntPtr.Zero)
            hIcon = GetClassLongPtr(hWnd, GCL_HICON);

        if (hIcon == IntPtr.Zero)
            hIcon = LoadIcon(IntPtr.Zero, (IntPtr)0x7F00/*IDI_APPLICATION*/);

        if (hIcon != IntPtr.Zero)
            return new Bitmap(Icon.FromHandle(hIcon).ToBitmap(), 16, 16);
        else
            return null;
    }
    catch (Exception)
    {
        return null;
    }
}

This code retrieves the small window icon, which is shown next to the window text in the titlebar.

此代码检索显示在标题栏中窗口文本旁边的小窗口图标。