OwnerDraw TextBox
-
I am trying to create an owner-drawn TextBox control, but as anyone who may have tried this knows, overriding the OnPaint method is useless as the base class uses the Windows API for painting. I know I can capture the WM_NCPAINT message in WndProc, but the absolute most you can do with this in reality is paint a custom border for the TextBox, anything else just gets painted over by the base class. So the question is, how can I paint the TextBox without having to resort to building the whole damn thing from scratch? Has anyone actually seen a custom painted TextBox actually implementsd in .Net? Or (hopefully) am I missing something here?
-
I am trying to create an owner-drawn TextBox control, but as anyone who may have tried this knows, overriding the OnPaint method is useless as the base class uses the Windows API for painting. I know I can capture the WM_NCPAINT message in WndProc, but the absolute most you can do with this in reality is paint a custom border for the TextBox, anything else just gets painted over by the base class. So the question is, how can I paint the TextBox without having to resort to building the whole damn thing from scratch? Has anyone actually seen a custom painted TextBox actually implementsd in .Net? Or (hopefully) am I missing something here?
I can't really test it since I don't have .NET at work, but from the class outlook, I can see that : - TextBox inherits the PaintEventHandler from its base class (TextBoxBase) : have you tried to implement an handler for it ? - I don't know why overriding WndProc and processing WM_PAINT (not WM_NCPAINT) should not let you do exactly what you want. In addition to WM_PAINT, I believe you should process WM_ERASEBKGND as well. In fact what those WIN32 messages are for is as follows : - WM_ERASEBKGND : background - WM_PAINT : actual content - WM_NCPAINT : border It's reasonable to think that the .NET PaintEvent event is thrown whenever the base class receives (reflected) the WM_PAINT message.
-
I can't really test it since I don't have .NET at work, but from the class outlook, I can see that : - TextBox inherits the PaintEventHandler from its base class (TextBoxBase) : have you tried to implement an handler for it ? - I don't know why overriding WndProc and processing WM_PAINT (not WM_NCPAINT) should not let you do exactly what you want. In addition to WM_PAINT, I believe you should process WM_ERASEBKGND as well. In fact what those WIN32 messages are for is as follows : - WM_ERASEBKGND : background - WM_PAINT : actual content - WM_NCPAINT : border It's reasonable to think that the .NET PaintEvent event is thrown whenever the base class receives (reflected) the WM_PAINT message.
Thanks heaps for the suggestions, but unfortunately I'm still no closer to my goal. Intercepting WM_PAINT instead of WM_NCPAINT still only gives me the ability to paint the same 2px wide border - not the background itself. WM_ERASEBKGND also doesn't get me anywhere as the border and control are painted after it anyway. The problem is that the text area is being overlaid on top, wiping out any background painting you do. Even if I were happy to only have access to paint a 2px wide border, the problem when doing that is that it totally messes up the scrollbar painting.. Uggghhh.
-
Thanks heaps for the suggestions, but unfortunately I'm still no closer to my goal. Intercepting WM_PAINT instead of WM_NCPAINT still only gives me the ability to paint the same 2px wide border - not the background itself. WM_ERASEBKGND also doesn't get me anywhere as the border and control are painted after it anyway. The problem is that the text area is being overlaid on top, wiping out any background painting you do. Even if I were happy to only have access to paint a 2px wide border, the problem when doing that is that it totally messes up the scrollbar painting.. Uggghhh.
You didn't say, so just to confirm: are you are inheriting from TextBox or TextBoxBase, and explicitly overriding OnPaint and OnPaintBackground, and are calling SetStyle with the ControlStyles.UserPaint (and optionally AllPaintingInWmPaint and DoubleBuffer)? If you comment-out the normally-required base.OnPaint call, the text is still rendered by the CLR stuff?
-
You didn't say, so just to confirm: are you are inheriting from TextBox or TextBoxBase, and explicitly overriding OnPaint and OnPaintBackground, and are calling SetStyle with the ControlStyles.UserPaint (and optionally AllPaintingInWmPaint and DoubleBuffer)? If you comment-out the normally-required base.OnPaint call, the text is still rendered by the CLR stuff?
Hi Jeff, I'm inheriting from TextBox not TextBoxBase as I wish to maintin all the functionality of the TextBox control. Overriding OnPaint and/or OnPaintBackground has odd effects, regardless of what Styles have been set, as the TextBox (or TextBoxBase) is overpainting the control with WndProc intercepts.
-
Hi Jeff, I'm inheriting from TextBox not TextBoxBase as I wish to maintin all the functionality of the TextBox control. Overriding OnPaint and/or OnPaintBackground has odd effects, regardless of what Styles have been set, as the TextBox (or TextBoxBase) is overpainting the control with WndProc intercepts.
I'll have to remember that, thanks for the heads-up. I suppose I should be glad I wrote a binary editor derived straight from Control, as I can see I would have had the problems you're having. Of course, much of TextBox's functionality didn't apply for me. Still a pain in the "details", so I see why you're trying to avoid it. Still, I'm curious as to why the problems. I'll post back if I find anything.