Domanda

I have a WPF window which contains also WinForms controls wrapped in a WindowsFormsHost (besides other WPF controls). I'd like to determine the control (WPF or WinForms) which is displayed under a certain position.

To receive the position of a WPF control I can use

VisualTreeHelper.HitTest(wpfWindow, position)

This does not work when a WindowsFormsHost is displayed at this position. For that case HitTest() returns a WPF control which is behind the WindowsFormsHost.

Any ideas how to get that working also for interop scenarios?

È stato utile?

Soluzione

Check the position to see if they are within the winform bounds. You can determine the bounds by creating a transform between the highest level WPF control and the winform, then using coordinate 0,0. Combined with the hosted window width and height, you now have the bounds of the window. Translate your position using a tranform to the highest level WPF control, and now you have a method of checking if the position is within the winform bounds.

After you've decided if the mouse is within the winform. Refer to this question

If it's not in the winform, use VisualTreeHelper.

    public Rect HostRect
    {
        get
        {
            var transform = _Host.TransformToVisual(this);
            return new Rect(transform.Transform(new Point(0, 0)), new Point(_Host.ActualWidth, _Host.ActualHeight));
        }
    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top