Important alert: (current site time 7/15/2013 8:18:11 AM EDT)
 

VB icon

Add New Menu To System Menu

Email
Submitted on: 11/25/2003 8:56:48 AM
By: Philip Pierce  
Level: Intermediate
User Rating: By 4 Users
Compatibility: C#
Views: 21456
(About the author)
 
     Demonstrates how to add a new menu to the system menu found in the icon in the top left corner of your form, or by right clicking on the app in the task bar.

Original idea was submitted by Nicholas Afxentiou for VB6, which can be found at: http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=1519&lngWId=10

Please note that the code must be used in a form class, it can not be used in a non-gui class.


 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Add New Menu To System Menu
// Description:Demonstrates how to add a new menu to the system menu found in the icon in the top left corner of your form, or by right clicking on the app in the task bar.<br><br>
Original idea was submitted by Nicholas Afxentiou for VB6, which can be found at: http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=1519&lngWId=10
<br><br>
Please note that the code must be used in a form class, it can not be used in a non-gui class.<br><br>
// By: Philip Pierce
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1822&lngWId=10//for details.//**************************************

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
.........
		/* API References:
		 * RemoveMenu: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/resources/menus/menureference/menufunctions/removemenu.asp
		 * InsertMenuItem: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/resources/menus/menureference/menufunctions/insertmenuitem.asp
		 * AppendMenu: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/resources/menus/menureference/menufunctions/appendmenu.asp
		*/
		
		#region API Declarations
		// return the handle for the system menu
		[DllImport ("user32.dll")]
		public static extern int GetSystemMenu(int hwnd, bool bRevert);
		// append a menu item to the menu
		[DllImport ("user32.dll", EntryPoint="AppendMenuA")]
		public static extern long AppendMenu(int hMenu, int wFlags, int wIDNewItem, string lpNewItem);
		// remove a menu item from the menu
		[DllImport ("user32.dll")]
		public static extern long RemoveMenu(int hMenu, int nPosition, int wFlags);
		#endregion // API Declarations
		// constants
		private const int MF_BYPOSITION = 1024;
		private const int MF_SEPERATOR = 2048;
		private const int MF_REMOVE = 4096;
		private const int WM_SYSCOMMAND = 274;
		#region System Menu API
		/// <summary>
		/// Returns the handle for the System Menu with the associate form
		/// </summary>
		/// <param name="frmHandle">The handle to the form to retrieve the menu handle</param>
		public int GetSysMenuHandle(int frmHandle)
		{
			return GetSystemMenu(frmHandle, false);
		}
		/// <summary>
		/// Removes a system menu
		/// </summary>
		/// <param name="mnuHandle">Then handle to the system menu. Use GetSysMenuHandle</param>
		/// <param name="mnuPosition">Zero based position of the menu item to delete</param>
		/// <returns>returns nonzero on success, returns 0 on fail</returns>
		public long RemoveSysMenu(int mnuHandle, int mnuPosition)
		{
			return RemoveMenu(mnuHandle, mnuPosition, MF_REMOVE);
		}
		/// <summary>
		/// Appends a menu item to the end of the system menu list
		/// </summary>
		/// <param name="mnuHandle">Handle to the system menu to append to</param>
		/// <param name="MenuID">A unique ID sent by the calling function to track events for the menu</param>
		/// <param name="mnuText">The display text of the menu</param>
		/// <returns>Returns nonzero on success, 0 on fail.</returns>
		public long AppendSysMenu(int mnuHandle, int MenuID, string mnuText)
		{
			return AppendMenu(mnuHandle, 0, MenuID, mnuText);
		}
		/// <summary>
		/// Appends a seperator bar at the end of the system menu
		/// </summary>
		/// <param name="mnuHandle">Handle to the system menu</param>
		/// <returns>Returns nonzero on success, 0 on fail</returns>
		public long AppendSeperator(int mnuHandle)
		{
			return AppendMenu(mnuHandle, MF_SEPERATOR, 0, null);
		}
		// THIS IS IMPORTANT. The WndProc function has to be overridden so
		// we can capture the events from a menu item being selected.
		// In the future, we could add OwnerDraw features here, and process
		// that here, too (see: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/resources/menus/menureference/menufunctions/appendmenu.asp
		/// <summary>
		/// Overrides the orignal WndPrc function to process menu events
		/// </summary>
		/// <param name="messg">The messages sent to WndProc</param>
		protected override void WndProc(ref Message messg)
		{
			int i = 1; // for EXAMPLE purposes only
			// see if this is a menu message to monitor
			if (messg.Msg == WM_SYSCOMMAND)
			{
				// decide how to handle the command
				switch (messg.WParam.ToInt32())
				{
					case x:
						// TODO: this is for example only, replace x with
						// whatever values you gave for the MenuID
						// then process the menu event here by calling
						// whatever function necessary
				}
			}
			// return the message so other wndprocs can process them
			base.WndProc(ref messg);
		}
		#endregion // System Menu API


Other 21 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

12/10/2003 5:23:52 AMPieter Joost van de Sande

Thanks!!! Nice one! :)
(If this comment was disrespectful, please report it.)

 
7/21/2004 10:07:19 AM

This is a good code
(If this comment was disrespectful, please report it.)

 
9/21/2004 10:04:14 PM

one probelm in RemoveSysMenu method - it needs an additional flag to work...

public long RemoveSysMenu(int mnuHandle, int mnuPosition){
return RemoveMenu(mnuHandle, mnuPosition, MF_BYPOSITION | MF_REMOVE);
}
(If this comment was disrespectful, please report it.)

 
5/30/2006 3:45:57 AMjibran

plz consider me as beginer in c# and plz give some example to use this above menu code.
regards
jibran
(If this comment was disrespectful, please report it.)

 
2/7/2007 3:08:20 PMazath

Its Very nice top develope my skill in the development.
(If this comment was disrespectful, please report it.)

 
8/28/2008 9:09:49 AMantonysamy

good
(If this comment was disrespectful, please report it.)

 
12/7/2008 7:10:37 AMbx

thnkx
(If this comment was disrespectful, please report it.)

 
2/26/2009 9:09:09 AMsombir

klu
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.