1

I'm porting an old app to .NET, in there all the forms sizes are set in Dialog Units.

int w = --- from dialog definition
int h = --- from dialog definition

I tried to use such an approach

int dx = (double)(LOWORD(GetDialogBaseUnits())) / 4;
int dy = (double)(HIWORD(GetDialogBaseUnits())) / 8;
w *= dx;
h *= dy;
_form->ClientSize = Size::Size(w, h);

but .NET form becomes way larger than expected.

Then I tried to use

RECT rc = { _form->Location.Y, _form->Location.X, _form->Location.Y + w, _form->Location.X + h };
BOOL b = ::MapDialogRect((HWND)_form->Handle.ToInt32(), &rc);
char err[1024];
GetLastErrorText(GetLastError(), err, 1024);

inside the OnLoad event, but I'm getting an error saying the window is not a valid dialog, and the rc variable remains un-changed.

How to correctly transform DU inside a WinForms application?

Thx

Eugen
  • 2,934
  • 2
  • 26
  • 47

1 Answers1

0

Per Raymond Chen, "Windows Forms windows are not window manager dialogs."

Instead, I calculated the base units with values from GetTextMetrics():

int baseUnitsX = 0, baseUnitsY = 0;

using (Graphics g = Graphics.FromHwnd(this.Handle))
{
    TEXTMETRICW tw;
    if (NativeMethods.GetTextMetricsW(g.GetHdc(), out tw))
    {       
        baseUnitsX = tw.tmAveCharWidth;
        baseUnitsY = tw.tmHeight;
    }
}

int w = NativeMethods.MulDiv(rect.right, baseUnitsX, 4);
int h = NativeMethods.MulDiv(rect.bottom, baseUnitsY, 8);
R.J. Dunnill
  • 2,049
  • 3
  • 10
  • 21