2010-06-07 122 views
4

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

+0

你俩这两个答案的工作您? – Josh 2010-06-08 04:35:31

回答

5

上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); 
     } 
    } 
+0

这是否在Win7上工作?我可以用它来改变Firefox的任务栏中的图标吗? – Noitidart 2014-07-04 00:29:33

+0

嗨Noitidart。 呃,是的,我想是的。 你将需要pinvoke更多的Windows API的。 查看EnumDesktopWindows和GetWindowText。 http://www.pinvoke.net/default.aspx/user32/EnumDesktopWindows.html 该页面上有一些示例代码甚至可以调用GetWindowText,因此它看起来像所有的繁重工作都已经完成.. 在桌面窗口的每次迭代中,您都可以调用GetWindowText来获取窗口的标题。 当您找到您感兴趣的窗口后,一旦您有窗口句柄,就可以使用它来更改图标的窗户。 – 2015-01-24 09:09:20

+0

谢谢@adrian :)我试过这样做,但Firefox不是控制台吗?我是WinAPI的noob,仍然试图做到这一点:P还是会拼命解决这个问题? – Noitidart 2015-01-24 14:16:14

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); 
相关问题