none
Add Menu to Taskbar

    Question

  • Hello,

    I know about creating contextMenus, menuStrips, and menuItems ext., but how to add one to the taskbar when right clicked?

    Regards,

    Adjutor
    Sunday, November 09, 2008 11:02 PM

Answers

  • It is possible to make your own context menu appear on right click of the taskbar, however, for your program only.

    It involves intercepting the message which is sent to your program that indicates the user has right clicked on the taskbar item. Basically, then once we intercept the message we can display our own context menu strip, preventing the real one appearing. This in effect allows you to use your own right click menu to your programs taskbar item.


    The following code is in VB.net. Place it in the form which appears in the taskbar.

       Private Const WMessageRightClickTaskbar As Integer = &H313  
     
        Protected Overloads Overrides Sub WndProc(ByRef m As Message)  
     
            If m.Msg = WMessageRightClickTaskbar Then 
     
                aContextMenuStrip.Show(Cursor.Position.X, Cursor.Position.Y)  'Replace this context menu strip with another, or another action all together.
     
                Exit Sub 
     
            End If 
     
            MyBase.WndProc(m)  
     
        End Sub 


    This is the same code in C#.

    private const int WMessageRightClickTaskbar = 0x313;  
     
    protected override void WndProc(ref Message m)  
    {  
          
        if (m.Msg == WMessageRightClickTaskbar) {  
            
            aContextMenuStrip.Show(Cursor.Position.X, Cursor.Position.Y);   
              
            return;  
        }  
          
          
        base.WndProc(m);  
          
    }  
     

    Hope that helps!

    -- Jake
    ShadowXVII | http://www.shadowsplace.net
    • Edited by ShadowXVII Thursday, November 13, 2008 4:29 AM
    • Proposed as answer by Michael Key Thursday, November 13, 2008 5:39 PM
    • Marked as answer by Martin Xie - MSFT Friday, November 14, 2008 11:04 AM
    Thursday, November 13, 2008 4:28 AM

All replies

  • Hi Adjutor,

    When you say 'Taskbar' are you referring to the Windows taskbar? The icon of your program in the system tray? Or a toolstrip on your form UI?


    -- Jake


    ShadowXVII | http://www.shadowsplace.net
    Wednesday, November 12, 2008 6:05 AM
  • Shadow XVII,

    Thank you for the reply.  I am referring to the taskbar where the programs go in Windows when minimzed.  There is an example at this url: http://cache.lifehacker.com/assets/resources/2007/03/xptaskbar-preview.png
    I would like to make a menu for the programs taskbar item when the user right clicks the taskbar item of the program.

    Regards,

    Adjutor
    Wednesday, November 12, 2008 10:15 PM
  • It is possible to make your own context menu appear on right click of the taskbar, however, for your program only.

    It involves intercepting the message which is sent to your program that indicates the user has right clicked on the taskbar item. Basically, then once we intercept the message we can display our own context menu strip, preventing the real one appearing. This in effect allows you to use your own right click menu to your programs taskbar item.


    The following code is in VB.net. Place it in the form which appears in the taskbar.

       Private Const WMessageRightClickTaskbar As Integer = &H313  
     
        Protected Overloads Overrides Sub WndProc(ByRef m As Message)  
     
            If m.Msg = WMessageRightClickTaskbar Then 
     
                aContextMenuStrip.Show(Cursor.Position.X, Cursor.Position.Y)  'Replace this context menu strip with another, or another action all together.
     
                Exit Sub 
     
            End If 
     
            MyBase.WndProc(m)  
     
        End Sub 


    This is the same code in C#.

    private const int WMessageRightClickTaskbar = 0x313;  
     
    protected override void WndProc(ref Message m)  
    {  
          
        if (m.Msg == WMessageRightClickTaskbar) {  
            
            aContextMenuStrip.Show(Cursor.Position.X, Cursor.Position.Y);   
              
            return;  
        }  
          
          
        base.WndProc(m);  
          
    }  
     

    Hope that helps!

    -- Jake
    ShadowXVII | http://www.shadowsplace.net
    • Edited by ShadowXVII Thursday, November 13, 2008 4:29 AM
    • Proposed as answer by Michael Key Thursday, November 13, 2008 5:39 PM
    • Marked as answer by Martin Xie - MSFT Friday, November 14, 2008 11:04 AM
    Thursday, November 13, 2008 4:28 AM
  • However, with the way you are proposing, the menu doesn't look like the windows menu that comes up, and I want to edit the options still, but it look just like the windows menu, but with edited options, then if there is also some key combination, to put that and everything as well, so it will show up too.

    Is there a way to do this?

    I could probably capture the right click, change some settings on my menu options, and then do it, but I don't know if this is what I need to do or not, is there a good way to do it?
    Thursday, November 13, 2008 5:49 PM
  • Your right there, in regard to it being different however maybe being different is a good thing :P :P

    Im quite sure however, that its possible to add items to the Windows menu (e.g. Windows Help or a Management Console)


    This code will allow you to add items to the existing menu bar, interestingly it uses the same-ish methods of intercepting the 'User clicked on this item button' which makes handling the events a little harder but not impossible.

    I hope that points you in the right direction!

    ShadowXVII | http://www.shadowsplace.net
    Friday, November 14, 2008 12:56 AM
  • Shadow XVII,

    Nice, the code works.  And also thank you for the code there to add items to the System Menu.  Thank you.

    Regards,

    Adjutor
    Wednesday, November 19, 2008 12:44 PM