C# 控制台应用程序图标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1170383/
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
C# console application icon
提问by kal3v
Does anyone know how to set a C# console application's icon in the code (not using project properties in Visual Studio)?
有谁知道如何在代码中设置 C# 控制台应用程序的图标(不使用 Visual Studio 中的项目属性)?
采纳答案by Jon Skeet
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!
/win32icon:<file>
如果有任何帮助,您可以从命令行使用,但您不能在应用程序的代码中指定它。不要忘记,大部分时间显示应用程序的图标,您的应用程序根本没有运行!
That's assuming you mean the icon for the file itself in explorer. If you mean the icon of the application while it's runningif you just double-click the file, I believe that will always just be the icon for the console itself.
假设您指的是资源管理器中文件本身的图标。如果您指的是应用程序运行时的图标,只要双击该文件,我相信那将始终只是控制台本身的图标。
回答by IGadget
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?
请参阅这篇 Stack Overflow 文章: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.
总结一下,在 Visual Studio 中右键单击您的项目(不是解决方案)并选择属性。在“应用程序”选项卡的底部有一个“图标和清单”部分,您可以在其中更改图标。
回答by JoScha
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()
}