Position of context menu

Hi,

How can I get the position of context menu

Thank's
Alexei




Answer this question

Position of context menu

  • wildcard_swe

    Hi,

    I have tryed it. Try to read before about it. It gives me the mouse position and not the left top corner of context menu.

    Thank's
    Alexei



  • catgskat3

    Hi,

    You are wrong. When I click the mouse and want to show the context menu it appears near but the Left Top corner can be in another place then the mouse position.

    I took as base position that you tell but I found that is wrong.

    Thank's
    Alexei



  • 北极星

    Hi,

    I checked. It's nice but I didn't get the position in context menu. I get the position of context menu container.

    Thank's
    Alexei



  • Amish Bandekar

    Normally, the context menu will be shown at the center of the control when the keyboard is used to invoke the context menu of a control (Shift+F10, for example). You can customize the location as follows.

    Override WndProc in the grid and check if the Message.Msg is WM_CONTEXTMENU (0x007b)

    If so, check if the Message.LParam is -1, which means this was due to a keyboard message as opposed to the user right-clicking the mouse.

    Now, call the associated ContextMenu's Show method explicity at the selected row position.

    Make sure NOT to call the base class after showing the menu explicitly.

    protected override void WndProc(ref Message m)
    {
    if(m.Msg == 0x007B /*WM_CONTEXTMENU*/)
    {
    // LParam == -1 means that this is due to a keyboard message
    if((int)m.LParam == -1)
    {
    this.contextMenu.Show();
    return;
    }
    }
    }

    [VB.NET]

    Protected Overrides Sub WndProc(ByRef m As Message)
    If m.Msg = 0x007B Then 'WM_CONTEXTMENU
    ' LParam == -1 means that this is due to a keyboard message
    If (CType(m.LParam,Integer)) = -1 Then
    Me.contextMenu.Show()
    Return
    End If
    End If
    End Sub

    Reference:
    http://www.windowsforms.net/FAQs/default.aspx PageID=3&CategoryID=3&SubcategoryID=71&tabindex=3


  • Wedgebert

    Hello Alex,

    If we get the mouse position at the time of displaying the contextmenu ,that is the top-left position of context menu.bcos the context menu will display in the same position of mouse pointer always.

    I think I had told this point in my first post.

    Regards

    Joy



  • Patrick.E

    Hi

    The context menu will be displayed near to mouse always.So get the position of mouse using

    Control.MousePosition

    Regards,

    Joy



  • Another Emcee

    Alexei,

    Is this context menu for a form or some other control.If it for some other control,then add this method to that control.Put a breakpoint on the first line and see if code comes in there.


  • DaveEngineer

    Have you tried Control.MousePosition in popup event of contextmenu

    private void contextMenu1_Popup(object sender, System.EventArgs e)

    {

    Console.WriteLine(Control.MousePosition .ToString());

    }

    Joy



  • dave_t_was_taken_already

    In the MouseUp Event of your panel write this code:


    if (e.Button == MouseButtons.Right)
    {
    Point contextMenuLocation = e.Location;
    MessageBox.Show(contextMenuLocation.ToString());
    }


  • Yougewenti

    Hi,

    I want to get screen X,Y position of Context Menu that created on Panel.

    Thank's
    Alexei



  • Hephie

    Hi Alexei,

    If you want the top-left of the context menu,then Point p in my example above has that value.Do you want the top-left position of the contextmenu or the point where the contextmenu was clicked


  • Nasha

    Hi,

    You are wrong. I put this post becouse it doesn't work like you tell me.

    Thank's
    Alexei



  • Carl Daniel

    In your form,use this bit of code to return the left-top co-ordinate of the contextmenu.

    protected override void WndProc(ref Message m)

    {

    if (m.Msg == 0x7b) //WM_CONTEXTMENU

    {

    Point p = Control.MousePosition;

    p = this.PointToClient(p); //Convert Screen to client coordinates

    this.CreateGraphics().DrawRectangle(Pens.Red,p.X,p.Y,100,100); //Proof that it works

    }

    base.WndProc(ref m);

    }

    Hope this helps.


  • WL Dev Forums Moderator - MSFT

    Hi,

    It doesn't work. It doesn't get into the if clause.

    Thank's
    Alexei



  • Position of context menu