简体   繁体   English

查找Wpf WindowsFormsHost中托管的Winforms控件的中心

[英]Finding the Center of a Winforms Control hosted in a Wpf WindowsFormsHost

I am in the process of converting a Winforms project to WPF, and in doing so I have hit a very particular issue. 我正在将Winforms项目转换为WPF的过程中,遇到了一个非常特殊的问题。 Allow me to explain. 请允许我解释。 Within my project I am using OpenTK, which means I need to host the GLControl within a WindowsFormHost. 在我的项目中,我正在使用OpenTK,这意味着我需要在WindowsFormHost中托管GLControl。 The GLControl makes use of a First Person Camera, and for it to work I need to calculate the exact center of the GLControl, which is where I place the mouse when the user is "looking around". GLControl使用了第一人称摄影机,并且要使其正常工作,我需要计算GLControl的确切中心,当用户“环顾四周”时,我将鼠标置于该中心。 Here is an example of my application in WPF: 这是我在WPF中的应用示例:

在此处输入图片说明

The function that I had previously used in WinForms was: 我以前在WinForms中使用的功能是:

        public static Point FindCenterOfControl(Form parent, Control control)
        {
            Point location = parent.PointToScreen(control.Location);
            return new Point(location.X + control.Width / 2, location.Y + control.Height / 2);
        }

But naturally, this does not work in WPF. 但是自然地,这在WPF中不起作用。 I have tried various parameters, but I keep getting null crashes, mainly from my GLControl, which apparently no longer uses the "Location" property when it's hosted within a WPF application. 我尝试了各种参数,但是我总是收到空崩溃的消息,主要是因为我的GLControl在WPF应用程序中托管时显然不再使用“ Location”属性。 So, quite frankly, I am at a loss of how to accomplish this. 因此,坦率地说,我不知道如何实现这一目标。

As another note, my code works as long as I guess where the center should be. 另外要注意的是,只要我猜中心应该在哪里,我的代码就可以工作。 I can move the camera around and it looks just fine. 我可以移动相机,看起来还不错。 The issue arises when I resize the window, because then the center is nowhere near where it should be and my first person camera goes ballistic. 当我调整窗口大小时会出现问题,因为这样中心距应该放置的位置很远,而我的第一人称相机却弹道。 I need a way to replicate my previous function of calculating the absolute center of a control within my WPF application. 我需要一种方法来复制我以前在WPF应用程序中计算控件绝对中心的功能。

Does anyone have any ideas how I can convert my old function to something that supports hosted controls within a WPF environment? 有谁知道如何在WPF环境中将旧功能转换为支持托管控件的东西?

Nevermind, I found a solution, or at least...a dirty hack perhaps. 没关系,我找到了解决方案,或者至少……是一个肮脏的hack。 The trick was to not try and find the center of my GLControl, but find the center of the WindowsFormsHost instead. 诀窍是不要尝试找到我的GLControl的中心,而是找到WindowsFormsHost的中心。 Here is my code so that it may help someone else in the future. 这是我的代码,以便将来对其他人有帮助。

        public Point FindCenter()
        {
            System.Windows.Point relativePoint = _host.TransformToAncestor(_parent).Transform(new System.Windows.Point(0, 0));
            return new Point((int) (relativePoint.X + _host.ActualWidth / 2), (int) (relativePoint.Y + _host.ActualHeight / 2));
        }

Update: 更新:

As it turns out, the above method was actually wrong. 事实证明,上述方法实际上是错误的。 It centered my cursor at a predefined location regardless of my window size or position. 无论我的窗口大小或位置如何,它都将光标居中在预定位置。 This location was wrong. 这个位置错了。 I have crafted a new method, however, which gives the desired result. 但是,我精心设计了一种新方法,可以达到预期的效果。

        public Point FindCenter()
        {
            System.Windows.Point relativePoint = _host.PointToScreen(new System.Windows.Point(0, 0));
            return new Point((int)(relativePoint.X + (this.Width/2)), (int)(relativePoint.Y + (this.Height/2)));
        }

That method DOES center the cursor on the control at the correct location. 该方法的确将光标居中在控件的正确位置。 If you're going to use this within your own application I would suggest trying both to make certain which one suits your needs. 如果您打算在自己的应用程序中使用它,则建议两者都尝试确定哪一个适合您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM