简体   繁体   English

如何从直接创建的HwndSource中“获取” WPF窗口?

[英]How do I get a WPF Window “from” an HwndSource I create directly?

If I create an HwndSource directly, have I then also created a WPF Window that I can now access from code? 如果直接创建HwndSource ,那么是否还创建了WPF Window ,现在可以从代码中访问它? If so, how can I access it? 如果是这样,我如何访问它?

Or do I now need to somehow "add" a WPF Window to that HwndSource ? 还是我现在需要以某种方式将WPF Window “添加”到该HwndSource If so, how do I do this? 如果是这样,我该怎么做?

I have studied the HwndSource documentation thoroughly, and this part is not explained well at all. 我已经对HwndSource文档进行了彻底的研究,而这部分内容并没有得到很好的解释。 I understand that I can get the HwndSource from an existing WPF Window, but that does not help me. 我知道可以从现有的WPF窗口获取HwndSource ,但这无济于事。 I need to intercept the creation of the Window , so I can force it to WS_CHILD style and set its parent directly; 我需要拦截Window的创建,因此可以将其强制为WS_CHILD风格并直接设置其父级。 and the docs say you must create the HwndSource directly if you want to force its parent. 文档说如果要强制其父代必须直接创建HwndSource。

Edit: I've been studying every question I can find with HwndSource in it; 编辑:我一直在研究我可以在其中找到HwndSource每个问题; it looks like you "add" the WPF object to the HwndSource object by setting the RootVisual property of the HwndSource object to the WPF object you want displayed; 看起来像你“加上” WPF对象到HwndSource通过设置对象RootVisual的财产HwndSource对象到WPF对象,你想显示; or maybe by calling the HwndSource AddSource method? 还是通过调用HwndSource AddSource方法? Will examine those next. 接下来将检查那些。 Hope this is useful to other questioners. 希望这对其他提问者有用。

As I suspected, the solution is to add your WPF object to the HwndSource.RootVisual object. 我怀疑,解决方案是将WPF对象添加到HwndSource.RootVisual对象。 In the example below, NativeMethods is my class for PInvoke of Win32 APIs. 在下面的示例中,NativeMethods是Win32 API的PInvoke的类。 Use SetLastError and GetLastError to check for Windows errors. 使用SetLastError和GetLastError检查Windows错误。

Note that in this case you must use a User Control or Page or the like; 注意,在这种情况下,您必须使用用户控件或页面等。 you can NOT set HwndSource.RootVisual to be an existing or "new" WPF Window, as WPF Windows already have a Parent, and it won't accept an object with a Parent. 您不能将HwndSource.RootVisual设置为现有的WPF窗口或“新的” WPF窗口,因为WPF Windows已经有一个Parent,并且它将不接受带有Parent的对象。

    private void ShowPreview(IntPtr hWnd)
    {
        if (NativeMethods.IsWindow(hWnd))
        {
            // Get the rect of the desired parent.
            int error = 0;
            System.Drawing.Rectangle ParentRect = new System.Drawing.Rectangle();
            NativeMethods.SetLastErrorEx(0, 0);
            bool fSuccess = NativeMethods.GetClientRect(hWnd, ref ParentRect);
            error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();

            // Create the HwndSource which will host our Preview user control
            HwndSourceParameters parameters = new HwndSourceParameters();
            parameters.WindowStyle = NativeMethods.WindowStyles.WS_CHILD | NativeMethods.WindowStyles.WS_VISIBLE;
            parameters.SetPosition(0, 0);
            parameters.SetSize(ParentRect.Width, ParentRect.Height);
            parameters.ParentWindow = hWnd;
            HwndSource src = new HwndSource(parameters);

            // Create the user control and attach it
            PreviewControl Preview = new PreviewControl();
            src.RootVisual = Preview;
            Preview.Visibility = Visibility.Visible;
        }
    }

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

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