[Sample of Apr 6th] Customize Windows Forms app's System Menu - Microsoft All-In-One Code Framework - Site Home - MSDN Blogs
Microsoft All-In-One Code Framework - Developers' Pain Killer

[Sample of Apr 6th] Customize Windows Forms app's System Menu

Rate This
  • Comments 2

 

Homepage image
Sample of the Day RSS Feed

Sample Download: http://code.msdn.microsoft.com/CSCustomizeSysMenu-024ccb7f

Today’s code sample demonstrates how to modify a Windows Forms application’s system menu

image

The sample was written by the Microsoft engineer Nibu Babu Thomas.

imageYou 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/.

 

Introduction

This application demonstrates how to modify a Windows Forms application’s system menu.

 

Running the Sample

This is how the application looks like…

image

 

Using the Code

In this application I’ve demonstrated how to do the following actions…

  1. Retrieve system menu: refer btnAddToFormSystemMenu_Click
  2. Modify system menu: refer btnAddToFormSystemMenu_Click
  3. Retrieve text item of a system menu item: refer handleSysCustomCommand

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

More Information

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

Leave a Comment
  • Please add 4 and 3 and type the answer here:
  • Post
  • nice article!

  • I had gone through the link i.e., video it's really informative!! Thanks for sharing!!!<a href="www.sampleforms.org/.../a>

Page 1 of 1 (2 items)