Modify a Window's System Menu

I wanted to keep the SiteWatcher utility as streamlined as possible. There really isn't a need for a cumbersome menu hierarchy or flashy toolbar. But I didn't want to release an application without crediting myself! So I simply added an About SiteWatcher item to the application window's system menu.

Adding an item to the system menu is quite easy once you discover a couple of existing User32 functions. A little Interop... and... voila!

You do have to remember to override the WndProc method so you can inject your own custom handler when the menu item is clicked.

  using System.Runtime.InteropServices;
 
  [DllImport("user32.dll")]
  private static extern int GetSystemMenu (int hwnd, int bRevert);
 
  [DllImport("user32.dll")]
  private static extern int AppendMenu (
    int hMenu,int Flagsw,int IDNewItem,string lpNewItem);
 
  private void SetupSystemMenu ()
  {
    // get handle to system menu
    int menu = GetSystemMenu(this.Handle.ToInt32(),0);
    // add a separator
    AppendMenu(menu,0xA00,0,null);
    // add an item with a unique ID
    AppendMenu(menu,0,1234,"About SiteWatcher");
  }
 
  protected override void WndProc (ref Message m)
  {
    base.WndProc(ref m);
    // WM_SYSCOMMAND is 0x112
    if (m.Msg == 0x112)
    {
      // check for my new menu item ID
      if (m.WParam.ToInt32() == 1234)
      {
        // show About box here...
      }
    }
  }

You might also like...

Comments

Steven Cohn

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Debugging is anticipated with distaste, performed with reluctance, and bragged about forever.” - Dan Kaminsky