CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    MFC Dialog: Add tooltips to your controls in a dialog

    Q: Why should I add tooltips to my program?

    A: By adding tooltips to your controls you enhance the usability of your program.


    Q: How do I add tooltips to the controls in my dialog?

    A: First of all you need to add member variables of type Control for your controls. In this FAQ I will assume that you have added a member variable for a CButton control called m_myButton and a CEDIT control called m_myEdit.

    Then you manually add a member variable of type CToolTipCtrl to your dialog class;

    Code:
    //In your CDialog derived class' header file
    protected:
         CToolTipCtrl m_ToolTip;
    Now you override the PreTranslateMessage() function in your dialog's class. This relay messages to the ToolTip control:

    Code:
    BOOL CMyDialog::PreTranslateMessage(MSG* pMsg)
    {
         m_ToolTip.RelayEvent(pMsg);
    
         return CDialog::PreTranslateMessage(pMsg);
    }
    The call to RelayEvent() is necessary to pass a mouse message to the tool tip control for processing.

    Last but not least we have to make sure our ToolTip control is created, then add the dialog box's controls to the ToolTip and finally activate the ToolTip control. This code should be in the WM_INITDIALOG message handler function, OnInitDialog;

    Code:
    BOOL CMyDialog::OnInitDialog()
    {
    	CDialog::OnInitDialog();
    
    	//Create the ToolTip control
    	if( !m_ToolTip.Create(this))
    	{
    	   TRACE0("Unable to create the ToolTip!");
    	}
    	else
    	{
    	  // Add tool tips to the controls, either by hard coded string 
    	  // or using the string table resource
    	  m_ToolTip.AddTool( &m_myButton, _T("This is a tool tip!"));
    	  m_ToolTip.AddTool( &m_myEdit, IDS_MY_EDIT);
    
    	  m_ToolTip.Activate(TRUE);
    	}
         
    	return TRUE;
    }
    Please note that the string for the tool tip for the CEdit control is defined the string table resource with the ID IDS_MY_EDIT.
    Last edited by cilu; February 12th, 2007 at 04:14 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured