Tuesday, November 18, 2014

How to Add Tooltip and Balloon Tip on a dialog control

Hello!
Today I will show you how to add a tool tip for dialog control and a balloon tip for an edit box.

  1. Create a MFC Dialog Preject.
  2. Add following control to the dialog.
   
3.   Open BlogTest2Dlg.h and add CToolTipCtrl *m_pToolTip; as private.
Private:
CToolTipCtrl *m_ToolTip;
 
4.      Open BlogTest2Dlg.cpp and add following on OnInitDialog(); function.
BOOL CBlogTest2Dlg::OnInitDialog()
{
                …….
 
     // TODO: Add extra initialization here
     m_pToolTip = new CToolTipCtrl();
     if (!m_pToolTip->Create(this))
     {
           TRACE(L"Unable to create ToolTipCtrl.\r\n");
           return TRUE;
     }
     if (!m_pToolTip->AddTool(GetDlgItem(IDOK), L"This is OK Button."))
     {
           TRACE(L"Unable to add OK Button.");
           return TRUE;
     }
     if (!m_pToolTip->AddTool(GetDlgItem(IDCANCEL), L"This is Cancel Button."))
     {
           TRACE(L"Unable to add Cancel Button.");
           return TRUE;
     }
     if (!m_pToolTip->AddTool(GetDlgItem(IDC_EDIT1), L"Try to insert key '5' and will summon balloon."))
     {
           TRACE(L"Unable to add Edit Control.");
           return TRUE;
     }
     m_pToolTip->Activate(TRUE);

}
5.       Add PreTranslateMessage Function, add following code.
BOOL CBlogTest2Dlg::PreTranslateMessage(MSG* pMsg)
{
                if (m_pToolTip != NULL)
                                m_pToolTip->RelayEvent(pMsg);
                return CDialogEx::PreTranslateMessage(pMsg);
}
Done with ToolTip. Now go for Balloon Tip.
1.       Add a new MFC Class “CBalloonEdit” and the base class is “CEdit” from Class Wizard.
2.       Add WM_CHAR Message handler.
3.       Add following Code OnChar Function.
void CBalloonEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
        if (nChar == 0x35)
        {
                        ShowBalloonTip(L"Unacceptable Character", L"The Number 5 will summon the Balloon.",TTI_INFO);
                        return;
        }
        CEdit::OnChar(nChar, nRepCnt, nFlags);
}
4.       Add CBalloonTip Variable on BlogTest2Dlg.h
CBalloonEdit m_baloonEdit;
Also Include BalloonEdit.h on the BlogTest2Dlg.h
5.       Add following code to
void CBlogTest2Dlg::DoDataExchange(CDataExchange* pDX)
{
        CDialogEx::DoDataExchange(pDX);
        DDX_Control(pDX, IDC_EDIT1, m_baloonEdit);
}
 
Now build and run the program. You should have following output.
 
 
Thanks.

No comments:

Post a Comment