Custom buttons in CFrameWnd caption?

Posted on 2000-04-30
Medium Priority
Last Modified: 2013-11-20
Does anyone know how I can draw my own buttons in a FrameWnd caption?  Maybe the answer lies in ::DrawCaption()?   But if so, I can't get it to work.
Question by:Bill Nolan
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
LVL 12

Expert Comment

ID: 2772382
look at www.codeguru.com
here is example how to draw custom window caption

Author Comment

by:Bill Nolan
ID: 2774016
*Where?* This seems to be a local favorite here.  Several times I've been referred to codeguru.com, yet given absolutely no idea on how to find the article in question,  just "here is an example". In this case, there is nothing in their cute database that comes up under anything related to this topic.  And I can't find anything in the highlighted sections, either.
LVL 12

Expert Comment

ID: 2783800
LVL 12

Expert Comment

ID: 2784060
also look at dialog section (same site) there is article about adding controls to the dialog caption/ The technique is the same for any non dialog window.

Accepted Solution

JunHaan earned 300 total points
ID: 2821991

To have a button in the title bar, you'd need to create a 'pseudo' button. We would have to process the mouse messages and draw the button ourselves...

First of all, since we're drawing our own button, we'd need two bitmaps... One for the button-up state amd the other for the button-down state. Give the bitmaps a dimension of 16x16(I assume you're app is a regular window, not a tool window). I'll use IDB_BUTTONUP and IDB_BUTTONDOWN for this example.

First, declare a few variables in your header file, as public :

//Start of code

class CYourDlg : public CDialog
// Construction
 CYourDlg(CWnd* pParent = NULL); // standard constructor

 //Add these
 void DrawCapButton();
 void GetButtonRect( CRect &rect );    

 bool m_bPressed;
 CBitmap m_bitmapPressed;
 CBitmap m_bitmapUnpressed;


//End of code

In the constructor of your dialog class, load the two bitmaps :

//Start of code

CYourDlg::CYourDlg(CWnd *pParent ) : CDialog( CYourDlg::IDD, pParent )
 //Add these
 m_bPressed = FALSE;
 m_bitmapPressed.LoadBitmap( IDB_BUTTONDOWN );
 m_bitmapUnpressed.LoadBitmap( IDB_BUTTONUP );


//End of code

Then, using the ClassWizard, add the WM_NCPAINT and the WM_ACTIVATE message handlers to your class. Modify them and
add the DrawCapButton and GetButtonRect functions as well :

//Start of code
void CYourDlg::OnNcPaint()
 //Add this

void CYourDlg::OnActivate( UINT nState, CWnd* pWndOther, BOOL bMinimized )
 //Add this

void CYourDlg::DrawCapButton()

 //Skip if window is not visible or minimized
 if( !IsWindowVisible() || IsIconic() ) return;

 //Get the bitmap
 CDC memDC;
 CDC* pDC = GetWindowDC();
 memDC.CreateCompatibleDC( pDC );
 memDC.SelectObject( m_bPressed ? &m_bitmapPressed : &m_bitmapUnpressed );

 //Convert button rect into non-client area coordinates
 CRect rect, rectWnd;
 rect.OffsetRect( -rectWnd.left, -rectWnd.top );

 pDC->StretchBlt( rect.left, rect.top, rect.Width(),
              rect.Height(). &memDC, 0, 0, 16, 16, SRCCOPY );

 //Clean up
 ReleaseDC( pDC );

void CWinsysDlg::GetButtonRect(CRect& rect)

rect.top += GetSystemMetrics(SM_CYFRAME)+1;
rect.bottom = rect.top + GetSystemMetrics(SM_CYSIZE)-4;
            rect.left = rect.right - GetSystemMetrics(SM_CXFRAME) - ( 4 * GetSystemMetrics(SM_CXSIZE))-1;
            //Replace 4 with the current number of buttons in your caption + 1

rect.right = rect.left + GetSystemMetrics(SM_CXSIZE)-3;

Okay, now you'd want to activate your button :

//Start of Code
//In .h file

// Generated message map functions
afx_msg void OnNcLButtonDown( UINT nHitTest, CPoint point ); //Add this

//In .cpp file
//Add to message map


void CWinsysDlg::OnNcLButtonDown(UINT nHitTest, CPoint point)
if( nHitTest == HTCAPTION )

//see if in button area
CRect rect;
GetButtonRect( rect );
if( rect.PtInRect( point ) )
m_bPressed = !m_bPressed;
CDialog::OnNcLButtonDown( nHitTest, point);

//End of Code
Phew! Hope this answers your question! :)


Featured Post

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month10 days, 23 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question