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

VC++
C++类对象的拷贝构造函数
使用MFC的数组类
指针专题
精通VC与MATLAB联合编程:综合举例二
C 程序的外部变量与函数
MFC 编写的仿 Windows 计算器
static_cast揭密
事件编程(二)
事件编程(一)
精通 VC 与 MATLAB 联合编程:综合举例一
.c文件和.h文件的概念与联系
过马路,左右看
google 竞赛题 SecretSum 的 C++ 解法
如何实现快捷方式中的查找目标功能
几个 Windows 到 Linux 的代码移植问题
打造自定义的 AfxMessageBox
精通VC与MATLAB联合编程:编译器的使用
禁用屏幕拷贝(Print Screen),调用派生的析构函数及其它......
基于表达式计算的科学计算器
轻松实现DES算法查看器

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-30   浏览: 68 ::
收藏到网摘: 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)