Rendering standard Windows elements

Raymond Chen

The DrawFrameControl function allows you to render standard Windows elements in your custom controls. Let’s start by simply rendering a selected radio button. Start with our new scratch program and make this very simple change:

class RootWindow : public Window
{
 …
protected:
 void PaintContent(PAINTSTRUCT *pps);
 …
};

void RootWindow::PaintContent(PAINTSTRUCT *pps) { RECT rc = { 0, 0, 32, 32 }; DrawFrameControl(pps->hdc, &rc, DFC_BUTTON, DFCS_BUTTONRADIO | DFCS_CHECKED); }

When you run the program, you’ll see a little radio button in the corner. Woo-hoo.

You might also notice that it’s an unthemed radio button. To get a themed radio button, you need to use the theme-drawing functions defined in the uxtheme.h header file. Let’s make the following further changes:

class RootWindow : public Window
{
 …
protected:
 void OpenTheme() { m_htheme = OpenThemeData(m_hwnd, L”Button”); }
 void CloseTheme()
 {
  if (m_htheme) { CloseThemeData(m_htheme); m_htheme = NULL; }
 }
 RootWindow() : m_htheme(NULL) { }
 ~RootWindow() { CloseTheme(); }
 …
};

LRESULT RootWindow::OnCreate() { OpenTheme(); return 0; }

void RootWindow::PaintContent(PAINTSTRUCT *pps) { RECT rc = { 0, 0, 32, 32 }; if (m_htheme) { DrawThemeBackground(m_htheme, pps->hdc, BP_RADIOBUTTON, RBS_CHECKEDNORMAL, &rc, NULL); } else { DrawFrameControl(pps->hdc, &rc, DFC_BUTTON, DFCS_BUTTONRADIO | DFCS_CHECKED); } }

LRESULT RootWindow::HandleMessage(…) { … case WM_THEMECHANGED: CloseTheme(); OpenTheme(); break; … }

This new version attempts to open the “Button” theme for the window when the window is created. If themes are not enabled, then this call will fail. When it comes time to draw, we see whether we have a theme available. If so, then we use the DrawThemeBackground function to draw it; otherwise, we draw it the unthemed way. Of course, we close the theme handle at destruction, and we also refresh the theme handle if the user changes the theme.

If you run this new program with themes enabled, then you will get the pretty themed radio button instead of the old-fashioned unthemed radio button.

Next time, we’ll look at the trickier menu bitmaps.

0 comments

Discussion is closed.

Feedback usabilla icon