简体   繁体   English

在Win32 API中绘制格式化文本的最快方法是什么?

[英]What is the fastest way to draw formatted text in the Win32 API?

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. 我正在使用vanilla Win32 API在C ++中实现文本编辑器,并且我正在尝试找到实现语法突出显示的最佳方法。 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. 我知道有像scintilla那样的现有控件,但我这样做是为了好玩,所以我想自己完成大部分工作。 I also want it to be fast and lightweight. 我也希望它快速轻巧。

From what I've learned so far, it looks like the most low level option for drawing text in GDI is the TextOut function. 从我到目前为止所学到的,看起来在GDI中绘制文本的最低级别选项是TextOut函数。 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. 但是,如果我需要不断更改字体颜色,那么这意味着我需要多次调用TextOut才能绘制一个混合格式的文本体。 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? 当实现语法​​高亮和富文本控件时,他们是否可能在幕后使用TextOut或者还有其他方法吗? Is every other method of drawing text in GDI just a higher level wrapper around TextOut ? 在GDI中绘制文本的其他方法是否只是TextOut的更高级别的包装器?

Both DrawText and TextOut are wrappers for ExtTextOut, so ExtTextOut is the low-level API. DrawText和TextOut都是ExtTextOut的包装器,因此ExtTextOut是低级API。 In my experience, ExtTextOut is pretty fast, so I doubt you'll see any performance issues with ExtTextOut itself. 根据我的经验,ExtTextOut非常快,所以我怀疑你会看到ExtTextOut本身的任何性能问题。 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. 但是,创建/选择字体可能是性能问题的根源,因此如果您在字体之间来回切换,则可以通过缓存和重用字体(HFONT)而不是每次使用CreateFont / SelectObject / DeleteObject来实现显着的性能提升。 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. 基本上,在创建新字体后第一次调用SelectObject时,Windows将执行字体匹配过程,以找到您请求的逻辑字体的最佳物理字体。 This is a fairly complex process, so you want to minimize the number of times that occurs in situations where performance is important. 这是一个相当复杂的过程,因此您希望最大限度地减少性能很重要的情况下发生的次数。

I developed a rich edit control many years ago that was essentially a mini version of Microsoft Word. 多年前我开发了一个丰富的编辑控件,它本质上是Microsoft Word的迷你版本。 I used ExtTextOut as the main workhorse for all text output. 我使用ExtTextOut作为所有文本输出的主要工具。 The control would maintain a font cache of the most recently used fonts (default cache size was 10 fonts). 该控件将维护最近使用的字体的字体缓存(默认缓存大小为10种字体)。 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. 它支持WYSIWYG布局,所以它实际上是使用打印机DC和字体进行所有布局,然后使用屏幕DC和类似字体渲染屏幕兼容版本,因此有很多额外的工作正在进行,这可能不适用于你的情况。 Even so, performance was excellent running on typical hardware of the day (eg, 266 mhz Pentium). 即便如此,在当天的典型硬件上运行性能也非常出色(例如,266 mhz Pentium)。

相反,考虑其“绘制文本”功能是最快的,它可能是更有利的考虑,“我怎样才能减少我在所有呈现的文本量”,通过聪明什么重绘/无效文本的变化或者如何缓存渲染文本以​​进行滚动。

For complex usage you probably want DrawText as it gives you more control than TextOut . 对于复杂的用法,您可能需要DrawText因为它比TextOut提供更多控制。 It has some basic formatting support, but less than you'll need for an editor. 它有一些基本的格式化支持,但比编辑器需要的少。 The next step up is the rich text editor from the common controls library, which pretty much takes care of all that for you. 下一步是来自通用控件库的富文本编辑器,它几乎可以满足您的所有需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM