如何使用 CreateFontA 设置字体大小? [英] How to set font size using CreateFontA?

查看:187
本文介绍了如何使用 CreateFontA 设置字体大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不习惯 CreateFontA,但我正在为使用此功能的游戏制作 mod,我也必须使用它.

I'm not used to CreateFontA, but I'm doing a mod for a game that uses this function and I have to use it too.

我设法更改了字体,但在更改字体大小时遇到​​了一些麻烦.

I managed to change the font, but I'm having some trouble to change font size.

我想使用 11px 的字体大小,我该如何实现?我尝试将宽度和高度设置为 11,但它出错了.

I want to use font size 11px, how can I achieve this? I tried just setting width and height to 11 but it went terrible wrong.

推荐答案

答案是(或至少可以是)有点重要.

The answer is (or at least can be) somewhat non-trivial.

首先,您可以选择两种不同的高度:字符本身的高度,或正常"字符所在的单元格的高度(但是,例如,带有像 y 通常会延伸到单元格之外).因此,对于相同大小的字体,字符单元格通常会比字符本身的大小略大.

First of all, there are two separate heights you can choose from: the height of the characters themselves, or the height of cell in which a "normal" character lives (but, for example, letters with descenders like y will usually extend outside the cell). So, for the same size of font, the character cell will usually be slightly larger than the size of the characters themselves.

要根据字符单元格高度选择字体,请为高度传递一个正数.要根据字符本身的高度进行选择,请传递一个负数(其绝对值将用作高度).

To choose the font based on the character cell height, you pass a positive number for the height. To choose based on the height of the characters themselves, you pass a negative number (and its absolute value will be used as the height).

所以让我们假设您希望字符他们自己高 11 磅(因此我们需要为高度传递一个负数),并且您希望宽度为默认值对于手头的字体.我们还假设您正在处理 MM_TEXT 模式,其中一个逻辑单元等于一个像素.

So let's assume that you want the characters themselves to be 11 points tall (so we'll need to pass a negative number for the height), and that you want the width to be the default for the font at hand. Let's also assume that you're dealing with MM_TEXT mode, where a logical unit is equal to one pixel.

当 Windows 使用该术语时,一点是 1/72nd 英寸(注意:72 点到英寸不是通用的——例如,有些使用 72.72 点到英寸相反——如果您需要遵守点"的不同定义,请随意修改以下代码).

As Windows uses the term, one point is 1/72nd of an inch (note: 72 points to the inch isn't universal--for example, some use 72.72 points to the inch instead--feel free to modify the following code if you need to comply with a different definition of a "point").

因此,要计算大小,我们可以先检索屏幕上每英寸(垂直)的像素数,然后计算正确点数的像素数:

So, to compute the size, we can start by retrieving the number of pixels per inch (vertically) on the screen, then compute the number of pixels for the right number of points:

static const int points_per_inch = 72;
int pixels_per_inch = GetDeviceCaps(hDC, LOGPIXELSY);
int pixels_height = - (points * pixels_per_inch / points_per_inch);

[Microsoft 有一个 MulDiv 函数来确保在该计算中不会出现中间值溢出,但除非您使用的是 16 位机器或真正巨大的字体,否则这主要是搜索中的解决方案一个问题.]

[Microsoft has a MulDiv function to assure against an intermediate value overflowing in that computation, but unless you're using a 16-bit machine or a truly immense font, that's mostly a solution in search of a problem.]

因为我们想要默认宽度,所以我们将传递 0 作为宽度.

Since we want the default width, we'll pass 0 for the width.

所以,我们最终得到了这样的结果:

So, we end up with something like this:

HFONT font = CreateFont(pixels_height, 0, // size
                        0, 0, 0, // normal orientation
                        FW_NORMAL,   // normal weight--e.g., bold would be FW_BOLD
                        false, false, false, // not italic, underlined or strike out
                        DEFAULT_CHARSET,
                        OUT_OUTLINE_PRECIS, // select only outline (not bitmap) fonts
                        CLIP_DEFAULT_PRECIS,
                        CLEARTYPE_QUALITY,
                        VARIABLE_PITCH | FF_SWISS,
                        "Arial");

这篇关于如何使用 CreateFontA 设置字体大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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