Thoughts, ideas, ramblings. Covering a wide range of topics from Windows to Linux, Azure, hardware, software & more.
Search

Custom Right Click (Context) Menu on the Taskbar for your Program

Sometimes it may be required in your program that you need to alter the right click menu, otherwise known as context menu. With this following code snippit, you can completley yet simply change the entire menu to your own liking…


The theroy behind this code is that when a user right clicks on the program item, a ‘message’ is sent by the Windows UI to your program. Normally your program handles this message itself however we can ‘override’ this, and place our own actions there instead.

Private Const WMessageRightClickTaskbar As Integer = &H313

Protected Overloads Overrides Sub WndProc(ByRef m As Message)

        ' Check if the intercepted 'message' is a 'right click on taskbar'
        If m.Msg = WMessageRightClickTaskbar Then

            ' It is possible to change the action that occurs.
            ' In this case, a context menu is shown at the cursor position
            ContextMenuStrip1.Show(Cursor.Position)

            ' If you change the above event to something other
            ' than a context menu, remove the 'Exit Sub' below
            ' to restore the context menu and make it appear also.
            Exit Sub

        End If

        ' If it isnt, then handle it normally
        MyBase.WndProc(m)

End Sub

private const int WMessageRightClickTaskbar = 0x313;

protected override void WndProc(ref Message m)
{

    // Check if the intercepted 'message' is a 'right click on taskbar'
    if (m.Msg == WMessageRightClickTaskbar) {

        // It is possible to change the action that occurs.
        // In this case, a context menu is shown at the cursor position
        ContextMenuStrip1.Show(Cursor.Position);

        // If you change the above event to something other
        // than a context menu, remove the 'return' below
        // to restore the context menu and make it appear also.
        return;

       }

// If it isnt, then handle it normally
base.WndProc(m);

}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *