Hi Juan,
The PaintEventArgs parameter of OnPaintBackground() method is hard coded.
It always contains the client rectangle of the target control. It is setup
in the Control.WmEraseBkgnd() method, like below:
//From .Net Reflector
private void WmEraseBkgnd(ref Message m)
{
.....
NativeMethods.RECT rect = new NativeMethods.RECT();
UnsafeNativeMethods.GetClientRect(new HandleRef(this,
this.Handle), ref rect);
PaintEventArgs e = new PaintEventArgs(wParam,
Rectangle.FromLTRB(rect.left, rect.top, rect.right, rect.bottom));
this.PaintWithErrorHandling(e, 1, true);
.....
}
As you can see, the Winform code sets up PaintEventArgs structure with
values from GetClientRect() Win32 API, so this is expected.
PaintWithErrorHandling() finally calls OnPaintBackground() method by
passing this PaintEventArgs.
Regarding why OnPaintBackground() is called, it is determined by the
ControlStyle of the UserControl. Basically, this message is determined by
the 3rd parameter of the Win32 InvalidateRect() API. In the source code of
"Invalidate()", we can see:
public void Invalidate(bool invalidateChildren)
{
if (this.IsHandleCreated)
{
if (invalidateChildren)
{
SafeNativeMethods.RedrawWindow(new HandleRef(this.window,
this.Handle), (NativeMethods.COMRECT) null, NativeMethods.NullHandleRef,
0x85);
}
else
{
using (MultithreadSafeCallScope scope = new
MultithreadSafeCallScope())
{
SafeNativeMethods.InvalidateRect(new HandleRef(this.window,
this.Handle), (NativeMethods.COMRECT) null, (this.controlStyle &
ControlStyles.Opaque) != ControlStyles.Opaque);
}
}
this.NotifyInvalidate(this.ClientRectangle);
}
}
As you can see, .Net Winform code use (this.controlStyle &
ControlStyles.Opaque) != ControlStyles.Opaque) logic to determine whether
to erase the control background or not. Since your control did not
ControlStyles.Opaque style by default, the "bErase" parameter is always
true for InvalidateRect() API. This generates the WM_ERASEBKGND message
when BeginPaint function is finally called. Please see the MSDN link below
for details:
http://msdn2.microsoft.com/en-us/library/ms534893(VS.85).aspx
Hope this helps.
Best regards,
Jeffrey Tan
Microsoft Online Community Support
=========================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@microsoft.com.
This posting is provided "AS IS" with no warranties, and confers no rights.