Library code snippets

System Menu Items

The following code adds a menu to the form's system menu. It responds to click events by intercepting the WM_SYSCOMMAND message.

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
using System.ComponentModel;
using System.Reflection;

class test : Form {

[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);

public static void Main() {
   Application.Run(new test());
}

public test() {
   this.Text = "Hello";
   this.Show();
   int Menu1 = GetSystemMenu(this.Handle.ToInt32(), 0);  // get handle to system menu
   AppendMenu(Menu1,0xA00,0,null);   // makes a separator
   AppendMenu(Menu1,0,666,"C# Rules!");
}

protected override void WndProc(ref Message m) {
   base.WndProc(ref m);
   if(m.Msg==0x112) {    // WM_SYSCOMMAND is 0x112
   if(m.WParam.ToInt32()==666) {   // the Menu's ID is 666
       //everything in here will run when menu is clicked
       MessageBox.Show("Yo!");
   }
   }    
}

}

Comments

  1. 29 Jul 2004 at 03:26

    nices stuff...excellent tip
  2. 01 Jan 1999 at 00:00

    This thread is for discussions of System Menu Items.

Leave a comment

Sign in or Join us (it's free).

Michael H

Related podcasts

  • Object-Oriented Programming in Ruby

    In this episode, I talk with Scott Bellware about object-oriented programming in Ruby, and Ruby's object model. This is taken from a private conversation, and the audio quality suffers at times. Much thanks to Scott for allowing this to be released.This episode of the Alt.NET Podcast is bro...

Want to stay in touch with what's going on? Follow us on twitter!