Click here to Skip to main content
15,897,334 members
Articles / Desktop Programming / MFC

CBalloonMsg - An Easy-to-use Non-modal Balloon Alternative to AfxMessageBox

Rate me:
Please Sign up or sign in to vote.
4.71/5 (29 votes)
1 Apr 2008CPOL5 min read 123.2K   3.7K   109  
Makes it easy to use a balloon tooltip to convey hints/messages non-modally
// TTTestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TTTest.h"
#include "TTTestDlg.h"
#include "BalloonMsg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CTTTestDlg dialog



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

void CTTTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT1, m_ctrlEdit);
	DDX_Text(pDX, IDC_EDIT1, m_strJunk);
	
	if ( pDX->m_bSaveAndValidate )
	{
		if ( m_strJunk.IsEmpty() )
		{
			// Use SafeShowMsg here - if balloons are disabled, we'll see a conventional message box
			pDX->PrepareEditCtrl( IDC_EDIT1 );
			CBalloonMsg::SafeShowMsg( MB_OK | MB_ICONEXCLAMATION, IDS_ERR_TITLE, IDS_ERR_BODY, &m_ctrlEdit );
			pDX->Fail();
		}
		
	}
}

BEGIN_MESSAGE_MAP(CTTTestDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1)
	ON_EN_CHANGE(IDC_EDIT1, OnEnChangeEdit1)
END_MESSAGE_MAP()


// CTTTestDlg message handlers

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

	// 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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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 CTTTestDlg::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 CTTTestDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CTTTestDlg::OnBnClickedButton1()
{
	CBalloonMsg::ShowForCtrl( _T("Test Title"), _T("Test Text"), &m_ctrlEdit, (HICON) 1 );
}

void CTTTestDlg::OnEnChangeEdit1()
// If the user types something, ditch the balloon fast!
{
	CString	strTest;
	//
	
	m_ctrlEdit.GetWindowText( strTest );
	if ( !strTest.IsEmpty() )
		CBalloonMsg::RequestCloseAll();
}

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
Started programming on a Commodore Vic 20(!), and later entered employment programming for the Mac back in the days of System 6. Soon the pull of the Dark Side became too strong and I switched to Windows (Win 3.1) and have been coding for Windows ever since.

I'm now lead programmer for a small software house in Glasgow, Scotland. Our main products include PTFB Pro, ColorCache, and LogMeister.

Comments and Discussions