Click here to Skip to main content
Click here to Skip to main content

XTipComboBox - Display tooltips for combobox

By , 1 Jul 2003
 
<!-- Add the rest of your HTML here -->

Introduction

XTipComboBox displays tooltips for a combobox, just like tooltips are displayed for a tree control whose items are too long to fit in tree control's client area. When a listbox item in combobox is too long to fit in the listbox, a tooltip will be displayed that allows viewing of complete text. Similarly, a tooltip is displayed when text in combo's edit box is too wide for edit box.

Here is how the tooltips look - note that the tooltip colors will match what is being tooltipped:

        screenshot


        screenshot


        screenshot


Implementation Notes

CXTipComboBox is derived from CComboBox, and implements one virtual function and four message handlers:
  • PreSubclassWindow() - this virtual function allows us to create tooltip window, add combobox as its tool, and perform other initialization. Note use of TTF_TRANSPARENT. This flag tells tooltip control to forward mouse messages (including mouse clicks) to the parent window. In the case of the listbox, this will prevent two CBN_SELENDOK messages from being sent to the parent dialog.

  • OnCtlColor() - This is not what you think. According to MSDN article HOWTO: Subclass CListBox and CEdit Inside of CComboBox (Q174667), this is actually recommended way of subclassing listbox of a combobox. We use this to subclass listbox only - for edit box, it is simpler to handle inside CXTipComboBox.

  • OnMouseMove() - This message handler catches mouse moves, and when mouse is inside combo client rect, tooltip will be activated.

  • OnTimer() - A timer is used only when a tooltip is being displayed. When code in OnTimer() detects that mouse is no longer inside client rect, tooltip is removed.

  • OnDestroy() - Unsubclasses the listbox.

Note that nearly identical code is implemented for first four functions in XTipComboBox.cpp and XTipListBox.cpp.

Tooltip Notes

As mentioned above, tooltip is created in PreSubclassWindow():
    // create tooltip
    m_hWndToolTip = ::CreateWindowEx(WS_EX_TOPMOST,
                              TOOLTIPS_CLASS,
                              NULL,
                              TTS_NOPREFIX | TTS_ALWAYSTIP,
                              CW_USEDEFAULT,
                              CW_USEDEFAULT,
                              CW_USEDEFAULT,
                              CW_USEDEFAULT,
                              m_hWnd,
                              NULL,
                              NULL,
                              NULL);
    ASSERT(m_hWndToolTip);

    // initialize toolinfo struct
    memset(&m_ToolInfo, 0, sizeof(m_ToolInfo));
    m_ToolInfo.cbSize = sizeof(m_ToolInfo);
    m_ToolInfo.uFlags = TTF_TRACK | TTF_TRANSPARENT;
    m_ToolInfo.hwnd = m_hWnd;

    // add combo box
    ::SendMessage(m_hWndToolTip, TTM_SETMAXTIPWIDTH, 0, SHRT_MAX);
    ::SendMessage(m_hWndToolTip, TTM_ADDTOOL, 0, 
                                          (LPARAM) (LPTOOLINFO) &m_ToolInfo);
    ::SendMessage(m_hWndToolTip, TTM_SETTIPBKCOLOR, 
                                          ::GetSysColor(COLOR_HIGHLIGHT), 0);
    ::SendMessage(m_hWndToolTip, TTM_SETTIPTEXTCOLOR, 
                                      ::GetSysColor(COLOR_HIGHLIGHTTEXT), 0);

    // reduce top & bottom margins
    CRect rectMargins(0,-1,0,-1);
    ::SendMessage(m_hWndToolTip, TTM_SETMARGIN, 0, (LPARAM)&rectMargins);

    // set font
    CFont *pFont = GetFont();
    ::SendMessage(m_hWndToolTip, WM_SETFONT, (WPARAM)(HFONT)*pFont, FALSE); 
In typical dialog box situations, you would need to use RelayEvent() along with LPSTR_TEXTCALLBACK to pass tooltip text to tooltip control. Since we are dealing with two separate controls (edit box and list box) wrapped in one combobox control, it is easier to intercept mouse moves and position the tooltip ourselves. Inside OnMouseMove(), we determine position of the mouse, get text underneath, and calculate whether text will fit inside client rect. If it won't fit, we display tooltip, using TTM_TRACKACTIVATE message. Here we also set text and background colors for tooltip, depending on what is being tooltipped. Finally, a timer is used to keep track of when mouse moves outside client rect, so that tooltip will be removed.

How To Use

To integrate CXTipComboBox into your app, you first need to add following files to your project:

  • XTipComboBox.cpp
  • XTipComboBox.h
  • XTipListBox.cpp
  • XTipListBox.h

Next, include header file XTipComboBox.h in appropriate project files (usually, dialog header files). Now you are ready to start using CXTipComboBox. If you already have a dialog box with combobox controls, just replace CComboBox with CXTipComboBox in the dialog header file. There is no extra initialization you need to do.

Demo App

The XTipComboBoxTest.exe demo shows how to use CXTipComboBox.

Revision History

Version 1.0 - 2003 June 30

  • Initial public release.

Usage

This software is released into the public domain. You are free to use it in any way you like. If you modify it or extend it, please to consider posting new code here for everyone to share. This software is provided "as is" with no expressed or implied warranty. I accept no liability for any damage or loss of business that this software may cause.



License

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

About the Author

Hans Dietrich
Software Developer (Senior) Hans Dietrich Software
United States United States
I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.
 
Recently, I have moved to Los Angeles where I am doing consulting and development work.
 
For consulting and custom software development, please see www.hdsoft.org.






Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5membermanoj kumar choubey4-Apr-12 23:29 
GeneralMy vote of 5memberMember 851724911-Jan-12 0:53 
Generaltooltip flickermemberdeshmukhshivkumar9-May-11 23:43 
GeneralTip window is hiddenmemberAndrew Phillips12-Jan-11 21:05 
GeneralCXTipComboBox does not seem to work for me -- with VS2005memberderejem13-Apr-10 1:23 
GeneralTop most window problemmemberElsie9-Nov-09 21:54 
GeneralTooltip flickers on vista machinemembersandeep sumbe20-Oct-09 23:38 
Hi,
 
while displaying tool tip on Vista Operating system it flickers.
Please suggest good solution to avoid flickering.
 
Thanks,
Sandeep Sumbe
GeneralRe: Tooltip flickers on vista machinemembersandeep sumbe1-Nov-09 22:46 
GeneralCXTipComboBox derived from CComboBoxExmemberMd Saleem Navalur27-May-09 5:13 
QuestionWhy it is not working in ATL Composte control ?memberJagdish Vasani18-Oct-07 0:46 
QuestionUsing this one without a manifestmemberracmax13-Sep-07 0:56 
AnswerRe: Using this one without a manifestmemberracmax17-Sep-07 1:07 
GeneralRe: Using this one without a manifestmemberJagdish Vasani18-Oct-07 3:50 
QuestionCan this feature work in ASP.memberrachellim9-Aug-06 16:19 
General.Net 2005 usagemembermikeh10-May-06 11:50 
GeneralRe: .Net 2005 usagememberHumanTargetJoe8-Aug-06 9:23 
GeneralVisual BasicsussAnonymous24-Aug-05 3:55 
GeneralIt does not work with &quot;owner draw&quot; boxesmemberMichael Sternberg25-Apr-05 2:36 
GeneralRe: It does not work with &quot;owner draw&quot; boxesmemberGilles066008-May-06 3:26 
GeneralRe: It does not work with &quot;owner draw&quot; boxesmembermicrodus13-Feb-07 12:03 
GeneralCompile errormemberreiser4-Oct-04 21:51 
GeneralSlightly off-topic question: combo drop-down arrowmemberDavid Pritchard22-Sep-04 6:56 
GeneralBug and FixmemberAndrew115-Aug-04 3:06 
GeneralMouse wheelmemberhalloandy15-Jan-04 3:00 
GeneralXTipComboBox in Toolbarmembersimonpp18-Sep-03 15:04 
GeneralRe: XTipComboBox in Toolbarmembersrisahana27-May-04 21:53 
GeneralRe: XTipComboBox in Toolbarmembersrisahana27-May-04 21:53 
GeneralDoesnt work with VC++ 7.0memberUmair Ahmad11-Sep-03 23:12 
GeneralRe: Doesnt work with VC++ 7.0memberJohn Noël1-Nov-04 22:04 
GeneralRe: Doesnt work with VC++ 7.0memberUmair Ahmad2-Nov-04 3:58 
QuestionHow to create this XTipComboBox dynamically?memberedison3-Sep-03 4:34 
AnswerRe: How to create this XTipComboBox dynamically?membergangadhar npk17-Nov-03 1:17 
GeneralRe: How to create this XTipComboBox dynamically?membernismael16-Jun-05 11:45 
GeneralRe: How to create this XTipComboBox dynamically?memberQinShengChun2-Jul-07 23:41 
GeneralWhat's about CComboBoxExmemberOCV6-Aug-03 2:21 
GeneralRe: What's about CComboBoxExmemberCool19768-Jun-04 20:41 
GeneralRe: What's about CComboBoxExmemberTim D11-Mar-08 14:09 
GeneralEliminate flickering in CXTipListBox and solve the bug regarding the tooltips being displayed 'behind' the ScrollBarsussLiviu Birjega17-Jul-03 9:03 
GeneralRe: Eliminate flickering in CXTipListBox and solve the bug regarding the tooltips being displayed 'behind' the ScrollBarmemberPremek20-Jul-03 23:19 
GeneralRe: Eliminate flickering in CXTipListBox and solve the bug regarding the tooltips being displayed 'behind' the ScrollBarmemberJacques Cooper14-Dec-03 0:14 
QuestionPossible ScrollBar bug?memberPremek16-Jul-03 2:59 
AnswerRe: Possible ScrollBar bug?sussLiviu Birjega17-Jul-03 8:07 
GeneralRe: Possible ScrollBar bug?memberJohn Noël3-Nov-04 21:03 
GeneralRe: Possible ScrollBar bug?memberJohn Ritzenthaler18-Jan-05 5:52 
GeneralRe: Possible ScrollBar bug?sussAnonymous7-Apr-05 2:30 
GeneralRe: Possible ScrollBar bug?membernismael17-Jun-05 2:14 
QuestionVC++ 6.0, or VC++ 7.0 ?memberWREY8-Jul-03 10:09 
AnswerRe: VC++ 6.0, or VC++ 7.0 ?memberHans Dietrich9-Jul-03 0:26 
GeneralRe: VC++ 6.0, or VC++ 7.0 ?memberWREY9-Jul-03 7:25 
GeneralRe: VC++ 6.0, or VC++ 7.0 ?memberHans Dietrich10-Jul-03 13:10 

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130619.1 | Last Updated 2 Jul 2003
Article Copyright 2003 by Hans Dietrich
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid