Click here to Skip to main content
15,886,788 members
Articles / Desktop Programming / MFC

Control Message Bar

Rate me:
Please Sign up or sign in to vote.
5.00/5 (47 votes)
23 Oct 2008CPOL10 min read 90.2K   3.3K   140  
Code to add a message bar to virtually any existing Windows control.
// CtrlMessageBarDemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CtrlMessageBarDemo.h"
#include "CtrlMessageBarDemoDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CCtrlMessageBarDemoDlg dialog

CCtrlMessageBarDemoDlg::CCtrlMessageBarDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCtrlMessageBarDemoDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCtrlMessageBarDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_TREE1, m_tree);
	DDX_Control(pDX, IDC_LISTBOX, m_listbox);
	DDX_Control(pDX, IDC_LIST1, m_list);
	DDX_Control(pDX, IDC_EDIT1, m_edit);
	DDX_Control(pDX, IDC_BUTTON1, m_btn);
}

BEGIN_MESSAGE_MAP(CCtrlMessageBarDemoDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_RESET, OnReset)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


// CCtrlMessageBarDemoDlg message handlers

BOOL CCtrlMessageBarDemoDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
	m_iml.Create(IDB_ICONS_SML, 16, 0, RGB(255, 0, 255));
	m_imlLarge.Create(IDB_ICONS, 32, 0, RGB(255, 0, 255));
	m_imlMessage.Create(IDB_MSG_ICONS, 16, 0, RGB(255, 0, 255));
	m_list.SetImageList(&m_imlLarge, LVSIL_NORMAL);
	m_list.SetImageList(&m_iml, LVSIL_SMALL);
	m_tree.SetImageList(&m_iml, TVSIL_NORMAL);
	
	m_list.InsertColumn(0, _T("Hello"), LVCFMT_LEFT, 200);
	HTREEITEM hItemRoot = m_tree.InsertItem(_T("Root item"), 0, 0);
	for (int n = 0; n < 20; n++)
	{
	  CString sItem;
	  sItem.Format(_T("Item %d"), n+1);
	  m_list.InsertItem(n, sItem, 0);
	  m_tree.InsertItem(sItem, 1, 1, hItemRoot);
	  m_listbox.AddString(sItem);
	}
	m_tree.Expand(hItemRoot, TVE_EXPAND);

	AddInitialMessageBars();
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCtrlMessageBarDemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CDialog dlg(IDD_ABOUTBOX);
		dlg.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CCtrlMessageBarDemoDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCtrlMessageBarDemoDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CCtrlMessageBarDemoDlg::AddInitialMessageBars()
{
  bar.Detach();
  VERIFY(bar.Attach(this));
  bar.SetResize();
  bar.SetImageList(&m_imlMessage);
  bar.SetText(_T("This is a dialog message"), 3);

  barEdit.Detach();
  VERIFY(barEdit.Attach(m_edit));
  barEdit.SetImageList(&m_imlMessage);
  barEdit.Show(FALSE);
  m_edit.SetWindowText(_T("Click in here to \r\nadd a message bar"));
  barEdit.SetShowMenuCallback(CCMBEdit::CMB_ShowMenuCallback);

  barList.Detach();
  VERIFY(barList.Attach(m_list));
  barList.SetImageList(&m_imlMessage);
  barList.SetText(_T("This is a message bar in a list, which lights up on mouse-over"), 3);
  barList.SetHighlightOnMouseOver(TRUE);

  barTree.Detach();
  VERIFY(barTree.Attach(m_tree));
  barTree.SetImageList(&m_imlMessage);
  barTree.SetText(_T("This is a wrapped message bar in a tree control, which is a non-default colour"), 1);
  barTree.SetWrapText(TRUE);
  barTree.SetColours(RGB(255, 192, 192), RGB(0, 0, 255));

  barListBox.Detach();
  VERIFY(barListBox.Attach(m_listbox));
  barListBox.SetImageList(&m_imlMessage);
  barListBox.SetText(_T("This is a message bar in a list, which is not wrapped, and is too long, so shows in a tool-tip"), 2);

  barBtn.Detach();
  VERIFY(barBtn.Attach(m_btn));
  barBtn.SetResize();
  barBtn.SetText(_T("Button message bar"));
}

void CCtrlMessageBarDemoDlg::OnReset() 
{
  AddInitialMessageBars();
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
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.

Comments and Discussions