当前位置: 首页 > 图文教程 > 开发语言 > VC++ > 介绍一个有Toolbar功能的可重用类 CPopupText

VC++
宏的妙用
泛型编程与设计新思维
C++中的虚函数(一)
C++模板元编程
C++多态技术
通用结构复制函数
<C++实践系列>C++中的虚函数(virtual function)
<C++实践系列>C++中的引用(reference)
<C++实践系列>C++中的异常(exception)
<C++实践系列>C++中的模板(template)
构造函数中的this指针
串行化(Serialization)
二进制浏览、编辑的实现
介绍一个模板动态数组
VC++界面一揽子解决方案(第三版) 介绍
VC++通用GIS功能开发解决方案 2.0v 介绍
确定有穷自动机分析内核
委托、信号和消息反馈的模板实现技术
按照类型名称动态创建对象
Boost中应用的泛型编程技术

VC++ 中的 介绍一个有Toolbar功能的可重用类 CPopupText


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-30   浏览: 84 ::
收藏到网摘: n/a

介绍一个有Toolbar功能的可重用类 CPopupText
赵湘宁
本文例子代码
背景:我用一个CListBox派生类实现宿主(owner-draw)列表框,这个列表框的项目宽度超过了列表框本身的宽度,因此当鼠标指针指向大宽度的列表框项时,我想显示一个类似Toolbar的提示窗口,在窗口中显示完整的列表框项目文本。
起初我想使用CToolTipCtrl::AddTool的第三个参数lpRectTool来实现这个功能,但没有成功。后来,我采用了自立更生的解决方案,创建了一个可重用窗口类:
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)