High DPI with embedded WinForm UserControl in a Win32 application.

Robinson 146 Reputation points
2024-09-27T11:30:11.23+00:00

Apologies in advance as this question is a long shot and somewhat obscure.

I have a C++ application I maintain. It embeds two .NET 4.x WinForms User Controls as panels in its Win32 window with:

My_Control.CreateInstance(L"My Namespace.UI.My_User_Control"); My_Control->Set_Hwnd(ptrdiff_t(m_hWnd), 0, 0, m_sPrev.cx, m_sPrev.cy);

My_Control is a _COM_SMARTPTR_TYPEDEF(IUserControl, __uuidof(IUserControl)); Set_Hwnd is a method on the class to set the user control's parent Win32 window. So, in the user control this function will call the Win32 function:

Win32.SetParent(Handle, hWnd);

So far, so good and that all works. But now I have to support High DPI. To that end I have an embedded manifest in the .rc of the C++ app with:

<dpiAwareness>permonitorv2,permonitor</dpiAwareness>

This works for all my Win32/ATL and C++ code. However the User Controls do not recognise the DPI setting at all, even when I set their AutoScaleMode to DPI. Handling OnResize and asking GDI+ what it thinks the DPI is also returns 96. The Win32 controls around these embedded controls all auto size themselves with display changes but the user controls do not.

Is there any way to get User Controls embedded in this way to get the memo about DPI changes? I'm not overly keen on doing all the layout myself, though I could.

Developer technologies | Windows Forms
Windows development | Windows API - Win32
Windows for business | Windows Client for IT Pros | User experience | Other
Developer technologies | .NET | Other
Developer technologies | .NET | .NET CLI
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.