C# 控制台应用程序图标

发布于 2024-07-27 23:50:20 字数 60 浏览 1 评论 0原文

有谁知道如何在代码中设置 C# 控制台应用程序的图标(不使用 Visual Studio 中的项目属性)?

Does anyone know how to set a C# console application's icon in the code (not using project properties in Visual Studio)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一个人的旅程 2024-08-03 23:50:20

您可以在项目属性中更改它。

请参阅此 Stack Overflow 文章:是否可以更改 .net 控制台窗口的图标?

总结一下,在 Visual Studio 中右键单击您的项目(而不是解决方案)并选择属性。 在“应用程序”选项卡的底部有一个“图标和清单”部分,您可以在其中更改图标。

You can change it in the project properties.

See this Stack Overflow article: Is it possible to change a console window's icon from .net?

To summarize right click on your project (not the solution) in Visual Studio and select properties. At the bottom of the "Application" tab there is a section for "Icon and manifest" where you can change the icon.

脸赞 2024-08-03 23:50:20

您无法在代码中指定可执行文件的图标 - 它是二进制文件本身的一部分。

如果有任何帮助,您可以从命令行使用 /win32icon: ,但您无法在应用程序的代码中指定它。 不要忘记,大多数时候应用程序图标显示时,您的应用程序根本没有运行!

假设您指的是资源管理器中文件本身的图标。 如果您指的是应用程序运行时的图标,如果您只是双击该文件,我相信它始终只是控制台本身的图标。

You can't specify an executable's icon in code - it's part of the binary file itself.

From the command line you'd use /win32icon:<file> if that's any help, but you can't specify it within the code of the application. Don't forget that most of the time the application's icon is displayed, your app isn't running at all!

That's assuming you mean the icon for the file itself in explorer. If you mean the icon of the application while it's running if you just double-click the file, I believe that will always just be the icon for the console itself.

情绪少女 2024-08-03 23:50:20

这是通过代码更改图标的解决方案:

class IconChanger
{
    public static void SetConsoleIcon(string iconFilePath)
    {
        if (Environment.OSVersion.Platform == PlatformID.Win32NT)
        {
            if (!string.IsNullOrEmpty(iconFilePath))
            {
                System.Drawing.Icon icon = new System.Drawing.Icon(iconFilePath);
                SetWindowIcon(icon);
            }
        }
    }

    public enum WinMessages : uint
    {
        /// <summary>
        /// An application sends the WM_SETICON message to associate a new large or small icon with a window. 
        /// The system displays the large icon in the ALT+TAB dialog box, and the small icon in the window caption. 
        /// </summary>
        SETICON = 0x0080,
    }

    [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, IntPtr lParam);


    private static void SetWindowIcon(System.Drawing.Icon icon)
    {
        IntPtr mwHandle = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
        IntPtr result01 = SendMessage(mwHandle, (int)WinMessages.SETICON, 0, icon.Handle);
        IntPtr result02 = SendMessage(mwHandle, (int)WinMessages.SETICON, 1, icon.Handle);
    }// SetWindowIcon()
}

Here is a solution to change icon by code:

class IconChanger
{
    public static void SetConsoleIcon(string iconFilePath)
    {
        if (Environment.OSVersion.Platform == PlatformID.Win32NT)
        {
            if (!string.IsNullOrEmpty(iconFilePath))
            {
                System.Drawing.Icon icon = new System.Drawing.Icon(iconFilePath);
                SetWindowIcon(icon);
            }
        }
    }

    public enum WinMessages : uint
    {
        /// <summary>
        /// An application sends the WM_SETICON message to associate a new large or small icon with a window. 
        /// The system displays the large icon in the ALT+TAB dialog box, and the small icon in the window caption. 
        /// </summary>
        SETICON = 0x0080,
    }

    [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, IntPtr lParam);


    private static void SetWindowIcon(System.Drawing.Icon icon)
    {
        IntPtr mwHandle = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
        IntPtr result01 = SendMessage(mwHandle, (int)WinMessages.SETICON, 0, icon.Handle);
        IntPtr result02 = SendMessage(mwHandle, (int)WinMessages.SETICON, 1, icon.Handle);
    }// SetWindowIcon()
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文