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

时间:2010-06-07 02:58:57

标签: c# console icons

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

2 个答案:

答案 0 :(得分:7)

根据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类,它还能够显示控制台窗口。这是全班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);
        }
    }

答案 1 :(得分:1)

由于Josh的回答中提到的评论似乎已经消失,所以这里有C ++代码:

HMODULE hKernel32 = ::LoadLibrary(_T("kernel32.dll"));
typedef BOOL (_stdcall * SetConsoleIconFunc)(HICON);
SetConsoleIconFunc setConsoleIcon
    = (SetConsoleIconFunc)::GetProcAddress(hKernel32, "SetConsoleIcon");
if (setConsoleIcon != NULL)
    setConsoleIcon(m_hIcon);
::FreeLibrary(hKernel32);