Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 21,247 times

Related Categories

Modify a Window's System Menu

stevencohn

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...
      }
    }
  }

Comments

  • Additional notes

    Posted by n_d on 31 Dec 2006

    Hi,


    I tryed to do this with .NET 2.0 and VS 2005 in C#: I've added the three lines code to add a separator and a menu item in the ...