How do I resize a WPF control inside a CWnd?(如何在 CWnd 中调整 WPF 控件的大小?)
问题描述
我在 MFC CWnd
中托管 WPF UserControl
.它工作得很好我现在需要弄清楚如何用它的父级调整控件的大小.我已经挂上了 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();
}
推荐答案
Eureka!解决方法是将 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
");
// return -1;
/
本文标题为:如何在 CWnd 中调整 WPF 控件的大小?
基础教程推荐
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 从 std::cin 读取密码 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01