默认按钮大小? [英] Default button size?

查看:22
本文介绍了默认按钮大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建一个按钮控件(使用 BUTTON 窗口类的 CreateWindow),它具有与其余部分一致的标准系统范围大小(尤其是高度)Windows 应用程序?我当然应该考虑 DPI 和其他设置.

How do I create a button control (with CreateWindow of a BUTTON window class) that has a standard system-wide size (especially height) that's consistent with the rest of Windows applications? I should of course take DPI into account and probably other settings.

备注:USE_CW_DEFAULT 用于宽度和高度会导致按钮大小为 0, 0,因此这不是解决方案.

Remark: Using USE_CW_DEFAULT for width and height results in a 0, 0 size button, so that's not a solution.

推荐答案

在完美、无忧的世界中...

要创建标准大小的按钮,我们必须这样做:

In the perfect, hassle-free world...

To create a standard size button we would have to do this:

LONG units = GetDialogBaseUnits();
m_hButton = CreateWindow(TEXT("BUTTON"), TEXT("Close"), 
                 WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 
                 0, 0, MulDiv(LOWORD(units), 50, 4), MulDiv(HIWORD(units), 14, 8),
                 hwnd, NULL, hInst, NULL);

其中5014分别是DLU维度,48是水平和垂直对话框模板单元分别基于 GetDialogBaseUnits()功能 文档说明.

where 50 and 14 are respective DLU dimensions, 4 and 8 are horizontal and vertical dialog template units respectively, based on GetDialogBaseUnits() function documentation remarks.

但是 正如 Anders 指出的那样,这些指标基于系统字体.如果您的窗口使用 shell 对话框字体或任何不会让您流血的字体,那么您几乎可以靠自己了.

BUT as Anders pointed out, those metrics are based on the system font. If your window uses a shell dialog font or simply anything not making your eyes bleed, you're pretty much on your own.

要获得自己的对话框"基本单位,您必须使用 GetTextMetrics() 检索当前文本度量,并使用字符高度和平均宽度(tmHeighttmAveCharWidth 分别为 TEXTMETRIC 结构)并用您自己的 MulDiv 翻译它们,除非您在对话框中,然后 MapDialogRect() 将为您完成所有工作.

To get your own "dialog" base units, you have to retrieve current text metrics with GetTextMetrics() and use character height and average width (tmHeight and tmAveCharWidth of the TEXTMETRIC struct respectively) and translate them with MulDiv by your own, unless you are in a dialog, then MapDialogRect() will do all the job for you.

请注意,tmAveCharWidth 仅近似于实际的平均字符宽度,因此建议使用 GetTextExtentPoint32() 函数代替字母字符集.

Note that tmAveCharWidth only approximates the actual average character width so it's recommended to use a GetTextExtentPoint32() function on an alphabetic character set instead.

见:

如果按钮是您想要自动调整大小的唯一控件,您还可以使用 BCM_GETIDEALSIZE 消息 Button_GetIdealSize() 宏(仅适用于 Windows XP 及更高版本)以检索适合按钮所含任何内容的最佳宽度和高度,尽管它看起来很丑,但没有在按钮周围应用任何边距按钮的文字.

If buttons are the only control you want to resize automatically, you can also use BCM_GETIDEALSIZE message Button_GetIdealSize() macro (Windows XP and up only) to retrieve optimal width and height that fits anything the button contains, though it looks pretty ugly without any margins applied around the button's text.

这篇关于默认按钮大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆