自动调整静态控件大小的正确方法是什么? [英] What is the correct way to autosize a Static control?

查看:95
本文介绍了自动调整静态控件大小的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将静态控件的大小调整为其内容大小,因此需要首先计算其文本内容的大小。我找到了一种使用 GetTextExtentPoint32 来计算大小的方法,但是我需要首先将DC的字体设置为与控件的字体相同。有一个更好的方法吗?我已经设置了静态控件的字体一次,我想也许我不需要第二次设置DC的字体。

I want to adjust a Static control's size to its content size, so I need to calculate the size of its text content first. I found a way to use GetTextExtentPoint32 to calculate the size, but I need to set the DC's font to the same as the control's font first. Is there a better way to do this? I've set the Static control's font once, I think maybe I don't need to set the DC's font the second time.

什么是最好的计算静态控件的文本内容的大小?还有一种更好的方法来自动调整静态控件的大小吗?

What is the best way to calculate the size of a Static control's text content? And is there a better way to autosize the Static control?

推荐答案

在我看来,您已经找到正确的方法了做到的方式。呼叫 GetTextExtentPoint32 找出给定包含该文本的控件的理想大小,然后将其调整为计算出的大小。

It sounds to me like you've already figured out the correct way to do it. Call GetTextExtentPoint32 to figure out the ideal size of the control given the text that it contains, and then resizing the control to the calculated size.

这是很多工作,但这就是使用原始Win32 API时发生的情况。您没有方便的包装器库,可以通过 Control.AutoSize()函数为您抽象所有这些内容。您可以轻松地编写自己的函数并重新使用它,但是Win32标准控件不会公开自动调整大小 API。

It's a lot of work, but that's what happens when you're working with the raw Win32 API. You don't have a handy wrapper library that abstracts all this for you in a Control.AutoSize() function. You could easily write your own function and re-use it, but the Win32 standard controls do not expose an "auto-size" API.

就字体而言,您将<确定>确定必须确保设备上下文使用与控件相同的字体,否则将计算错误的大小。但是,您不必创建新的设备上下文,请求处理静态控件的字体并将其选择到新的DC中。相反,您可以使用 <$ c来使用静态控件的DC。 $ c> GetDC 函数并将句柄传递到静态控制窗口。确保如果您呼叫 GetDC ,则始终跟进对 ReleaseDC 完成后!

As far as the font, you will definitely need to make sure that the device context is using the same font as the control, otherwise you'll calculate the wrong size. But you don't have to create a new device context, request a handle the static control's font, and select that into your new DC. Instead, you can just use the static control's DC using the GetDC function and passing in the handle to your static control window. Make sure that if you call GetDC, you always follow up with a call to ReleaseDC when you're finished!

但是,请注意 GetTextExtentPoint32 函数的一些警告可能会干扰您计算出的大小的准确性:

However, do note some caveats of the GetTextExtentPoint32 function that may interfere with the accuracy of the size you calculate:


  • 它忽略剪切。

  • 它不考虑换行符( \n )或回车符( \r\n )。

  • 它不考虑前缀字符(如果您的静态控件没有 SS_NOPREFIX 样式

  • 鉴于字距调整,它可能不会返回准确的结果可能会实施

  • It ignores clipping.
  • It does not take into account new lines (\n) or carriage returns (\r\n) when computing the height.
  • It does not take into account prefix characters (those preceded in the string with ampersand) and used to denote keyboard mnemonics if your static control does not have the SS_NOPREFIX style.
  • It may not return an accurate result in light of the kerning that may be implemented automatically by some devices.

(这在链接的文档中都有提及,但实际上有人读过吗?)

也许更简单的选择是使用静态控件已经在执行的方式绘制文本。除非您设置了 SS_SIMPLE 样式(使用 TextOut ExtTextOut 作为优化来绘制文本),静态控件通过调用 DrawText 函数给定设置的其他控件样式,请使用适当的参数(参考 )。

Perhaps an easier alternative is to draw the text the same way that the static control is already doing. Unless you have the SS_SIMPLE style set (which uses TextOut or ExtTextOut to draw text as an optimization), static controls draw their text by calling the DrawText function with the appropriate parameters, given the other control styles that are set (reference).

您可以做完全相同的事情,并将 DT_CALCRECT 标志添加到 DrawText 函数,该函数确定绘制指定文本而无需实际绘制文本所需的矩形的宽度和高度。

You can do exactly the same thing, and add the DT_CALCRECT flag in your call to the DrawText function, which causes it to determine the width and height of the rectangle required to draw the specified text without actually drawing the text.

这篇关于自动调整静态控件大小的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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