【问题标题】:DialogUnits in .NET WinForms.NET WinForms 中的 DialogUnits
【发布时间】:2017-08-22 03:23:46
【问题描述】:

我正在将一个旧应用程序移植到 .NET,其中所有表单大小都在对话框单元中设置。

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

我尝试使用这种方法

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

但 .NET 表单变得比预期的要大。

然后我尝试使用

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);

OnLoad 事件中,但我收到一条错误消息,提示窗口不是有效的对话框,并且rc 变量保持不变。

如何在 WinForms 应用程序中正确转换 DU?

谢谢

【问题讨论】:

    标签: c# winforms managed-c++


    【解决方案1】:

    根据 Raymond Chen,“Windows 窗体窗口不是窗口管理器对话框。”

    相反,我使用 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-12
      • 2011-09-03
      • 1970-01-01
      • 2010-12-08
      • 2011-01-13
      • 1970-01-01
      • 2010-12-08
      • 2011-03-02
      相关资源
      最近更新 更多