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