Painting the Dialog Backround

Generally, it’s not necessary to change the dialog’s default background. Besides, if have to display an image in a dialog, it is preferable to do it in a child control (e.g. static picture or custom control). However, let’s say that’s a requirement or we want to do it as an exercise or just for fun.

Painting the Dialog Background

Painting the Dialog Background

The image shows a dialog that has its background painted with a fancy brush. It also has a nice Earth image, transparently painted directly in the dialog’s client area.
Let’s see how to make it!.

Handling WM_ERASEBKGND message

This example handles the WM_ERASEBKGND message and fills the background by using a custom brush.  Finally, it returns TRUE to prevent further erasing.

Next’ we would notice that’s necessary to handle WM_CTLCOLOR in order to change also the beackground of some child controls (static, radio buttons, etc.). As long as, WM_CTLCOLOR allows also changing the dialog’s background, we can get rid of previous method and do all the work in the WM_CTLCOLOR message handler.

Handling WM_CTLCOLOR message

To change the default background as well as other attributes, WM_CTLCOLOR message handler (OnCtlColor) must return a not-NULL brush handle.

Note: In Win32, WM_CTLCOLOR notification message was replaced by WM_CTLCOLORDLG, WM_CTLCOLORSTATIC, and so on. However, MFC still keeps WM_CTLCOLOR for 16-bit legacy code compatibility. The third argument (nCtlColor) of OnCtlColor handler function, helps us to decide if in fact has been received one of the following notifications: WM_CTLCOLORDLG, WM_CTLCOLORSTATIC, etc.

Handling WM_PAINT message

Finally, let’s “paint the Earth”! As in any other kind of window, the right place to perform paining is the WM_PAINT message handler.

Demo project

Download the demo project and you can find more implementation details.


See also

4 thoughts on “Painting the Dialog Backround

  1. Victor Nijegorodov

    Hi Ovidiu,

    it is a good article.
    There are, however, some questions about how the background of the dialog controls should look like.
    In your example it is the same as the parent dialog has. But in some cases it could be better to leave the default one (to see the control boarders for example, or maybe some other reason.) In such a case you could extend your code using something similar to what J.Newcomer suggested in his essay “Changing Dialog Box/CFormView Background Color”:

    With the best regards,

  2. Akash Das

    Hi I use this function OnCtlColor(..) but it coudnt change the color of group box frame background as per you dialog box. what should it do please help!

  3. ForStarter

    Thanks for your great help with this article. It helped me out quite a lot. But I still have some questions regarding how to change not only the background of the dialog, but also the background of other Controls like buttons, groupboxes (frame and caption). I am using win32 api and got most of the controls changed like editfields, text, etc. But can’t get the buttons done, any help is welcome.

    Best regards a starter 🙂


Leave a Reply