Tooltip for the header


Adding a tooltip for the header control is quite straightforward. We use a tooltip control object and add a tool for the header control

Step 1: Add member of type CToolTipCtrl

Add a member variable of type CToolTipCtrl in your CListCtrl derived class.
CToolTipCtrl	m_tooltip;

Step 2: Initialize the tooltip object

Override PreSubclassWindow() in your CListCtrl derived class. After calling the base class PreSubclassWindow(), create the tooltip object. We override the PreSubclassWindow() instead of OnCreate() because the control is usually attached to the C++ object after it has already been created - usually from a dialog resource - and therefore OnCreate is never called for the object. It is important to note that the call to GetDlgItem(0) may fail if the control was created with a style other than LVS_REPORT.

If you are deriving from CListView then the code to create the tooltip and add a tool to it can be moved to OnCreate() or the OnInitialUpdate() function.

void CMyListCtrl::PreSubclassWindow() 
{
	CListCtrl::PreSubclassWindow();

	// Add initialization code
	m_tooltip.Create( this );
	m_tooltip.AddTool( GetDlgItem(0), "Right click for context menu" );
} 

Step 3: Call RelayEvents() function of the tooltip object

Override PreTranslateMessage() and call the RelayEvents() function of the CToolTipCtrl object.
BOOL CMyListCtrl::PreTranslateMessage(MSG* pMsg) 
{
	m_tooltip.RelayEvent( pMsg );	
	return CListCtrl::PreTranslateMessage(pMsg);
}



Comments

  • How to refresh tooltip position after using horizontal scroll bar?

    Posted by Legacy on 09/23/2001 12:00am

    Originally posted by: Gamli Zeevi


    I have used this code and it works nicely as long as I am not using the horizontal scroll bar. But after right or left scrolling the tooltip appears incorrectly in the previous position before the scrolling, i.e the tooltip positions are not refreshed after the horizontal scroll.

    Please your help

    Thank in advance

    Gamli

    Reply
  • Updated Code to Support Text for Each Individual Header

    Posted by Legacy on 05/16/2001 12:00am

    Originally posted by: Jimmy Riffe

    Here's a modification to allow individual text for each
    
    header column in the header control. A better solution
    would be to use string resources instead of hardcoded
    text for the call to UpdateTipText().

    BOOL CMyCustomView::PreTranslateMessage(MSG* pMsg)
    {

    UINT i;
    RECT rect;
    POINT newPoint;

    CString text[] =
    {
    "User", "Time Spent", "Action", "Charge",
    "Additional Allowances", "Security Code"
    };

    // Check for Each Rectangle

    for (i = 0; i < 6; ++i)
    {

    // fetch the location of the next header's
    // rectangle

    GetListCtrl().GetHeaderCtrl() ->
    GetItemRect(i, &rect);

    // fetch a copy of the mouse cursor location
    // (in screen coordinates)

    newPoint = pMsg -> pt;

    // convert the screen coordinates to client
    // coordinates

    ScreenToClient(&newPoint);

    // are we in a header rectangle ? If so,
    // we update the text and quit subsequent
    // checks

    if (::PtInRect(&rect, newPoint))
    {
    m_HeaderToolTip.UpdateTipText(text[i],
    GetListCtrl().GetHeaderCtrl());
    break;
    }

    }

    // relay the event to the tooltip control

    m_HeaderToolTip.RelayEvent(pMsg);

    // allow the base class handler to process

    return CListView::PreTranslateMessage(pMsg);

    }

    Reply
  • How to change the font of listctrl's headers

    Posted by Legacy on 01/10/1999 12:00am

    Originally posted by: ShaoJun

    Usually the font of listctrl's headers as same as menu's,I want to
    change the font of headers and items.But the font didn't change,
    recrangle changed.How can I do?

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: September 10, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild". This loop of continuous delivery and continuous feedback is …

  • Live Event Date: September 19, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT In response to the rising number of data breaches and the regulatory and legal impact that can occur as a result of these incidents, leading analysts at Forrester Research have developed five important design principles that will help security professionals reduce their attack surface and mitigate vulnerabilities. Check out this upcoming eSeminar and join Chris Sherman of Forrester Research to learn how to deal with the influx of new device …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds