有没有办法在运行时更改控制台图标

klu*_*msy 4 c# console icons

我对改变Windows资源管理器中显示的EXE中的实际图标并不感兴趣,只是显示在控制台窗口左上角的图标.我已经在visual studio项目中设置了图标,并且我在Windows资源管理器中很好地获得了它,并且该图标也显示在控制台窗口中,我只是希望能够在运行时在控制台窗口中更改它.也就是说我想要一个图标,显示有新的电子邮件或其他东西.

Oce*_*rop 7

根据Leniel的回答,我想在C#winforms应用程序中执行此操作.他发布的链接是C++ ..如果您想在C#中执行此操作,本质上就是您需要的代码:

[DllImport("kernel32.dll", SetLastError = true)]
static extern bool SetConsoleIcon(IntPtr hIcon);
Run Code Online (Sandbox Code Playgroud)

并称之为:

public static void SetConsoleIcon(System.Drawing.Icon icon)
        {
            SetConsoleIcon(icon.Handle);
        }
Run Code Online (Sandbox Code Playgroud)

我有一个我在winforms应用程序中使用的ConsoleWindow类,它还能够显示控制台窗口.这是全班def

 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);
        }
    }
Run Code Online (Sandbox Code Playgroud)