views:

253

answers:

4

Hi.

I have a panel which holds many pictureboxes. Each picturebox has registered "contextRightMenu" as their context menu.

What i want when the context menu pops up is to get the current mouseposition.

I have tried getting the mouseposition by using mouseDown and click, but these events happens after one of the items of the context menu is clicked, and that is too late.

the popup event of the context menu does not deliver mouse event args, so i don't know how to get the mouseposition.

If i can get mouse event args it is easy.

Then i just can:

 this.contextRightClick.Popup += new System.EventHandler(this.contextRightClick_Popup);

// If EventArgs include mouseposition within the sender
private void contextRightClick_Popup)(object sender, EventArgs e)
{
   int iLocationX = sender.Location.X;
   int iLocationY = sender.Location.Y;

   Point pPosition = new Point(iLocationX + e.X, iLocationY + e.Y);  // Location + position within the sender = current mouseposition
}

Can anyone help me either get some mouse event args, or suggest a event that will run before the contextmenu pop ups?

Thanks in advance

A: 

You can try the MouseClick event of picturebox and get the location if it is a right click.

Veer
Will try it out now!
The mouseclick did not have any mouseevent args
Try MouseDown or MouseUp Events. Don't know the things supported in CF. Also take a look at Cursor.Position property.
Veer
A: 

Handle the MouseClick of your PictureBox. Something like this (in vb.net):

Sub OnMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) handles YourPictureBox.mouseclick

        If e.Button = Windows.Forms.MouseButtons.Right then
        'if you need the screen posistion
        PointToScreen(New System.Drawing.Point(e.X, e.Y))
        'if you need just the location
        e.Location

        end if
end sub
Ando
Thanks, i'll try that now :)
bah, can't seem to find anything equivalent to your vb solution...
Found something:private void panelMouseClick(Object sender, EventArgs e) { MouseEventArgs args = e as MouseEventArgs; }But args just returned null, anyway :(
try to use 'e' as it is. I think it is of type MouseEventArgs in CF eventhough it is EventArgs. Not sure though.
Veer
A: 

you may wantto take a look to ContextMenuStrip Class and Control.ContextMenuStripChanged Event, some exemple here

metro
+1  A: 

Do you want the cursor location relative to the PictureBox that was right clicked or relative to the parent Panel, or the parent Window or possibly just the screen position?

The following might help as a starting point. Here I get the current mouse cooridnates on the entire screen then using the SourceControl from the contextRightMenu, which is a reference the instance of the control that was right clicked on, we convert the screen coordinates to a point relative to the source control.

void contextRightMenu_Popup(object sender, EventArgs e)
{
  ContextMenu menu = sender as ContextMenu;

  if (menu != null)
  {
    // Get cursor position in screen coordinates
    Point screenPoint = Cursor.Position;

    // Convert screen coordinates to a point relative to the control
    // that was right clicked, in your case this would be the relavant 
    // picture box.
    Point pictureBoxPoint = menu.SourceControl.PointToClient(screenPoint);
  }
}
Chris Taylor
Hi!It is the Parent Panel's position i am interested in. But i'll look into the SourceControl. Thanks