You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MFC嵌入WPF控件OnSize事件跨设备缩放异常问题求助

MFC嵌入WPF控件的跨设备缩放问题

我通过HwndSource与C++/CLI技术在MFC应用中嵌入WPF控件,并将SizeToContent设置为WidthAndHeight。但在不同电脑上出现错误缩放问题:MFC控件创建时WPF控件尺寸过大。我已尝试通过DPI值重新缩放客户区坐标,该方案在我的电脑上生效,但在其他电脑上嵌入控件的尺寸为MFC客户区的两倍。

相关代码如下:

c++cli.h

#pragma once

#if _MANAGED

ref class ControlManager
{
public:
  ControlManager() { }
  ~ControlManager() {}
  Sharp::Control^ Current;

  System::Windows::Interop::HwndSource^ HwndSource;
  void Init(LPCREATESTRUCT lp, HANDLE Hwdn);
  void SetSize(int w, int h);
};

#endif

class AFX_EXT_CLASS CXbimWnd final 
{
public:
  int OnCreate(LPCREATESTRUCT lpCreateStruct, HANDLE hwnd);
  void OnSize(UINT nId, int h, int w);
  
private:

#ifdef _MANAGED
  // current 3d view
  gcroot<ControlManager^ > m_ObjHandle;
#else
  intptr_t m_ObjHandle;
  intptr_t HwndSource;
#endif
};

c++cli.cpp

int CXbimWnd::OnCreate(LPCREATESTRUCT lp, HANDLE hwnd)
{
  m_ObjHandle = gcnew ControlManager(m_Log);
  m_ObjHandle->Init(lp, hwnd);

  return 0;
}

void ControlManager::Init(LPCREATESTRUCT lp, HANDLE hwnd)
{
  System::Windows::Interop::HwndSourceParameters sourceParams("XbimXplorer");
  sourceParams.PositionX = lp->x;
  sourceParams.PositionY = lp->y;
  sourceParams.Height = lp->cy;
  sourceParams.Width = lp->cx;
  sourceParams.ParentWindow = (System::IntPtr)hwnd; 
  sourceParams.WindowStyle = WS_VISIBLE | WS_CHILD | WS_MAXIMIZE;

  HwndSource = gcnew System::Windows::Interop::HwndSource(sourceParams);
  HwndSource->SizeToContent = System::Windows::SizeToContent::WidthAndHeight;

  Current = gcnew Sharp::Control();
  HwndSource->RootVisual = Current;
}


void CXbimWnd::OnSize(UINT /*nId*/, int h, int w)
{
  m_ObjHandle->SetSize(h, w);
}

void ControlManager::SetSize(int h, int w)
{
  Current->Height = h;
  Current->Width = w;
}

mfc.h

class CBIMXBimBar : public CBCGPDockingControlBar
{
public:
  CBIMXBimBar();

  void AdjustLayout();
  virtual ~CBIMXBimBar();

  // Generated message map functions
protected:
  afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
  afx_msg void OnSize(UINT nType, int cx, int cy);
  DECLARE_MESSAGE_MAP()
  CXbimWnd xbim;
};

mfc.cpp

BEGIN_MESSAGE_MAP(CBIMXBimBar, CBCGPDockingControlBar)
  ON_WM_CREATE()
  ON_WM_SIZE()
END_MESSAGE_MAP()


int CBIMXBimBar::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
  if (CBCGPDockingControlBar::OnCreate(lpCreateStruct) == -1)
    return -1;

  CRect rectDummy;
  rectDummy.SetRectEmpty();
  auto result = xbim.OnCreate(lpCreateStruct, m_hWnd);

  if (result == 0)
    AdjustLayout();

  return result;
}

void CBIMXBimBar::OnSize(UINT nType, int cx, int cy)
{
  AdjustLayout();
}

void CBIMXBimBar::AdjustLayout()
{

  CRect rectClient;
  GetClientRect(rectClient);
  auto scale = globalUtils.GetDPIScale(); 
  xbim.OnSize(0, int(rectClient.Height()/scale), int(rectClient.Width()/scale));  //<<---

}

内容的提问来源于stack exchange,提问作者Vitaly

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 08:56:10