Virtual Developer Workshop: Containerized Development with Docker

(Fabi Pantera)

Environment: Windows NT 4 SP5, Visual C++ 6 SP2

This source code demonstrates how you can add buttons to dialog caption bar and also how you can paint the caption.

A lot of peoples asked me to show how they can add buttons to the caption. Of course, first the question is:
Is it possible to add buttons to the non-client area?
The answer is NO.
So what is this article about? Well, this demo shows how to add buttons to the non-client area.

As you probably now you can add controls only in the client area. To add something similar in the non-client area, you have to draw everything by yourself and handle the mouse message for the non-client area. For this you have just to implement WM_NCPAINT message and draw whatever you want. I drawed a gradient caption and some buttons:

void CTemplateDlg::OnNcPaint()
 Default();//call default to let windows handle first
 CRect rc, rcwin; 
 CalculateCaptionTextRect(this, &rc);
 CDC* pDC = GetWindowDC();
 CDC dcMem;
 CBitmap bitmap;
 bitmap.CreateCompatibleBitmap(pDC, rc.Width(), rc.Height());
 CBitmap* pOldBitmap = dcMem.SelectObject(&bitmap);
 CSize sizeButton(GetSystemMetrics(SM_CXSIZE),
 DoGradientFill(this, acLeft, m_startColor, m_endColor);
 rc.right -= 5;
 rc.top += 1;
 rc.bottom -= 3;
 CRect rcBitBlt = rc;
 for (int i = 0; i < 6; i++)
  rc.right -= sizeButton.cx;
  rc.left = rc.right - sizeButton.cx;
  switch (i)
   case 0:
     if (mPushedButton[i] == TRUE)
     //this draw button
      dcMem.DrawFrameControl(rc, DFC_BUTTON , 
      dcMem.DrawFrameControl(rc, DFC_BUTTON , 
   case 1:
After this, you should treat the WM_NCHITTEST, WM_NCLBUTTONDOWN, WM_LBUTTONUP messages and force a redraw in the non-client area by sending a WM_NCPAINT message:
For now, that's all for the custom drawing in the non-client area. Anyway, this demo can be reused. The class that implements the previous behaviour is called CTemplateDlg. This class is an abstract class which provide in its interface, 6 virtual functions: Button0, BUtt1,...,Button5. This are the "callback" functions which handle the left button up click message on every button. So, to use this class just derive your CDialog class from CTemplateDlg and simply implement this functions. Of course you also have to modify the construction, but you can take a look in the code.

If everyone need to modify this code or to create a smart control don't hesitate to e-mail me.


Download demo project - 24 Kb


  • Does not work with Windows Vista

    Posted by pavitx on 08/21/2009 12:09pm

    Nice article but so old it does not work with Windows Vista. The caption area has flicker (yes) and none of the buttons works. A real pity. I converted the dsp project into Visual Studio 2008.

  • put CButton on caption - is it possible???

    Posted by Legacy on 08/10/2002 07:00am

    Originally posted by: mamont

    I'm shocked!!
    How put a ownerdrawn button on the caption does anyone know???

    thx for any advices...

  • dialog

    Posted by Legacy on 06/13/2002 07:00am

    Originally posted by: Ramya

    how to creat a dialog boex,
    how to modify help in dialog box

  • Can i add the buttons with my own GUI

    Posted by Legacy on 11/13/2001 08:00am

    Originally posted by: vivek dabral

    Can i add the minimize & close button which have the appearance as my bitmap.
    How & which handlers can i call on mouse clicking?

    Thanx in advance
    Vivek Dabral

  • Thank you!

    Posted by Legacy on 05/21/2001 07:00am

    Originally posted by: Nick Kong

    Thank you , It's cool! just i want

  • Logical Syntax Error

    Posted by Legacy on 01/27/2000 08:00am

    Originally posted by: Paul Mestemaker II

    On line 642 of the TemplateDlg.cpp file there is an error:
    int iB = GetGValue(crStart);
    I am assuming that this should be:
    int iB = GetBValue(crStart);

  • Great job.

    Posted by Legacy on 01/26/2000 08:00am

    Originally posted by: Dani

    You've made a great job.
    I like it.

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date