Click here to Skip to main content
15,921,382 members
Please Sign up or sign in to vote.
3.29/5 (3 votes)
See more:
I'm currently writing a custom control for use with a WinForms application - basically, it's a hex editor control, which displays offsets, rows of bytes, a text column, and some other information.

I'm doing all the drawing inside OnPaint, but I've started to notice that rendering becomes a little laggy when the control is large (and thus a lot of bytes are being drawn). In extreme cases it can take around 20 to 30 milliseconds to draw the entire control. This isn't terrible, but it makes dragging selections feel very slightly sluggish, and I'm sure it's much worse on lower-end machines.

The control is entirely text based - aside from a couple Graphics.DrawPath/FillPath calls, the bulk of the GDI+ calls are to Graphics.DrawString (one for each byte displayed, to allow for custom column spacing). These DrawString calls end up taking up the vast majority of the rendering time.

Is there any faster way to do text drawing in GDI+? It seems that it would be a good move to try to reduce the number of DrawString calls, but how can I do that while still maintaining control over the spacing of characters?

Perhaps there's another, simpler way of rendering text for this purpose that I'm just unaware of? Any advice would be appreciated.
Posted

1 solution

It is a hard art to master, but text can be drawn very fast under GDI+. There is just too much detail to go into, but you can look at my 'AdvancedTextBox' control in xacc.ide. The source code is on sourceforge.

To summarize:
- Tokenize data
- Optimize tokens by grouping similar ones (store this and 'invalidate' if the 'line/text' changes)
- Paint the groupings

I would also suggest sticking to monospace fonts. Then only 1 call is needed to get the width of a character.

 
Share this answer
 


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900