Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Overriding OnPaint for TextBox

I've tried to override the OnPaint method of TextBox, RichTextBox and other Windows native controls, but in vain, my instance never got called.

I did something like (C#):

protected override void OnPaint(PaintEventArgs e)
{
  base.OnPaint(e);
  e.Graphics.DrawString(Text, Font,
                                  new SolidBrush(ForeColor),               
                                  ClientRectangle, style);
}

Is it possible to overide onPaint for Windows native control from a managed code at all or should I go low-level (subclassing)?

Anyone did it before?

Vlad Gudim
Wednesday, April 14, 2004

All the standard controls are merely wrappers around Win32 controls. The Windows system (Comctl.dll or whatever) does all the painting, and you can't change it.

I think there's some property you can set to draw the controls yourself but then you'll have to draw _everything_ yourself since the Win32 code no longer gets called.

Chris Nahr
Wednesday, April 14, 2004

That's what I've suspected. It's simply not fair!

Well is there a way of subclassing a control from C# + .NET, even if its "unsafe"? Anyone done it before?

Vlad Gudim
Wednesday, April 14, 2004

Try overriding WndProc and catching WM_PAINT.

Duncan Smart
Thursday, April 15, 2004

Had the same problem today.
The call of
"this.SetStyle(ControlStyles.UserPaint, true);"
in my ctor solved the problem: OnPaint is called now.

Uwe
Friday, April 23, 2004

*  Recent Topics

*  Fog Creek Home