Sample Download: http://code.msdn.microsoft.com/CSCustomizeSysMenu-024ccb7f
Today’s code sample demonstrates how to modify a Windows Forms application’s system menu
The sample was written by the Microsoft engineer Nibu Babu Thomas.
You can find more code samples that demonstrate the most typical programming scenarios by using Microsoft All-In-One Code Framework Sample Browser or Sample Browser Visual Studio extension. They give you the flexibility to search samples, download samples on demand, manage the downloaded samples in a centralized place, and automatically be notified about sample updates. If it is the first time that you hear about Microsoft All-In-One Code Framework, please watch the introduction video on Microsoft Showcase, or read the introduction on our homepage http://1code.codeplex.com/.
This application demonstrates how to modify a Windows Forms application’s system menu.
This is how the application looks like…
In this application I’ve demonstrated how to do the following actions…
How to handle events from system menu items: refer WndProc
The gist of the application lies in the following functions…
private void btnAddToFormSystemMenu_Click(object sender, EventArgs e) { IntPtr hMenu = GetSystemMenu(this.Handle, false); if (hMenu != IntPtr.Zero) { Int32 Count = GetMenuItemCount(hMenu); MENUITEMINFO mnuInfo = new MENUITEMINFO(); mnuInfo.cbSize = MENUITEMINFO.sizeOf; mnuInfo.fMask = MIIM_STRING | MIIM_ID; mnuInfo.fState = MFS_ENABLED; mnuInfo.fType = MFT_STRING; mnuInfo.wID = BaseCommandId + ++CurrentIndex; mnuInfo.dwTypeData = txtMenuCaption.Text; InsertMenuItem(hMenu, (UInt32)Count, true, ref mnuInfo); DrawMenuBar(hMenu); } }
private void handleSysCustomCommand(UInt32 CmdId) { IntPtr hMenu = GetSystemMenu(this.Handle, false); MENUITEMINFO mnuInfo = new MENUITEMINFO(); mnuInfo.cbSize = MENUITEMINFO.sizeOf; mnuInfo.fType = MFT_STRING; mnuInfo.fMask = MIIM_STRING; // Get size of buffer first, this call will fill out mnuInfo.cch val which is the buffere size if (!GetMenuItemInfo(hMenu, CmdId, false, ref mnuInfo)) { ShowLastError(); } else { // Pad up a bit mnuInfo.cch += 4; mnuInfo.dwTypeData = new String(' ', (Int32)mnuInfo.cch); if(!GetMenuItemInfo(hMenu, CmdId, false, ref mnuInfo)) // This time get menu item text { ShowLastError(); } else { String Text = String.Format("You clicked on: {0}", mnuInfo.dwTypeData); MessageBox.Show(Text, "Menu text", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } // A demo on how to handle the event from the new system menu items... protected override void WndProc(ref Message Msg) { if (Msg.Msg == WM_SYSCOMMAND) { if ((UInt32)Msg.WParam.ToInt32() > BaseCommandId) { handleSysCustomCommand((UInt32)Msg.WParam.ToInt32()); } } base.WndProc(ref Msg); }
GetSystemMenu function http://msdn.microsoft.com/en-us/library/windows/desktop/ms647985(v=vs.85).aspx
GetMenuItemInfo function http://msdn.microsoft.com/en-us/library/windows/desktop/ms647980(v=vs.85).aspx
GetMenuItemCount http://msdn.microsoft.com/en-us/library/windows/desktop/ms647978(v=vs.85).aspx
DrawMenuBar function http://msdn.microsoft.com/en-us/library/windows/desktop/ms647633(v=vs.85).aspx
GetMenuItemID function http://msdn.microsoft.com/en-us/library/windows/desktop/ms647979(v=vs.85).aspx
GetMenuInfo function http://msdn.microsoft.com/en-us/library/windows/desktop/ms647977(v=vs.85).aspx
nice article!
I had gone through the link i.e., video it's really informative!! Thanks for sharing!!!<a href="www.sampleforms.org/.../a>