August 1st, 2005

Rendering standard Windows elements

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.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.

Feedback