While looking at ways of improving the UI of a dialog in an
application, I wanted to display some status text in a
control that was empty. The default Windows Forms
(which uses the underlying native Win32 control) doesn't support
this, but with a little effort we can extend the control.
Control receives either the
WM_ERASEBKGND messages, it will check to see if the
ControlStyles.UserPaint style is set. If set then the
WM_PAINT message will cause the
Paint event to be raised,
PaintBackground event - but only
if the the
AllPaintingInWmPaint style is not set.
For both messages, if the
UserPaint style is not set, then the
control will call the default window procedure allowing that to
handle the message.
This is important to note, as for certain controls (such as
ListBox which wrap a native window) the
UserPaint style is
not set, meaning the paint events are never raised. If you try
and set the flag yourself, then you will find the paint events
work again - but the native control will stop painting correctly
due to the default window procedure not being called.
Unfortunately, while you can manually call the default window
procedure via the
DefWndProc method, you won't have access to
the original message data to pass to it.
Based on the above primer, we now know that we can't easily use
OnPaint to provide our custom drawing. Instead, we'll
WM_PAINT message when it arrives for our control
and initiate painting manually.
Controlclass offers many events for easily hooking into various actions, it isn't possible to hook into window procedures in this manner. The simplest solution is to create an inherited class and then override the
Even though we're very slightly going outside the box to
intercept windows messages, we don't need to actually use any
Win32 calls. Instead we call
CreateGraphics to get a
Graphics instance for our control and paint away as we
In this example it will print the message centred in the middle of the list with word wrapping enabled.
There's just one flaw with the above code - as soon as you
resize the control, it will paint the text again without
clearing the existing content, which can result in a bit of a
mess. As I discussed above, Windows uses the
notify a window that it should erase its background and so if we
WndProc to intercept this message we can clean up
This time I'm simply instructing the control to draw
itself, which will cause the underlying native window to repaint
its background ready for our re-positioned text to be
In the original posting of this article, I'd accidentally
14 which is actually
WM_GETTEXTLENGTH. So the example managed to work only by
WM_ERASEBKGND is the wrong
approach as it leads to mass flicker. In the revised version, I
just manually erase the background.
And that is pretty much it, short and sweet - the associated download includes an updated fully functional demonstration project.
While this article describes extending the
ListBox control, it
should be possible to use in other controls too. For example, I
use the exact same technique to add empty text support to the
- 2018-04-28 - First published
- 2020-11-22 - Updated formatting
Like what you're reading? Perhaps you like to buy us a coffee?