Set icon of Window (Mogre)        

This snippet is about how to set the icon of a RenderWindow.

For questions use this thread.

[DllImport("user32.dll")]
 private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
 [DllImport("user32.dll")]
 private static extern int DrawMenuBar(int currentWindow); 
 
 private const int WM_SETICON = 0x80;
 private const int ICON_SMALL = 0;
 
 /// <summary>
 /// Sets the icon of a window using it's handle.
 /// </summary>
 /// <param name="icon">The System.Drawing.Icon you wish to use as the window's icon.</param>
 /// <param name="hwnd">The window's handle.</param>
 public static void SetIcon(System.Drawing.Icon icon, IntPtr hwnd)
 {
     if (icon == null || hwnd == null || hwnd == IntPtr.Zero) // check parameters
         return;
     SendMessage(hwnd, WM_SETICON, (IntPtr)ICON_SMALL, (IntPtr)icon.Handle); // Set the icon with SendMessage
     DrawMenuBar((int)hwnd); // Force windows to update the icon
 }

You can get the handle of a RenderWindow instance like this:

IntPtr hwnd = new IntPtr();
 MyRenderWindow.GetCustomAttribute("WINDOW", out hwnd);

Then it's just a matter of

SetIcon(new System.Drawing.Icon("myicon.ico"), hwnd);

Another interesting thing you could do is create an extension method for RenderWindow...

If you know where to get free icons it would be nice if you add the link here (-;

A great place for free icons is Iron Archive