根据 Leniel 的回答,我想在 C# winforms 应用程序中执行此操作。他发布的链接是 C++。如果您想在 C# 中执行此操作,基本上这里是您需要的代码:
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool SetConsoleIcon(IntPtr hIcon);
然后这样称呼它:
public static void SetConsoleIcon(System.Drawing.Icon icon)
{
SetConsoleIcon(icon.Handle);
}
我有一个在 winforms 应用程序中使用的 ConsoleWindow 类,它也可以显示控制台窗口。这是完整的课程定义
class ConsoleWindow
{
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool AllocConsole();
[DllImport("kernel32.dll")]
static extern bool AttachConsole(int dwProcessId);
private const int ATTACH_PARENT_PROCESS = -1;
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool SetWindowText(IntPtr hwnd, String lpString);
[DllImport("user32.dll")]
static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
[DllImport("user32.dll")]
static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool SetConsoleIcon(IntPtr hIcon);
const int SW_HIDE = 0;
const int SW_SHOW = 5;
const int SC_CLOSE = 0xF060;
const int MF_GRAYED = 1;
public static void AttachConsoleWindow()
{
// redirect console output to parent process;
// must be before any calls to Console.WriteLine()
AttachConsole(ATTACH_PARENT_PROCESS);
}
public static void ShowConsoleWindow()
{
var handle = GetConsoleWindow();
if (handle == IntPtr.Zero)
{
AllocConsole();
}
else
{
ShowWindow(handle, SW_SHOW);
}
}
public static void HideConsoleWindow()
{
var handle = GetConsoleWindow();
ShowWindow(handle, SW_HIDE);
}
public static void SetWindowText(string text)
{
var handle = GetConsoleWindow();
SetWindowText(handle, text);
}
public static void DisableCloseButton()
{
var handle = GetConsoleWindow();
var hmenu = GetSystemMenu(handle, false);
EnableMenuItem(hmenu, SC_CLOSE, MF_GRAYED);
}
public static void SetConsoleIcon(System.Drawing.Icon icon)
{
SetConsoleIcon(icon.Handle);
}
}