如何在CWnd中调整WPF控件的大小?

编程入门 行业动态 更新时间:2024-10-26 02:25:14
本文介绍了如何在CWnd中调整WPF控件的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我将WPF UserControl托管在MFC CWnd中.它工作得很漂亮,我现在需要弄清楚如何使用其父项调整控件的大小.我已经钩住OnSize,并且正在调用GetWindowRect并将结果设置为我的控件,如下所示:

I'm hosting a WPF UserControl inside an MFC CWnd. It works beautifully I now need to figure out how to resize the control with its parent. I've hooked the OnSize and I'm calling GetWindowRect and setting the result to my control like so:

void CChildFrame::OnSize(UINT nType, int cx, int cy) { CRect rect; this->GetWindowRect(&rect); m_mainControl->Width = rect.Width(); m_mainControl->Height = rect.Height(); }

推荐答案

尤里卡!解决方法是将HwndSource.SizeToContent设置为SizeToContent.WidthAndHeight.这似乎很违反直觉,因为SizeToContent涉及视口调整其所包含内容的大小的能力,但它确实起作用.我的想法是,它改变了重新绘制控件的方式.整个解决方案(如果有人需要的话)如下:

Eureka! The solution is to set the HwndSource.SizeToContent to SizeToContent.WidthAndHeight. This seems counter intuitive as SizeToContent is involved with a viewport's ability to size to what it contains, but it worked. My thinking is that it changes the way the control is repainted. The solution as a whole, if anyone wants it is as follows:

用于创建并获取WPF用户控件的句柄的函数.在这种情况下称为MyControl:

HWND CChildFrame::GetMyControlHwnd(HWND a_parent, int a_x, int a_y, int a_width, int a_height) { HWND mainHandle = AfxGetMainWnd()->GetSafeHwnd(); IntPtr testHandle = IntPtr(mainHandle); HwndSource^ test = HwndSource::FromHwnd(testHandle); Global::Bootstrap(IntPtr(mainHandle)); HwndSourceParameters^ sourceParameters = gcnew HwndSourceParameters("MyControl"); sourceParameters->PositionX = a_x; sourceParameters->PositionY = a_y; sourceParameters->Height = a_height; sourceParameters->Width = a_width; sourceParameters->ParentWindow = IntPtr(a_parent); sourceParameters->WindowStyle = WS_VISIBLE | WS_CHILD | WS_MAXIMIZE; m_hwndSource = gcnew HwndSource(*sourceParameters); m_myControl = gcnew MyControl(); // *** This is the line that fixed my problem. m_hwndSource->SizeToContent = SizeToContent::WidthAndHeight; m_hwndSource->RootVisual = m_myControl; return (HWND) m_hwndSource->Handle.ToPointer(); }

在主机窗口的OnCreate中调用GetMyControlHwnd.该函数通过设置HwndSource.ParentWindow属性来自己创建父子关系.

Call GetMyControlHwnd in the OnCreate of the host window. The function creates the parent child relationship itself by setting the HwndSource.ParentWindow property.

int CChildFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CMDIChildWnd::OnCreate(lpCreateStruct) == -1) return -1; m_hMyControl = GetMyControlHwnd(this->GetSafeHwnd(), 0, 0, lpCreateStruct->cx, lpCreateStruct->cy); //// create a view to occupy the client area of the frame //if (!m_wndView.Create(NULL, NULL, AFX_WS_DEFAULT_VIEW, // CRect(0, 0, 0, 0), this, AFX_IDW_PANE_FIRST, NULL)) //{ // TRACE0("Failed to create view window\n"); // return -1; //} return 0; }

ChildFrame调整大小时,我只是更改控件的宽度和高度.

When ChildFrame resizes I simply change the width and height of the control.

void CChildFrame::OnSize(UINT nType, int cx, int cy) { CRect rect; this->GetWindowRect(&rect); m_myControl->Width = cx; m_myControl->Height = cy; }

我在头文件中有以下私有字段:

// Fields private: gcroot<HwndSource^> m_hwndSource; gcroot<MyControl^> m_myControl; HWND m_hMyControl;

它有助于您了解这是如何在MFC C ++/CLI代码文件中包括CLR名称空间的方法:

using namespace System; using namespace System::Windows; using namespace System::Windows::Interop;

更多推荐

如何在CWnd中调整WPF控件的大小?

本文发布于:2023-11-05 09:36:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560485.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   大小   如何在   WPF   CWnd

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!