在 Win32 API 中绘制格式化文本的最快方法是什么? [英] What is the fastest way to draw formatted text in the Win32 API?

查看:30
本文介绍了在 Win32 API 中绘制格式化文本的最快方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 C++ 中实现一个文本编辑器,仅使用 vanilla Win32 API,我正在尝试找到实现语法突出显示的最佳方法.我知道有像 scintilla 这样的现有控件,但我这样做是为了好玩,所以我想自己完成大部分工作.我还希望它既快速又轻便.

I am implementing a text editor in C++ just using the vanilla Win32 API and I'm trying to find the best way to implement syntax highlighting. I know that there are existing controls out there like scintilla, but I'm doing this for fun so I want to do most of the work myself. I also want it to be fast and lightweight.

从我目前学到的知识来看,在 GDI 中绘制文本的最底层选项似乎是 TextOut 函数.但是,如果我需要不断更改字体颜色,那么这意味着我需要多次调用 TextOut 以绘制具有混合格式的文本正文.这是低效的吗?当实现语法​​高亮和富文本控件时,它们是否有可能在幕后使用 TextOut 还是有其他方式?在 GDI 中绘制文本的所有其他方法是否只是围绕 TextOut 的更高级别的包装?

From what I've learned so far, it looks like the most low level option for drawing text in GDI is the TextOut function. However, if I need to keep changing the font color then that means I will need to make many calls to TextOut in order to draw one body of text with mixed formatting. Is this inefficient? When syntax highlighting and rich text controls are implemented, would they be likely to use TextOut behind the scenes or is there some other way? Is every other method of drawing text in GDI just a higher level wrapper around TextOut?

推荐答案

DrawText 和 TextOut 都是 ExtTextOut 的包装器,所以 ExtTextOut 是低级 API.根据我的经验,ExtTextOut 速度非常快,所以我怀疑您会发现 ExtTextOut 本身存在任何性能问题.但是,创建/选择字体可能是性能问题的根源,因此如果您在字体之间来回切换,您可以通过缓存和重用字体 (HFONT) 而不是每次都使用 CreateFont/SelectObject/DeleteObject 来实现显着的性能提升.基本上,在创建新字体后第一次调用 SelectObject 时,Windows 将执行字体匹配过程,为您请求的逻辑字体找到最佳物理字体.这是一个相当复杂的过程,因此您希望尽量减少在性能很重要的情况下发生的次数.

Both DrawText and TextOut are wrappers for ExtTextOut, so ExtTextOut is the low-level API. In my experience, ExtTextOut is pretty fast, so I doubt you'll see any performance issues with ExtTextOut itself. However, creating/selecting fonts can be a source of performance issues, so if you are switching back and forth between fonts, you can realize significant performance gains by caching and reusing fonts (HFONT) rather than CreateFont / SelectObject / DeleteObject each time. Basically, the first time you call SelectObject after creating a new font, Windows will perform a font matching process to find the best physical font for the logical font that you have requested. This is a fairly complex process, so you want to minimize the number of times that occurs in situations where performance is important.

多年前我开发了一个富编辑控件,它本质上是 Microsoft Word 的迷你版.我使用 ExtTextOut 作为所有文本输出的主要工具.该控件将维护最近使用的字体的字体缓存(默认缓存大小为 10 种字体).它支持 WYSIWYG 布局,所以它实际上是使用打印机 DC 和字体进行所有布局,然后使用屏幕 DC 和类似字体呈现屏幕兼容版本,因此有很多额外的工作正在进行,可能不适用于你的情况.即便如此,在当时的典型硬件(例如,266 mhz Pentium)上运行的性能也非常出色.

I developed a rich edit control many years ago that was essentially a mini version of Microsoft Word. I used ExtTextOut as the main workhorse for all text output. The control would maintain a font cache of the most recently used fonts (default cache size was 10 fonts). It supported WYSIWYG layout, so it was actually doing all layout using a printer DC and fonts, then would render a screen compatible version using a screen DC and similar fonts, so there was a lot of extra work going on that likely is not applicable to your situation. Even so, performance was excellent running on typical hardware of the day (e.g., 266 mhz Pentium).

这篇关于在 Win32 API 中绘制格式化文本的最快方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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