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.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,888 questions
Windows
Windows
A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.
5,376 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,833 questions
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,609 questions
.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
336 questions
{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.