介绍一个有Toolbar功能的可重用类 CPopupText,介绍一个有Toolbar功能的可重用类 CPopupText
【 tulaoshi.com - C语言心得技巧 】
介绍一个有Toolbar功能的可重用类 CPopupText 赵湘宁 本文例子代码背景:我用一个CListBox派生类实现宿主(owner-draw)列表框,这个列表框的项目宽度超过了列表框本身的宽度,因此当鼠标指针指向大宽度的列表框项时,我想显示一个类似Toolbar的提示窗口,在窗口中显示完整的列表框项目文本。CPopupText 类定义和实现 ////////////////////////////////////////////////////////////////// PupText.h // #pragma once// Get NONCLIENTMETRICS info: ctor calls SystemParametersInfo.//class CNonClientMetrics : public NONCLIENTMETRICS {public: CNonClientMetrics() { cbSize = sizeof(NONCLIENTMETRICS); SystemParametersInfo(SPI_GETNONCLIENTMETRICS,0,this,0); }};// Popup text window, like tooltip.// Can be right or left justified relative to creation point.//class CPopupText : public CWnd {public: CSize m_szMargins; // extra space around text: change if you like enum {JUSTIFYLEFT=0, JUSTIFYRIGHT}; CPopupText(); virtual ~CPopupText(); BOOL Create(CPoint pt, CWnd* pParentWnd, UINT nStyle=0, UINT nID=0); void ShowDelayed(UINT msec); void Cancel();protected: CFont m_font; // font to use (same as tooltips) UINT m_nStyle; // style (see enum below) virtual void PostNcDestroy(); virtual BOOL PreCreateWindow(CREATESTRUCT& cs); afx_msg void OnPaint(); afx_msg void OnTimer(UINT nIDEvent); afx_msg LRESULT OnSetText(WPARAM wp, LPARAM lp); DECLARE_DYNAMIC(CPopupText); DECLARE_MESSAGE_MAP();};PupText.cpp ////////////////////////////////////////////////////////////////// VCKBASE -- September 2000 // Visual C++ 6.0 环境编译, Windows 98 和 NT 环境运行.// #include "stdafx.h"#include "puptext.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endifIMPLEMENT_DYNAMIC(CPopupText,CWnd)BEGIN_MESSAGE_MAP(CPopupText,CWnd) ON_WM_PAINT() ON_MESSAGE(WM_SETTEXT, OnSetText) ON_WM_TIMER()END_MESSAGE_MAP()CPopupText::CPopupText(){ m_szMargins = CSize(4,4); // create font ?use system tooltip font CNonClientMetrics ncm; m_font.CreateFontIndirect(&ncm.lfStatusFont);}CPopupText::~CPopupText(){}// Create window. pt is upper-left or upper-right corner depending on // nStyle.//CPopupText::Create(CPoint pt, CWnd* pParentWnd, UINT nStyle, UINT nID){ m_nStyle = nStyle; return CreateEx(0, NULL, NULL, WS_POPUP|WS_VISIBLE, CRect(pt,CSize(0,0)), pParentWnd, nID);}// Someone changed the text: resize to fit new text//LRESULT CPopupText::OnSetText(WPARAM wp, LPARAM lp){ CClientDC dc = this; CFont* pOldFont = dc.SelectObject(&m_font); CRect rc; GetWindowRect(&rc); int x = (m_nStyle & JUSTIFYRIGHT) ? rc.right : rc.left; int y = rc.top; dc.DrawText(CString((LPCTSTR)lp), &rc, DT_CALCRECT); rc.InflateRect(m_szMargins); if (m_nStyle & JUSTIFYRIGHT) x -= rc.Width(); SetWindowPos(NULL,x,y,rc.Width(),rc.Height(), SWP_NOZORDER|SWP_NOACTIVATE); return Default();}// Paint the text. Use system colors//void CPopupText::OnPaint(){ CPaintDC dc(this); CRect rc; GetClientRect(&rc); CString s; GetWindowText(s); CBrush b(GetSysColor(COLOR_INFOBK)); // use tooltip bg color dc.FillRect(&rc, &b); // draw text dc.SetBkMode(TRANSPARENT); CFont* pOldFont = dc.SelectObject(&m_font); dc.SetTextColor(GetSysColor(COLOR_INFOTEXT)); // tooltip text color dc.DrawText(s, &rc, DT_SINGLELINE|DT_CENTER|DT_VCENTER); dc.SelectObject(pOldFont);}// Register class if needed//BOOL CPopupText::PreCreateWindow(CREATESTRUCT& cs)
来源:http://www.tulaoshi.com/n/20160129/1484178.html
看过《介绍一个有Toolbar功能的可重用类 CPopupText》的人还看了以下文章 更多>>