1

I am trying to create a "tooltip" which contains a custom control. I implemented it using ToolStripDropDown which does what I need - closes when the user clicks somewhere else, or activates another window etc.

However, I'd like to be able to get the MouseMove event in the parent control even when the ToolStripDropDown is shown. I tried to set the Capture property of the parent control at various stages (before showing the dropdown, in its Opened event handler etc.) but it is always immediately set back to false. Is there a way (not necessarily employing the Capture property) to get the MouseMove event in the parent control? And no, I don't want to consider ugly hacks like using a timer and periodically checking the mouse position.

crypto_rsa
  • 341
  • 1
  • 12
  • Right, it closes when clicking outside of the dropdown. Getting an event when you click outside of a window requires a bit of magic. It requires capturing the mouse. You can't have it both ways. – Hans Passant Oct 19 '12 at 21:17
  • I don't need to get `MouseDown` events, only `MouseMove` events. Which window gets these events when a `ToolStripDropDown` is shown but the mouse is outside of it? – crypto_rsa Oct 24 '12 at 12:20
  • The dropdown window gets them. Because it captured the mouse. – Hans Passant Oct 24 '12 at 12:24
  • I mean which window gets them if I don't set the `Capture` property manually, ie. I simply show the dropdown (for example a context menu). – crypto_rsa Oct 24 '12 at 12:41
  • You don't have any choice in the matter, the dropdown *must* capture the mouse and will thus *always* get the mouse messages. It has to or it can't work correctly. Not sure why this confuses you, other than that it happens in code that you cannot see. – Hans Passant Oct 24 '12 at 12:47

1 Answers1

0

If you want to know the mouse position all the time, then you should register MouseDown event for both the parent control and the ToolStripDropDown control, something like this:

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    lblPosition.Text = e.Location.ToString();
}

private void toolStripDropDownButton2_MouseMove(object sender, MouseEventArgs e)
{
    lblPosition.Text = e.Location.X + toolStripDropDownButton2.Bounds.Location.X + ", " + toolStripDropDownButton2.Bounds.Location.Y + e.Location.Y;
}

For ToolStripDropDown you should calculate the relative location to its parent

Mohsen Afshin
  • 13,273
  • 10
  • 65
  • 90