Is there a way to change the console icon at runtime

I am not interested in changing the actual icon in EXE that appears in Windows Explorer, but only the icon that appears in the upper left corner of the console window. I already installed the icon in the visual studio project, and I understand it well in Windows Explorer, and also that the icon appears in the console window, I just want to be able to change it in the console windows at runtime. Let me say that I wanted to put an icon that shows that there are new emails or something like that.

+4
source share
3 answers

Following Leniel's answer, I wanted to do this in a C # winforms application. The link that he posted is C ++ .. Essentially, here you need the code you need if you want to do it in C #:

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

and name it as follows:

 public static void SetConsoleIcon(System.Drawing.Icon icon) { SetConsoleIcon(icon.Handle); } 

I have a ConsoleWindow class that I use in a winforms application, which also makes it possible to display a console window. Here is the full def class

  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); } } 
+5
source

Since the comment mentioned in Josh's answer seems to be gone, here is the C ++ code:

 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); 
+1
source

In the MSDN documentation for console functions , a comment has been added by a community member discussing an undocumented function called SetConsoleIcon. A quick Google search for this feature brings up additional information that you can use.

0
source

Source: https://habr.com/ru/post/1312042/


All Articles