Click here to Skip to main content
12,746,681 members (31,114 online)
Click here to Skip to main content
Add your own
alternative version


129 bookmarked
Posted 7 Oct 2008

Control Message Bar

, 23 Oct 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Code to add a message bar to virtually any existing Windows control.


I recently needed to add a message to a list when some items which would normally be shown in the list were excluded due to access permissions. I wanted a message bar which I could easily add to the control, similar in appearance to the message bar seen in most web browsers when pop-ups and other content are blocked.

How I did it

I wanted to show the message at the top of a listview control, but I didn't want to have to resize the control to fit something above the control as a sibling, so I decided to add it to the non-client area above the main client area, by intercepting the WM_NCCALCSIZE message in the control and leaving enough space to draw a message. I also wanted to provide the user with a way of dismissing the message, so I needed to respond to WM_NCLBUTTONDOWN etc. messages within the message bar area.

Not wanting to restrict myself to only being able to use the message bar in a listview control, I looked at how I could make it generic enough to be easily added to any control. My first attempt was to create a class from which I would co-inherit some MFC classes, thereby adding support for a message bar to any of my control classes. The main downside of this was that I would need a class derived from the standard MFC class for every control that I wanted to give the message bar to. I, therefore, wanted a method which allowed me to simply add the functionality to any existing control.

Adding functionality to a control by sub-classing is simple, but when the control is already sub-classed, it is not possible to subclass the control a second time. Looking into this brought me to an article by Ralph Varjabedian on Double Subclassing, which allows a previously subclassed control to be subclassed again, providing me with exactly what I needed.

Once the control has been (double-)subclassed in order to provide the message bar functionality, the message bar code is then able to intercept the window's messages, which allows it to modify the control's non-client area to provide a space for the message bar. Also, the message bar can intercept mouse messages so that the program can respond to clicks on the message bar to show a menu or perform any other action.

Some controls make their own use of the non-client area, which can interfere with the message bar's calculated area. The most common example is the list-view control, which uses the non-client area to place the column header. We are fortunate though that the list control sends a message when calculating the header's size and position, and so the message bar intercepts this message to adjust the area used by the header control, thus allowing it to keep its own space.

How to use it

Using the CCtrlMessageBar class is simple. Follow the steps below to add one to an existing project:

  1. After putting the source files (CtrlMessageBar.cpp and CtrlMessageBar.h) into the directory you wish to use them from, add the files to your Visual Studio project.
  2. Make sure you have a control to which you wish to add the message bar. I'll assume this is a list control, with a variable name of m_list.
  3. Add a CCtrlMessageBar variable to your dialog class. I'll assume this is called m_barList.
  4. Add a handler for WM_INITDIALOG in your dialog class if you don't already have one, and add the following code to it to add a message bar and set its text:
  5. m_barList.Attach(m_list);
    m_barlist.SetText(_T("This is my message bar text"))
  6. You can set an image for the bar by attaching an image list and setting the image index to use:
  7. CImageList imlMessageBar;

That's all you need to do to get started. See the features or member function documentation below, or the demo application, for more advanced options.


Some of the features included in the control, which you may find useful, are as follows:

Bars may have images

As in the example above, you may optionally set an image-list for the message bar, and then specify an image in that image-list to be displayed at the top left of the bar.

Text may be set to wrap

Text that is too long to fit in the bar's rectangle may be set to wrap, by calling the SetWrapText method, thus increasing the height of the bar to accommodate the text. Without wrapping enabled, the text is truncated with an ellipsis.

Bar text is displayed in a tool-tip if truncated

If text is too long to fit in the message bar, and you have not set wrapping, then the text is right-truncated with an ellipsis (...), in which case hovering the mouse over the bar will display the entire text in a tool-tip. This can be used where text is likely to be too long to fit, but you do not want the height of the bar to be increased.

The bar background and text may be set to any colour

By default, the bar uses the system tool-tip background colour for its background, and the system tool-tip text colour for the text. These, along with the highlight colours (see below), may be overridden by specifying new colours to the SetColours method. Specifying any colour as CLR_DEFAULT causes it to be used as the default colour.

Bars may be set to 'light up' on mouse-over

You can make the bar 'light up' when the user moves their mouse over it by calling the SetHighlightOnMouseOver method. The highlight background and text colours default to the system highlight colours, but may be set by calling the SetColours method (see above).

The bar can be set to resize its parent to make room

Normally, attaching a bar to a control will resize the control's client area to make room for the bar, while leaving the control's window size as it was. While this is probably ideal for most controls, there are times when the opposite is required, for instance, adding a bar to the top of a dialog. By calling the SetResize method with TRUE, the bar will resize the window to enable the bar to fit while leaving the size of the window's client area as it was.

Custom menus may be invoked

By default, clicking or right-clicking on the message bar will display a menu offering the user one option, which is to hide the bar. By specifying a callback function, you may intercept this behaviour and supply your own menu, or carry out any other action, or allow the default behaviour. An example of such a callback function is included in the demo application, in the CMBEdit class which subclasses the edit control (note though that it is not necessary to sub-class a control in order to provide a callback function).



The public member functions of the CCtrlMessageBar class are as follows:

  • CCtrlMessageBar();
  • Standard empty constructor.

  • virtual ~CCtrlMessageBar();
  • Standard virtual destructor.

  • BOOL Attach(CWnd* pCtrl);
  • Used to attach the message bar to the control specified by pCtrl. Returns TRUE is successful, else FALSE.

  • BOOL Attach(CWnd& ctrl)
  • Used to attach the message bar to the control specified by ctrl. Returns TRUE is successful, else FALSE.

  • BOOL Attach(CWnd* pDlg, UINT nID);
  • Used to attach the message bar to the control specified by nID on the dialog specified by pDlg. Returns TRUE is successful, else FALSE.

  • CWnd* Detach();
  • Detaches the message bar from the control. Returns the control to which the message had been attached.

  • CWnd* GetCtrl() const
  • Returns the controls to which the message bar is attached.

  • BOOL IsAttached() const
  • Returns whether the message bar is currently attached to a control.

  • void SetImageList(CImageList* piml);
  • Sets the image list containing the images to be used by the message bar.

  • virtual void SetText(LPCTSTR lpszText, UINT nImage = -1, BOOL bShow = TRUE);
  • Sets the current text, and optionally the image index, for the message bar. If nImage is -1, then no image is displayed. The bShow parameter determines whether the message bar is shown immediately.

  • virtual CString GetText() const;
  • Returns the current message bar text.

  • virtual void Show(BOOL bShow = TRUE);
  • Shows or hides the message bar.

  • virtual BOOL IsShown() const;
  • Returns whether the message bar is currently shown.

  • void SetImage(UINT nImage, BOOL bShow = TRUE);
  • Specifies the index of the image in the image list to display on the message bar.

  • UINT GetImage() const;
  • Returns the index of the image currently shown on the message bar.

  • CRect GetRect(BOOL bDrawing = FALSE) const;
  • Returns the size and position of the message bar in window co-ordinates.

  • CRect GetCloseButtonRect(CRect& rc) const;
  • Returns the size and position of the message bar's close button.

  • UINT GetMessageHeight() const;
  • Returns the height, in pixels, of the message bar.

  • void SetCloseButton(BOOL bShow = TRUE);
  • Sets whether to show a close button on the message bar.

  • BOOL GetCloseButton() const
  • Returns whether a close button is shown on the message bar.

  • void SetWrapText(BOOL bWrap = TRUE)
  • Sets whether the message bar text should be wrapped, if too long, to fit in the width of the bar. If text is not wrapped, and it is too long, then text is truncated with an ellipsis (...).

  • BOOL GetWrapText() const
  • Returns whether text will wrap if too long to fit in the width of the bar.

  • void SetHighlightOnMouseOver(BOOL bHighlight = TRUE)
  • Sets whether the bar should 'light up' when the user's mouse moves over it.

  • BOOL GetHighlightOnMouseOver() const
  • Returns whether the control is set to 'light up' when the user's mouse moves over it.

  • void SetColours(COLORREF crBackgroundNormal = CLR_DEFAULT, COLORREF crTextNormal = CLR_DEFAULT, COLORREF crBackgroundHilite = CLR_DEFAULT, COLORREF crTextHilite = CLR_DEFAULT);
  • Sets colours to use for the various aspects of the message bar. Specifying CLR_DEFAULT for any of the colours causes the bar to use its own defaults.

  • void GetColours(COLORREF& crBackgroundNormal, COLORREF& crTextNormal, COLORREF& crBackgroundHilite, COLORREF& crTextHilite) const;
  • Used to get the current colours used for the various aspects of the message bar.

  • void SetResize(BOOL bResize = TRUE)
  • Specifies whether the window hosting the message bar should be resized to accommodate the message bar. The normal behaviour is to reduce the window's client area to make room for the message bar while leaving the window's size and position untouched. This option is useful when adding a message bar to a dialog or other window, where the client area should not be resized, but the overall window size should be increased.

  • BOOL GetResize() const
  • Returns whether the message bar's host window will be resized to accommodate the message bar.

  • static CCtrlMessageBar* GetMessageBarCtrl(CWnd* pCtrl);
  • Returns a pointer to the CCtrlMessageBar currently attached to the specified Windows control.

  • void SetShowMenuCallback(PFNSHOWMENUCALLBACK pfnShowMenuCallback)
  • Sets a callback function to display a context menu. See the PFNSHOWMENUCALLBACK type documentation below, for details.


  • typedef BOOL (*PFNSHOWMENUCALLBACK)(CCtrlMessageBar* pBar, CWnd* pCtrl, UINT nHitTest, CPoint point);
  • The type of the callback function used to display a context menu. The callback function is called when the user left- or right-clicks on the message bar.

    The pBar parameter is the message bar clicked on. The pCtrl parameter is the control hosting the message bar. nHitTest is the hit-test code, which specifies where the user clicked, and will be one of HTMESSAGEBAR to indicate the body of the bar, or HTMESSAGEBARCLOSE to indicate the close button. The point parameter specifies the user's mouse position at the time of click, in window co-ordinates.

    The function may show - and act on - a context menu, or may choose to simply allow default handling. Return FALSE to perform default handling, else TRUE.

Acknowledgements and References


Version 1.0 - 23 Oct 2008

  • Added additional information to article text.

Version 1.0 - 08 Oct 2008

  • First version.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Paul Vickery
Software Developer (Senior)
United Kingdom United Kingdom
Originally from an electronics background, I moved into software in 1996, partly as a result of being made redundant, and partly because I was very much enjoying the small amount of coding (in-at-the-deep-end-C) that I had been doing!

I swiftly moved from C to C++, and learned MFC, and then went on to real-time C on Unix. After this I moved to the company for which I currently work, which specialises in Configuration Management software, and currently program mainly in C/C++, for Windows. I have been gradually moving their legacy C code over to use C++ (with STL, MFC, ATL, and WTL). I have pulled in other technologies (Java, C#, VB, COM, SOAP) where appropriate, especially when integrating with third-party products.

In addition to that, I have overseen the technical side of the company website (ASP, VBScript, JavaScript, HTML, CSS), and have also worked closely with colleagues working on other products (Web-based, C#, ASP.NET, SQL, etc).

For developing, I mainly use Visual Studio 2010, along with an in-house-designed editor based on Andrei Stcherbatchenko's syntax parsing classes, and various (mostly freeware) tools. For website design, I use Dreaweaver CS3.

When not developing software, I enjoy listening to and playing music, playing electric and acoustic guitars and mandolin.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Mihai MOGA22-Sep-11 4:04
memberMihai MOGA22-Sep-11 4:04 
GeneralRe: My vote of 5 Pin
skyformat99@gmail.com9-Dec-13 21:47
memberskyformat99@gmail.com9-Dec-13 21:47 
QuestionWhat about Views ? Pin
Alexandre GRANVAUD18-Jan-10 22:56
memberAlexandre GRANVAUD18-Jan-10 22:56 
AnswerRe: What about Views ? Pin
Alexandre GRANVAUD19-Jan-10 0:17
memberAlexandre GRANVAUD19-Jan-10 0:17 
GeneralReally useful Pin
Grump3-Jun-09 1:51
memberGrump3-Jun-09 1:51 
Very innovative and cleverly implemented!

I've beein using it in a lot of different ways, but ran into a problem attaching the control bar to a group box control, for which I've made the following fix:

UINT CCtrlMessageBar::DoNcHitTest(CPoint point, UINT nCode)
  UINT nStyle = m_pCtrlMessageBarThis->GetStyle();
  if (nCode == HTNOWHERE || nCode == HTCAPTION ||
      ((nStyle&BS_GROUPBOX) != 0 && nCode == HTTRANSPARENT))
    // lets see if we really want to consider it as being in the message-bar
    CRect rc = GetRect(); // get rect in client co-ords
    CPoint pt(point);
    CRect rcClose = GetCloseButtonRect(rc);
    if (rcClose.PtInRect(pt) && m_bMessageBarCloseButton)
    else if (rc.PtInRect(pt))
      nCode = HTMESSAGEBAR;
  return nCode;

Great work and thanks a lot!

GeneralNice Article Pin
Naveen27-Oct-08 19:04
memberNaveen27-Oct-08 19:04 
Questionhow can I use this in C# language? Pin
Member 404996323-Oct-08 18:48
memberMember 404996323-Oct-08 18:48 
Generalthere is nothing to say , excellent Pin
only_jack21-Oct-08 11:04
memberonly_jack21-Oct-08 11:04 
GeneralThat is cool! Pin
matrixnull19-Oct-08 19:49
membermatrixnull19-Oct-08 19:49 
GeneralReally neat! Pin
Alex Kolesnichenko19-Oct-08 7:59
memberAlex Kolesnichenko19-Oct-08 7:59 
GeneralIt's really a neat implementation Pin
Quynh Nguyen17-Oct-08 1:37
memberQuynh Nguyen17-Oct-08 1:37 
GeneralGreat article. Pin
Scott Dorman16-Oct-08 9:42
mvpScott Dorman16-Oct-08 9:42 
GeneralNice ! thank a lot (little bug found inside) Pin
Alexandre GRANVAUD8-Oct-08 6:36
memberAlexandre GRANVAUD8-Oct-08 6:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170215.1 | Last Updated 23 Oct 2008
Article Copyright 2008 by Paul Vickery
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid