调整作为 CView 子级的 WPF 窗口的大小
我面临一个奇怪的问题,我需要你的帮助。我有一个 MFC MDI 应用程序,我正在尝试创建一个 WPF 窗口作为打开的 CView 的子窗口。
通过在 CView 中处理 OnCreate 消息并创建 WPF 窗口,我成功地做到了这一点。我还将 CView 设置为 WPF 窗口的父窗口,因此它的行为就像其子窗口一样。
这是我所做的:
int CMyView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
m_windowWrapper.Create(this->GetSafeHwnd());
return 0;
}
void CMyWindowWrapper::Create(HWND hParent)
{
m_myWindow = gcnew MyWindow();
m_myWindow->Show();
IntPtr^ hChildWnd = m_myWindow->GetHwnd();
::SetParent((HWND)hChildWnd->ToPointer(), hParent);
}
public partial class MyWindow : Window
{
public MyWindow()
{
InitializeComponent();
MouseLeftButtonDown += (o, e) => DragMove();
}
public IntPtr GetHwnd()
{
return (new System.Windows.Interop.WindowInteropHelper(this)).Handle;
}
}
WPF 窗口按预期显示。现在,当我尝试调整 WPF 窗口大小时,问题发生了。突然,窗口内的控件被拉伸并重新定位,就好像 WPF 窗口的新大小是父 CView 的大小一样!我尝试处理 SizeChanged 事件,发现 NewSize 比 WPF 窗口的大小大得多,并且很可能是包含的 CView 的大小。
我尝试删除 SetParent 调用,并且调整大小工作正常。我想知道调整大小和父级为 CView 的 WPF 窗口出了什么问题。
我上传了一个说明问题的示例应用程序: http://cid-a059807de2e23c43.office.live.com/ self.aspx/.Public/ResizeProblem.zip
请帮助我。
谢谢。
I am facing a weird problem and I need your help. I have an MFC MDI application, and I am trying to create a WPF Window as a child of the opened CView.
I was able to successfully do that by handling the OnCreate message in my CView, and creating the WPF window. I also set the CView to be the parent of the WPF window so it behaves as its child.
Here is what I did:
int CMyView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
m_windowWrapper.Create(this->GetSafeHwnd());
return 0;
}
void CMyWindowWrapper::Create(HWND hParent)
{
m_myWindow = gcnew MyWindow();
m_myWindow->Show();
IntPtr^ hChildWnd = m_myWindow->GetHwnd();
::SetParent((HWND)hChildWnd->ToPointer(), hParent);
}
public partial class MyWindow : Window
{
public MyWindow()
{
InitializeComponent();
MouseLeftButtonDown += (o, e) => DragMove();
}
public IntPtr GetHwnd()
{
return (new System.Windows.Interop.WindowInteropHelper(this)).Handle;
}
}
The WPF window appears as expected. Now the problem happens when I try to resize the WPF window. Suddenly the controls inside the window are streched and re-positioned as if the new size of the WPF window is the size of the parent CView! I tried to handle the SizeChanged event, and found that the NewSize was much larger than the size of the WPF window and was most probably the size of the containing CView.
I tried to remove the SetParent call, and the resizing worked correctly. I wonder what is going wrong in resizing and WPF window whose parent is a CView.
I uploaded a sample application illustrating the problem:
http://cid-a059807de2e23c43.office.live.com/self.aspx/.Public/ResizeProblem.zip
Please help me.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到了同样的问题,在使用 SetParent 后,子窗口上的任何 Resize 或 DragMove 都会导致 NewSize 变得巨大。我的不同之处在于我的主要应用程序是 WPF,我在其中创建和创建在单独的线程上运行子 WPF 窗口。
我的老板找到了这个链接 1 有人评论了如何使用反射将子窗口的所有者设置为其父窗口:
使用此代码并删除我的 SetParent 后,我的问题消失了,并且能够将子窗口的所有者设置为运行在其上的主 WPF 窗口一个单独的线程。
I had the same problem where after using SetParent any Resize or DragMove on my child window would cause the NewSize to be enormous. My difference is that my main app is WPF where I create & run a child WPF window on a separate thread.
My boss found this link 1 where someone commented on how to set a child's Owner to its parent using reflection:
After using this code and removing my SetParent my problems went away and was able to set my child window's owner to my main WPF window running on a separate thread.