且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

怎么把DLU转换成像素?

更新时间:2023-02-19 13:59:32

您应使用

You should use the MapDialogRect() function.

以对话框为单位传入RECT,并以像素为单位返回等效的RECT.请注意,您需要对话框的句柄以便为MapDialogRect()提供足够的上下文.该功能需要知道字体才能执行转换.

Pass in a RECT in dialog units, and the equivalent RECT in pixel units is returned. Note that you need a handle to a dialog in order to give MapDialogRect() sufficient context. The function needs to know the font in order to perform the conversion.

如果您很想使用 GetDialogBaseUnits() ,请记住Raymond Chen所说的话, GetDialogBaseUnits是一个缸.

您可以从此项的标题中猜测,GetDialogBaseUnits是一个 缸.由于没有GetDialogBaseUnits的HWND参数,因此它 不知道您要检索哪个对话框的DLU.所以 猜.

As you can guess from the title of this entry, GetDialogBaseUnits is a crock. Since there is no HWND parameter to GetDialogBaseUnits, it doesn't know which dialog box's DLUs you want to retrieve. So it guesses.

它总是猜错了.

GetDialogBaseUnits返回对话框的对话框基本单位, 使用默认的系统字体.但是没有人使用默认的系统字体 还有.它尖叫着老而笨拙".但是它仍然是默认值 兼容性原因. (因此,GetDialogBaseUnits也是如此.)

GetDialogBaseUnits returns the dialog base units for dialog boxes that use the default system font. But nobody uses the default system font any more. It screams "old and dorky". But it remains the default for compatibility reasons. (And therefore so too does GetDialogBaseUnits.)

如果必须从DLU计算像素尺寸,并且没有对话框的句柄,则必须使用此处概述的方法:

If you have to calculate pixel dimensions from DLUs, and you don't have a handle to a dialog, then you must use the method outlined here: How To Calculate Dialog Base Units with Non-System-Based Font

但是,您在注释中明确指出,对于您的问题,实际上并不需要将DLU转换为像素.您可以使用Delphi的内置表单缩放功能来确保您的表单的大小适合当前的字体缩放比例.

However, you made it clear in the comments that, for your problem, you do not actually need to convert from DLUs to pixels. You can use Delphi's built in form scaling to ensure that your forms are sized appropriately for the prevailing font scaling.