【问题标题】:Is there a way to change the console Icon at runtime有没有办法在运行时更改控制台图标
【发布时间】:2010-06-07 02:58:56
【问题描述】:

我对更改显示在 Windows 资源管理器中的 EXE 中的实际图标不感兴趣,而只是更改显示在控制台窗口左上角的图标。我已经在 Visual Studio 项目中设置了图标,并且在 Windows 资源管理器中很好地得到了它,并且该图标也显示在控制台窗口中,我只想能够在运行时在控制台窗口中更改它。即假设我想放置一个图标,显示有新电子邮件或其他内容。

【问题讨论】:

  • 这两个答案对你有用吗?

标签: c# console icons


【解决方案1】:

根据 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);
        }
    }

【讨论】:

  • 这在 Win7 上有效吗?我可以用它来改变说firefox任务栏中的图标吗?
  • 嗨,Noitidart。嗯,是的,我想是的。您将需要调用更多的 Windows api。看看 EnumDesktopWindows 和 GetWindowText。 pinvoke.net/default.aspx/user32/EnumDesktopWindows.html 该页面上有一些示例代码甚至可以调用 GetWindowText,因此看起来所有繁重的工作都已为您完成。在桌面窗口的每次迭代中,您都可以调用 GetWindowText 来获取窗口的标题.. 当你找到你感兴趣的窗口时,一旦你有了它的窗口句柄,你就可以用它来改变窗口的图标。
  • 谢谢@adrian :) 我试过这样做,但 Firefox 不是控制台,对吗?我是 WinAPI 的菜鸟,但仍在尝试这样做:P 或者将 pinvoke'ing 它解决这个问题?
  • 嗨,Noitidart。是的,你说的没错。您需要调用 API SendMessage。例如:SendMessage(yourwnd, WM_SETICON, ICON_SMALL, hIcon) 更改 GUI 应用程序图标。看起来这个 StackOverflow 帖子有你的答案:stackoverflow.com/questions/9199523/…
  • 非常感谢@adrian 为我找到解决方案! :)
【解决方案2】:

由于 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-22
    • 1970-01-01
    • 2011-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-30
    相关资源
    最近更新 更多