Developing on Staxmanade

Console Icon Swapping – Just for fun…

I took the new logo for StatLight and created a/an .ico (by going to convertico.com) for use as a FavIcon and so I could fancy up a couple of the windows just for fun. What I ended up figuring out, was how to change the icon for a running console window.

It’s actually pretty easy… Here is the class I wrote and use.

internal class ConsoleIconSwapper : IDisposable
{
IntPtr consoleWindowHwnd;

private const int WM_SETICON = 0x80; // Api constant
private const int ICON_SMALL = 0; // Api constant

[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hwnd, int message, int wParam, IntPtr lParam);

[DllImport("kernel32")]
private static extern IntPtr GetConsoleWindow();

public ConsoleIconSwapper()
{
consoleWindowHwnd = GetConsoleWindow();
}

public void ShowConsoleIcon(Icon icon)
{
SendMessage(consoleWindowHwnd, WM_SETICON, ICON_SMALL, icon.Handle);
}

public void ChangeIconback()
{
SendMessage(consoleWindowHwnd, WM_SETICON, ICON_SMALL, (IntPtr)0);
}

public void Dispose()
{
ChangeIconback();
}
}


And here is how I use it.



using (var consoleIconSwapper = new ConsoleIconSwapper())
{
consoleIconSwapper.ShowConsoleIcon(CoreResources.FavIcon);
/*
* Code Here
*/

}


This is what it looks like when in use…



Before:image


During:image


After:image



Amused by simple things :)

Comments

CLS
Thanks man!

It works fine.

Comments