Get position / click on DoubleClick event

Is there a way to get the coordinates (x, y) of the mouse cursor in a DoubleClick control?

As far as I can tell, the position should be derived from global:

  Windows.Forms.Cursor.Position.X, Windows.Forms.Cursor.Position.Y

Also, is there a way to get which button double-clicked?

+6
winforms double-click
source share
3 answers

Control.MousePosition and Control.MouseButtons are what you are looking for. Use Control.PointToClient () and Control.PointToScreen () to convert between screen and relative control coordinates.

See the MSDN Property Control.MouseButtons , Property Control.MousePosition , Control.PointToClient Method and Control.PointToScreen Method for details.


UPDATE

Do not see a tree for trees ...: D Look at the Elk and look at the arguments of the event.

In this article

UPDATE

I missed Muse's throw so it won’t work. You must use the static control properties from Control.DoubleClick (). Since the button information is encoded as a bit field, you need to test as follows using the desired button.

(Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left 
+5
source share

Use the MouseDoubleClick event, not the DoubleClick event. MouseDoubleClick provides MouseEventArgs, not simple EventArg. This applies to "MouseClick", not "Click", as well as ... and all other mouse related events.

MouseDoubleClick ensures that the mouse is really there. DoubleClick may be triggered by something else, and mouse coordinates may not be practical - MSDN: "DoubleClick events are logically higher-level control events. They can be raised by other user actions, such as keyboard shortcuts."

+13
source share

Note. As Dunbrook noted, this will not work on UserControl, since e is not MouseEventArgs. Also note that not all controls even give you a DoubleClick event - for example, a button will simply send you two Click events.

  private void Form1_DoubleClick(object sender, EventArgs e) { MouseEventArgs me = e as MouseEventArgs; MouseButtons buttonPushed = me.Button; int xPos = me.X; int yPos = me.Y; } 

Gets x, y relative to the shape.

Also has a left or right button in MouseEventArgs.

+9
source share

All Articles