[.net] Drawing custom graphics on existing controls

Started by
1 comment, last by BlackGhost 18 years, 5 months ago
I'm trying to do some kind of code editor by overriding the RichTextBox class, so I want to highlight a line of code. The obvious way to do that seemed to be to override the OnPaint stuff and draw a rectangle there, with a height equal to the line's height. However, that doesn't seem to work, and any drawing code does not seem to actually draw anything, maybe getting overwritten by the RichTextbox's visuals even though I'm never calling the base class' onPaint. Is there is a way to actually draw on the controll I'm inheriting from? Thanks for any input.
Advertisement
RichTextBox has always been hard to modify for a whole bunch of reasons. You might want to try subclassing it and overriding the WndProc function. Look for WM_PAINT events, pass them to the base class then do your own drawing on top. Might work. It would mean you'd have to use GDI calls on an HDC unless there's some way to initialize a Graphics object from one.

But you could probably acheive the same effect justing the RichTextBox's native features - it'll allow you to assign different fonts to different ranges of text in the box. So make a Red font or a Green font and apply those.

I mean, I assume you are trying to do this in a chroma-coding sort of manner.
Thanks, I will probably try that. And yes, there are Graphics.FromHDC and Graphics.FromHwnd functions that create the managed graphics object from native handles. I know about the richtextbox being able to change text colors and fonts, but i need more than that (e.g. highlight the whole background of the line, not just its text).

This topic is closed to new replies.

Advertisement