Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

A Generic Tool Tip Class


April 2001/A Generic Tool Tip Class/Listing 2

Listing 2: tipwnd.cpp — Implementation to CTipWnd

/*------------------------------------------------------------------
TipWnd.cpp:    Implementation of the CTipWnd class
Author:        Zuoliu Ding, 11/2000
------------------------------------------------------------------*/
#include "afxwin.h"
#include "TipWnd.h"

BOOL CTipWnd::Create(CWnd* pOwner, UINT nTimerID)
{
    m_idTimer   = nTimerID;
        m_nSecDelay = nTimerID ? DEF_DURATION: 0;
    m_pOwner    = pOwner;

    VERIFY(m_font.CreatePointFont(DEF_FONT));    // def font    
    m_brush.CreateSolidBrush(DEF_COLOR);         // def brush

    return CWnd::CreateEx(0, "STATIC", "", WS_POPUP,    
                            CRect(0, 0, 1, 1), pOwner, 0);
}

BEGIN_MESSAGE_MAP(CTipWnd, CWnd)
    //{{AFX_MSG_MAP(CTipWnd)
    ON_WM_PAINT()
    ON_WM_TIMER()
    ON_WM_CLOSE()
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

// Message handlers
void CTipWnd::OnPaint() 
{
    CPaintDC dc(this); 

    CPen    penBlack;
    penBlack.CreatePen(PS_SOLID, 1, RGB(0, 0, 0));  
    CPen* pOldPen   = dc.SelectObject(&penBlack);
    CBrush* pOldBr  = dc.SelectObject(&m_brush);
    CFont* pOldFont = dc.SelectObject(&m_font);

    m_size = dc.GetTextExtent(m_strTip+"  "); 
    CRect rect  = CRect(0, 0, 0, 0);
    rect.right  = m_size.cx;
    rect.bottom = m_size.cy;

    if (m_dwStyle & TWS_ROURDRECT) dc.RoundRect(rect, CPoint(6, 6));
    else dc.Rectangle(rect);

    COLORREF oldClr = dc.SetTextColor(RGB(0, 0, 0));
    int nOldBkMode  = dc.SetBkMode(TRANSPARENT);
    dc.DrawText(m_strTip, -1, rect, 
                DT_SINGLELINE|DT_VCENTER|DT_CENTER);

    dc.SetTextColor(oldClr);
    dc.SetBkMode(nOldBkMode);
    dc.SelectObject(pOldFont);
    dc.SelectObject(pOldBr);
    dc.SelectObject(pOldPen);
}

void CTipWnd::OnTimer(UINT nIDEvent) 
{
    if (!m_hWnd || !m_idTimer) return; // No Wnd or timer

    if (--m_nSecCount ==0)
    {
        KillTimer(m_idTimer);
        ShowWindow(SW_HIDE);
    }
    CWnd::OnTimer(nIDEvent);
}

void CTipWnd::OnClose() 
{
    if (m_idTimer && m_nSecCount) KillTimer(m_idTimer); 

    m_font.DeleteObject();
    m_brush.DeleteObject();
    CWnd::OnClose();
}

// Operations
void CTipWnd::SetTipText(CString strTip, CPoint point)
{
    ASSERT(m_hWnd);
    if (m_idTimer && m_point==point) return;    
    m_point = point; 

    if (m_idTimer)                    // Need timer
    {
        if (!m_nSecCount)             // Tip window is hiden
            SetTimer(m_idTimer, 1000, NULL);
        m_nSecCount = m_nSecDelay;    // get another duration
    }

    m_strTip = strTip;
    ShowWindow(SW_SHOWNOACTIVATE); 
    Invalidate();
    UpdateWindow();

    int icyOffset = (m_dwStyle & TWS_SHOWABOVE)? 
                    -m_size.cy: GetSystemMetrics(SM_CYCURSOR)-12;
    m_pOwner->ClientToScreen(&point);
    SetWindowPos(&wndTop, point.x, point.y+icyOffset, 
                    m_size.cx, m_size.cy, SWP_NOACTIVATE);
}

BOOL CTipWnd::SetTipText(CString strTip, CWnd* pWnd)
{
    POINT pnt;        
    if (!GetCursorPos(&pnt)) return FALSE;
    if (pWnd && !PtInChildWnd(pWnd, pnt, FALSE)) return FALSE;

    m_pOwner->ScreenToClient(&pnt);
    SetTipText(strTip, pnt);
    return TRUE;
}

BOOL CTipWnd::SetTipText(CWnd* pWnd)
{
    if (!pWnd) return FALSE;
    CString str;
    pWnd->GetWindowText(str);
    if (str=="") return FALSE;

    return SetTipText(str, pWnd);
}

void CTipWnd::CancelTipWnd()
{
    if (!m_hWnd) return;

    if (m_idTimer) KillTimer(m_idTimer);
    ShowWindow(SW_HIDE);
    m_nSecCount =0;
}

BOOL CTipWnd::PtInOwnerWnd(CPoint pt, int iDeflate)
{
    CRect rc;
    m_pOwner->GetClientRect(rc);
    if (iDeflate) rc.DeflateRect(iDeflate,iDeflate);

    return rc.PtInRect(pt);
}

BOOL CTipWnd::PtInChildWnd(CWnd* pWnd, CPoint pt, BOOL bClient)
{
    CRect rc;
    pWnd->GetWindowRect(rc);
    if (bClient) m_pOwner->ClientToScreen(&pt);

    return rc.PtInRect(pt);
}

// Attributes
void CTipWnd::SetTipFont(int nPtSize, LPCTSTR szFace)
{
    m_font.DeleteObject();
    if (!m_font.CreatePointFont(nPtSize, szFace))
        m_font.CreatePointFont(DEF_FONT);       
}

void CTipWnd::SetTipBkClr(COLORREF clr)
{
    m_brush.DeleteObject();
    if (!m_brush.CreateSolidBrush(clr))    
        m_brush.CreateSolidBrush(DEF_COLOR); 
}

//End of File

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.