HEADER FILE :
#if !defined(AFX_TOOLTIPEXTIMPL_H__6E125E2F_DFE5_11D5_A96D_0060973D022E__INCLUDED_)
#define AFX_TOOLTIPEXTIMPL_H__6E125E2F_DFE5_11D5_A96D_0060973D022E__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// ToolTipExtImpl.h : header file
//
#include <AfxWin.h>
#include <AfxCmn.h>
/
// ToolTipExtImpl window
class ToolTipExtImpl : public CToolTipCtrl
{
// Construction/Disconstruction
public:
ToolTipExtImpl();
~ToolTipExtImpl();
protected:
virtual bool InitToolTip(void);
virtual bool CheckWindowSize(void);
virtual bool DrawTip(void);
public:
virtual int GetMaxTipWidth(void) const;
virtual int SetMaxTipWidth( int iWidth );
virtual void SetTipBkColor( COLORREF clr );
virtual void SetTipTextColor( COLORREF clr );
virtual void RelayEvent( LPMSG lpMsg );
virtual BOOL Create( CWnd* pParentWnd, DWORD dwStyle = 0);
virtual BOOL AddTool( CWnd* pWnd, LPCTSTR lpszText = LPSTR_TEXTCALLBACK, LPCRECT lpRectTool = NULL, UINT nIDTool = 0 );
virtual BOOL AddTool( CWnd* pWnd, UINT nIDText, LPCRECT lpRectTool = NULL, UINT nIDTool = 0 );
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(ToolTipExtImpl)
//}}AFX_VIRTUAL
protected:
//{{AFX_MSG(ToolTipExtImpl)
afx_msg void OnPaint();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
private:
CWnd * parent_;
HDC hdc_;
COLORREF bkcolor_,txtcolor_;
long bkmode_;
bool create_;
bool initok_;
bool flag_;
long maxwidth_;
CString title_;
TEXTMETRIC font_;
};
/
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_TOOLTIPEXTIMPL_H__6E125E2F_DFE5_11D5_A96D_0060973D022E__INCLUDED_)
IMPLEMENT FILE :
// ToolTipExtImpl.cpp : implementation file
#include "ToolTipExtImpl.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/
// ToolTipExtImpl
ToolTipExtImpl::ToolTipExtImpl()
{
this->parent_=NULL;
this->hdc_=NULL;
this->create_=false;
this->initok_=false;
this->flag_=false;
this->bkcolor_=NULL;
this->txtcolor_=NULL;
this->bkmode_=NULL;
this->maxwidth_=NULL;
}
ToolTipExtImpl::~ToolTipExtImpl()
{
::ReleaseDC(this->m_hWnd,this->hdc_);
}
BEGIN_MESSAGE_MAP(ToolTipExtImpl, CToolTipCtrl)
//{{AFX_MSG_MAP(ToolTipExtImpl)
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// ToolTipExtImpl message handlers
BOOL ToolTipExtImpl::AddTool( CWnd* pWnd, LPCTSTR lpszText, LPCRECT lpRectTool, UINT nIDTool)
{
this->title_.Empty();
if(lpszText==NULL)
{
this->title_.Insert(NULL,"Made By Cgh !");
}
else
{
this->title_.Insert(NULL,lpszText);
}
this->SetMaxTipWidth(NULL);
//return CToolTipCtrl::AddTool(pWnd,lpszText,lpRectTool,nIDTool);
return CToolTipCtrl::AddTool(pWnd," ",lpRectTool,nIDTool);
}
BOOL ToolTipExtImpl::AddTool( CWnd* pWnd, UINT nIDText, LPCRECT lpRectTool, UINT nIDTool)
{
this->title_.Empty();
if(nIDText==NULL)
{
this->title_.Insert(NULL,"Made By Cgh !");
}
else
{
this->title_.LoadString(nIDText);
}
this->SetMaxTipWidth(NULL);
//return CToolTipCtrl::AddTool(pWnd,nIDText,lpRectTool,nIDTool);
return CToolTipCtrl::AddTool(pWnd," ",lpRectTool,nIDTool);
}
BOOL ToolTipExtImpl::Create( CWnd* pParentWnd, DWORD dwStyle)
{
this->parent_=pParentWnd;
this->create_=true;
return CToolTipCtrl::Create(pParentWnd,dwStyle);
}
int ToolTipExtImpl::SetMaxTipWidth( int iWidth )
{
this->maxwidth_=iWidth;
return CToolTipCtrl::SetMaxTipWidth(NULL);
}
int ToolTipExtImpl::GetMaxTipWidth(void) const
{
return this->maxwidth_;
}
void ToolTipExtImpl::RelayEvent( LPMSG lpMsg )
{
CToolTipCtrl::RelayEvent(lpMsg);
}
bool ToolTipExtImpl::CheckWindowSize(void)
{
CString drawtext_;
HWND force_;
RECT mrect_;
long start_;
long cx_,cy_,hchar_,wchar_,row_;
if(!this->initok_)
{
this->InitToolTip();
}
if(!this->initok_)
{
return false;
}
hchar_=this->font_.tmHeight;
wchar_=this->font_.tmAveCharWidth;
this->GetMargin(& mrect_);
start_=NULL;
row_=1;
cx_=NULL;
cy_=NULL;
for(long x=NULL;x<this->title_.GetLength();x++)
{
if(((this->title_.GetAt(x)=='/r') || (this->title_.GetAt(x)=='/n')) && ((this->title_.GetAt(x+1)!='/r') && (this->title_.GetAt(x+1)!='/n')))
{
row_++;
}
}
for(x=NULL;x<this->title_.GetLength();x++)
{
if(((this->title_.GetAt(x)=='/r') || (this->title_.GetAt(x)=='/n')) && ((this->title_.GetAt(x+1)!='/r') && (this->title_.GetAt(x+1)!='/n')) || (x==this->title_.GetLength()-1))
{
drawtext_.Empty();
if((this->title_.GetAt(x-1)=='/r') || (this->title_.GetAt(x-1)=='/n'))
{
drawtext_=this->title_.Mid(start_,x-start_-1);
}
else
{
drawtext_=this->title_.Mid(start_,x-start_+1);
}
start_=x+1;
cx_=(cx_>drawtext_.GetLength()*this->font_.tmAveCharWidth?cx_:drawtext_.GetLength()*this->font_.tmAveCharWidth);
}
}
cx_=cx_+mrect_.left+mrect_.right;
cy_=hchar_*row_+mrect_.top+mrect_.bottom+this->font_.tmDescent;
force_=::GetForegroundWindow();
::SetWindowPos(this->m_hWnd,HWND_TOPMOST,NULL,NULL,cx_,cy_,SWP_NOMOVE);
::SetForegroundWindow(force_);
return true;
}
bool ToolTipExtImpl::DrawTip(void)
{
long start_,row_,hchar_,wchar_;
RECT rect_,mrect_;
CString drawtext_;
if(!this->initok_)
{
return false;
}
hchar_=this->font_.tmHeight;
wchar_=this->font_.tmAveCharWidth;
this->GetMargin(& mrect_);
start_=NULL;
row_=NULL;
for(long x=NULL;x<this->title_.GetLength();x++)
{
if(((this->title_.GetAt(x)=='/r') || (this->title_.GetAt(x)=='/n')) && ((this->title_.GetAt(x+1)!='/r') && (this->title_.GetAt(x+1)!='/n')) || (x==this->title_.GetLength()-1))
{
drawtext_.Empty();
if((this->title_.GetAt(x-1)=='/r') || (this->title_.GetAt(x-1)=='/n'))
{
drawtext_=this->title_.Mid(start_,x-start_-1);
}
else
{
drawtext_=this->title_.Mid(start_,x-start_+1);
}
start_=x+1;
memset(& rect_,NULL,sizeof(RECT));
rect_.top=row_*hchar_;
rect_.left=rect_.left;
rect_.bottom=rect_.top+hchar_;
rect_.right=rect_.left+wchar_*drawtext_.GetLength();
row_++;
FillRect(this->hdc_,& rect_,WHITE_BRUSH);
DeleteObject(SelectObject(this->hdc_,GetStockObject(SYSTEM_FONT)));
SetBkColor(this->hdc_,this->bkcolor_);
SetTextColor(this->hdc_,this->txtcolor_);
SetBkMode(this->hdc_,this->bkmode_);
DrawText(this->hdc_,drawtext_.GetBuffer(drawtext_.GetLength()),drawtext_.GetLength(),& rect_,DT_LEFT | TA_NOUPDATECP | TA_TOP | DT_SINGLELINE);
}
}
return true;
}
bool ToolTipExtImpl::InitToolTip(void)
{
if(this->parent_==NULL)
{
return false;
}
else
{
if(!this->initok_)
{
this->hdc_=::GetDC(this->m_hWnd );
this->bkcolor_=::GetBkColor(this->hdc_);
this->txtcolor_=::GetTextColor(this->hdc_);
this->bkmode_=::GetBkMode(this->hdc_);
this->maxwidth_=this->CToolTipCtrl::GetMaxTipWidth();
GetTextMetrics(this->hdc_,& this->font_);
this->initok_=this->create_ && (this->hdc_!=NULL);
}
}
return this->initok_;
}
void ToolTipExtImpl::SetTipBkColor( COLORREF clr )
{
this->bkcolor_=clr;
CToolTipCtrl::SetTipBkColor(clr);
}
void ToolTipExtImpl::SetTipTextColor( COLORREF clr )
{
this->txtcolor_=clr;
CToolTipCtrl::SetTipTextColor(clr);
}
void ToolTipExtImpl::OnPaint()
{
// TODO: Add your message handler code here
CToolTipCtrl::OnPaint();
if(!this->flag_)
{
this->CheckWindowSize();
this->flag_=true;
}
else
{
this->DrawTip();
this->flag_=false;
}
}
有一个问题是设置字体的颜色与背景颜色
如何在DRAWTEXT时,能正确的设置背景及字体的颜色呢,好象在WM_PAINT中,不好处理.
只要用SetMaxTipWidth设置完宽度,就会自动换行,这样还能用CToolTipCtrl提供地函数设置背景及字体地颜色,用不着这么麻烦
gust说得对,只要设置SetMaxTipWidth就可以实现多行显示了。