Page 1 of 1

[C++] Check if mouse on the GUI

Posted: 13 Jan 2017, 10:26
by realesmedia
How to implement events OnMouseEnter and OnMouseLeave for all elements at once?
How can I check if mouse on the GUI or not?

Re: [C++] Check if mouse on the GUI

Posted: 16 Jan 2017, 17:19
by sfernandez
What do you mean for all elements at once?

If you want to be notified when an element on a subtree of elements is hovered with the mouse you just need to register to MouseEnter in the parent of that tree, because routed events traverse all elements in the tree.

To check if mouse is over any element of the GUI you can use VisualTreeHelper like this:
Visual* root = VisualTreeHelper::GetRoot(anyElement);
HitTestResult hit = VisualTreeHelper::HitTest(root, mousePos);
if (hit.visualHit != 0)
{
  // Mouse is over a GUI element
}
else
{
  // No GUI element is under the mouse
}

Re: [C++] Check if mouse on the GUI

Posted: 30 Jan 2017, 09:24
by realesmedia
I have GUI but it's not only one who capture mouse click events. My scene capture it too and I want every click on GUI not be captured with scene.

Re: [C++] Check if mouse on the GUI

Posted: 30 Jan 2017, 21:41
by realesmedia
And, yes, variant with VisualTreeHelper is an option. Thanks.

Re: [C++] Check if mouse on the GUI

Posted: 01 Feb 2017, 19:31
by jsantos
Good to know you solved the issue!