Click here to Skip to main content
15,879,095 members
Articles / Desktop Programming / MFC

Static Control with ToolTip

Rate me:
Please Sign up or sign in to vote.
2.95/5 (10 votes)
13 Sep 2007CPOL 41.3K   2.1K   13  
The Tool Tip shows the Text of Static Control when the Text is clipped in Static Control.
//�y FileName �z TlTipStc.cpp
//
//�y Outline  �z CToolTipStatic
//
//�y History  �z [new] 2006/10/31 Sandesh
///////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "TlTipStc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CToolTipStatic

//----------------------------------------------------------------------------
//�y Function �zCToolTipStatic
//
//�y   Info   �zConstructor
//
//�y History  �z[new] 2006/10/31 Sandesh
//----------------------------------------------------------------------------
CToolTipStatic::CToolTipStatic()
{
	m_strText = _T("");

	COLORREF col = 0;

	col = GetSysColor(COLOR_WINDOWTEXT);
	m_clrFont = RGB(0, 0, 0);		//Text Foreground color

	col = GetSysColor(COLOR_BTNFACE);
	m_clrBckGnd	= col;				//Text Background color

	col = GetSysColor(COLOR_INFOTEXT);
	m_clrTTFont	= col;				// ToolTip Foreground color

	col = GetSysColor(COLOR_INFOBK);
	m_clrTTBckGnd = col;			// ToolTip Background color

	m_bToolTipCreated = FALSE;
	m_bFontInit	= FALSE; 
}

//----------------------------------------------------------------------------
//�y Function �z ~CToolTipStatic
//
//�y   Info   �z Destructor
//
//�y History  �z [new] 2006/10/31 Sandesh
//----------------------------------------------------------------------------
CToolTipStatic::~CToolTipStatic()
{
	m_strText.Empty();
	if(m_bFontInit){
		m_font.DeleteObject();
	}
}


BEGIN_MESSAGE_MAP(CToolTipStatic, CStatic)
	//{{AFX_MSG_MAP(CToolTipStatic)
	ON_WM_PAINT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


//----------------------------------------------------------------------------
//�y Function �z PreSubclassWindow
//
//�y   Info   �z Override to modify the window style to get mouse notifications.
//
//�y History  �z [new] 2006/10/31 Sandesh
//----------------------------------------------------------------------------
void CToolTipStatic::PreSubclassWindow() 
{
	//Modify windows style to get mouse notifications.
	DWORD dwStyle = GetStyle();
	::SetWindowLong(GetSafeHwnd(), GWL_STYLE, dwStyle | SS_NOTIFY);

	//Set the font
	CFont *pFont = NULL;
	pFont = GetFont();
	ASSERT(pFont);
	if(pFont){
		LOGFONT LogFont = {0};
		pFont->GetLogFont(&LogFont);
		if(m_font.CreateFontIndirect(&LogFont)){
			m_bFontInit = TRUE;
		}else{
			ASSERT(FALSE);
		}
	}

	// Create the tooltip
    CRect rect; 
    GetClientRect(rect);
    if(m_ToolTip.Create(this)){
		if(m_strText.IsEmpty()){
			CString strTemp;
			GetWindowText(strTemp);
			m_strText = strTemp;
		}
		if(m_ToolTip.AddTool(this, (LPCTSTR)m_strText, rect, TOOLTIP_ID)){
			m_bToolTipCreated = TRUE;
			_SetText();
		}else{
			ASSERT(FALSE);
		}
	}else{
		ASSERT(FALSE);
	}

	CStatic::PreSubclassWindow();
}

//----------------------------------------------------------------------------
//�y Function �zPreTranslateMessage
//
//�y   Info   �zRequired to forward messages to the tool tip through RelayEvent()
//
//�y History  �z[new] 2006/10/31 Sandesh
//---------------------------------------------------------------------------
BOOL CToolTipStatic::PreTranslateMessage(MSG* pMsg) 
{
	if(m_bToolTipCreated){
		m_ToolTip.RelayEvent(pMsg);
	}
	return CStatic::PreTranslateMessage(pMsg);
}

//----------------------------------------------------------------------------
//�y Function �zSetText
//
//�y   Info   �z������̐ݒ�
//
//�y  Param   �z[i] pszText : ������
//
//�y�߂�l�z ����ɏI�������ꍇ��TRUE�A����ȊO��FALSE
//
//�y History  �z[new] 2006/10/31 Sandesh
//----------------------------------------------------------------------------
BOOL CToolTipStatic::SetText(LPCTSTR pszText)
{
	if(pszText == NULL){
		ASSERT(FALSE);
		return(FALSE);
	}

	m_strText = pszText;	//������̐ݒ�

	if(m_bToolTipCreated){
		//Indicates window is active
		_SetText();
		InvalidateRect(NULL);
	}

	return(TRUE);
}

//----------------------------------------------------------------------------
//�y Function �zGetText
//
//�y   Info   �z������̎擾
//
//�y  Param   �z[o] strText	: ������
//
//�y�߂�l�z ����ɏI�������ꍇ��TRUE�A����ȊO��FALSE
//
//�y History  �z [new] 2006/10/31 Sandesh
//----------------------------------------------------------------------------
BOOL CToolTipStatic::GetText(CString& strText)
{
	strText = m_strText;
	
	return(TRUE);
}

//----------------------------------------------------------------------------
//�y Function �zSetFGColor
//
//�y   Info   �z�e�L�X�g�̐F�̐ݒ�
//
//�y  Param   �z[i] btRed		: �� (BYTE)
//				[i] btGreen	: �� (BYTE)
//				[i] btBlue		: �� (BYTE)
//
//�y�߂�l�z ����ɏI�������ꍇ��TRUE�A����ȊO��FALSE
//
//�y History  �z [new] 2006/10/31 Sandesh
//----------------------------------------------------------------------------
BOOL CToolTipStatic::SetFGColor(BYTE btRed, BYTE btGreen, BYTE btBlue)
{
	m_clrFont = RGB(btRed, btGreen, btBlue);

	if(m_bToolTipCreated){
		InvalidateRect(NULL);
	}

	return(TRUE);
}

//----------------------------------------------------------------------------
//�y Function �zGetFGColor
//
//�y   Info   �z�e�L�X�g�̐F�̎擾
//
//�y  Param   �z[o] btRed		: �� (BYTE)
//				[o] btGreen	: �� (BYTE)
//				[o] btBlue		: �� (BYTE)
//
//�y�߂�l�z ����ɏI�������ꍇ��TRUE�A����ȊO��FALSE
//
//�y History  �z [new] 2006/10/31 Sandesh
//----------------------------------------------------------------------------
BOOL CToolTipStatic::GetFGColor(BYTE& btRed, BYTE& btGreen, BYTE& btBlue)
{
	btRed	= GetRValue(m_clrFont);
	btGreen	= GetGValue(m_clrFont);
	btBlue	= GetBValue(m_clrFont);

	return(TRUE);
}

//----------------------------------------------------------------------------
//�y Function �zSetBGColor
//
//�y   Info   �z�w�i�̐F�̐ݒ�
//
//�y  Param   �z[i] btRed		: �� (BYTE)
//				[i] btGreen	: �� (BYTE)
//				[i] btBlue		: �� (BYTE)
//
//�y�߂�l�z ����ɏI�������ꍇ��TRUE�A����ȊO��FALSE
//
//�y History  �z [new] 2006/10/31 Sandesh
//----------------------------------------------------------------------------
BOOL CToolTipStatic::SetBGColor(BYTE btRed, BYTE btGreen, BYTE btBlue)
{
	m_clrBckGnd = RGB(btRed, btGreen, btBlue);

	if(m_bToolTipCreated){
		InvalidateRect(NULL);
	}

	return(TRUE);
}

//----------------------------------------------------------------------------
//�y Function �zGetBGColor
//
//�y   Info   �z�w�i�̐F�̎擾
//
//�y  Param   �z[o] btRed		: �� (BYTE)
//				[o] btGreen	: �� (BYTE)
//				[o] btBlue		: �� (BYTE)
//
//�y�߂�l�z ����ɏI�������ꍇ��TRUE�A����ȊO��FALSE
//
//�y History  �z [new] 2006/10/31 Sandesh
//----------------------------------------------------------------------------
BOOL CToolTipStatic::GetBGColor(BYTE& btRed, BYTE& btGreen, BYTE& btBlue)
{
	btRed	= GetRValue(m_clrBckGnd);
	btGreen	= GetGValue(m_clrBckGnd);
	btBlue	= GetBValue(m_clrBckGnd);

	return(TRUE);
}

//----------------------------------------------------------------------------
//�y Function �z_SetText
//
//�y   Info   �zChecks if the ToolTip is to be dialayed
//
//�y  Param   �z �Ȃ�
//
//�y�߂�l�z ����ɏI�������ꍇ��TRUE�A����ȊO��FALSE
//
//�y History  �z [new] 2006/10/31 Sandesh
//----------------------------------------------------------------------------
BOOL CToolTipStatic::_SetText()
{
	if(m_bToolTipCreated){
		//Window is existing
		CDC *pDC = GetDC();
		if(pDC == NULL){
			ASSERT(FALSE);
			return(FALSE);
		}
		//StaticControl client rectangle
		CRect rcStcCtrl(0,0,0,0);		
		GetClientRect(rcStcCtrl);

		//Store size of the formatted text to be drawn in static control.
		CRect rcNew(0, 0 ,0, 0);	
		CFont *pfontOld = NULL;
		if(m_bFontInit){
			pfontOld = (CFont *)pDC->SelectObject(&m_font);
		}
		pDC->DrawText(m_strText, &rcNew, DT_CALCRECT); 
		if(m_bFontInit){
			pDC->SelectObject(pfontOld);
		}
		ReleaseDC(pDC);

		//Check if the tool tip is to be shown
		if(rcNew.right > rcStcCtrl.right){
			m_ToolTip.UpdateTipText(m_strText, this, TOOLTIP_ID);
			m_ToolTip.Activate(TRUE);
		}else{
			m_ToolTip.UpdateTipText("", this, TOOLTIP_ID);
			m_ToolTip.Activate(FALSE);
		}
	}

	return(TRUE);
}

void CToolTipStatic::OnPaint() 
{
	CPaintDC dc(this); // �`��p�̃f�o�C�X �R���e�L�X�g
	
	// �`��p���b�Z�[�W�Ƃ��� CStatic::OnPaint() ��Ăяo���Ă͂����܂���

	//StaticControl client rectangle
	CRect rcStcCtrl(0,0,0,0);		
	GetClientRect(rcStcCtrl);

	//Set the new status of GDIs
	int nOldBckMode = dc.SetBkMode(TRANSPARENT);
	dc.FillSolidRect(rcStcCtrl, m_clrBckGnd);

	CFont *pfontOld = NULL;
	if(m_bFontInit){
		pfontOld = (CFont *)dc.SelectObject(&m_font);
	}

	COLORREF clrOld = dc.SetTextColor( m_clrFont );

	dc.DrawText(m_strText, rcStcCtrl, DT_END_ELLIPSIS);

	//Set the previous status of GDIs
	dc.SetTextColor( clrOld );
	
	if(m_bFontInit){
		dc.SelectObject(pfontOld);
	}
	
	dc.SetBkMode(nOldBckMode);
}

//----------------------------------------------------------------------------
//�y Function �zSetToolTipFGColor
//
//�y   Info   �zToolTip�̃e�L�X�g�̐F��ݒ�
//
//�y  Param   �z[i] btRed		: �� (BYTE)
//				[i] btGreen	: �� (BYTE)
//				[i] btBlue		: �� (BYTE)
//
//�y�߂�l�z ����ɏI�������ꍇ��TRUE�A����ȊO��FALSE
//
//�y History  �z [new] 2006/10/31 Sandesh
//----------------------------------------------------------------------------
BOOL CToolTipStatic::SetToolTipFGColor(BYTE btRed, BYTE btGreen, BYTE btBlue)
{
	m_clrTTFont = RGB(btRed, btGreen, btBlue);

	if(m_bToolTipCreated){
		m_ToolTip.SetTipTextColor(m_clrTTFont);
	}

	return(TRUE);
}

//----------------------------------------------------------------------------
//�y Function �zGetToolTipFGColor
//
//�y   Info   �zToolTip�̃e�L�X�g�̐F��擾
//
//�y  Param   �z[o] btRed		: �� (BYTE)
//				[o] btGreen	: �� (BYTE)
//				[o] btBlue		: �� (BYTE)
//
//�y�߂�l�z ����ɏI�������ꍇ��TRUE�A����ȊO��FALSE
//
//�y History  �z [new] 2006/10/31 Sandesh
//----------------------------------------------------------------------------
BOOL CToolTipStatic::GetToolTipFGColor(BYTE& btRed, BYTE& btGreen, BYTE& btBlue)
{
	if(m_bToolTipCreated){
		m_clrTTFont = m_ToolTip.GetTipTextColor();
	}
	btRed	= GetRValue(m_clrTTFont);
	btGreen	= GetGValue(m_clrTTFont);
	btBlue	= GetBValue(m_clrTTFont);

	return(TRUE);
}

//----------------------------------------------------------------------------
//�y Function �zSetToolTipBGColor
//
//�y   Info   �zTool-Tip�̔w�i�̐F��ݒ�
//
//�y  Param   �z[i] btRed		: �� (BYTE)
//				[i] btGreen	: �� (BYTE)
//				[i] btBlue		: �� (BYTE)
//
//�y�߂�l�z ����ɏI�������ꍇ��TRUE�A����ȊO��FALSE
//
//�y History  �z [new] 2006/10/31 Sandesh
//----------------------------------------------------------------------------
BOOL CToolTipStatic::SetToolTipBGColor(BYTE btRed, BYTE btGreen, BYTE btBlue)
{
	m_clrTTBckGnd = RGB(btRed, btGreen, btBlue);

	if(m_bToolTipCreated){
		m_ToolTip.SetTipBkColor(m_clrTTBckGnd);
	}

	return(TRUE);
}

//----------------------------------------------------------------------------
//�y Function �zGetToolTipBGColor
//
//�y   Info   �zTool-Tip�̔w�i�̐F��擾
//
//�y  Param   �z[o] btRed		: �� (BYTE)
//				[o] btGreen	: �� (BYTE)
//				[o] btBlue		: �� (BYTE)
//
//�y�߂�l�z ����ɏI�������ꍇ��TRUE�A����ȊO��FALSE
//
//�y History  �z [new] 2006/10/31 Sandesh
//----------------------------------------------------------------------------
BOOL CToolTipStatic::GetToolTipBGColor(BYTE& btRed, BYTE& btGreen, BYTE& btBlue)
{
	if(m_bToolTipCreated){
		m_clrTTBckGnd = m_ToolTip.GetTipBkColor();
	}

	btRed	= GetRValue(m_clrTTBckGnd);
	btGreen	= GetGValue(m_clrTTBckGnd);
	btBlue	= GetBValue(m_clrTTBckGnd);

	return(TRUE);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
me a VC++ programmer

Comments and Discussions