关于tooltips的问题

jinfuzhao 2002-04-29 04:21:50
怎样给在对话框中的picture控件加上tooltip。
...全文
107 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
jinfuzhao 2002-04-30
  • 打赏
  • 举报
回复
我的email是jinfuzhao0909.sina.com.cn
Hover 2002-04-30
  • 打赏
  • 举报
回复
这是个在Edit控制中用Tip的代码,你可以把它移到你的picture控件的派生类中

类中声明:
bool m_bSelectable;
CToolTipCtrl m_ToolTip;

public:
void SetTipText(string& sText, bool bActivate=true);
void ActivateTooltip(bool bActivate = true);

void InitToolTip();

virtual BOOL PreTranslateMessage(MSG* pMsg);



BOOL CEditEx::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
InitToolTip();
m_ToolTip.RelayEvent(pMsg);
return CEdit::PreTranslateMessage(pMsg);
}

void CEditEx::SetTipText(string &sText,bool bActivate)
{
// We cannot accept NULL pointer
if (sText.empty()) return;

// Initialize ToolTip
InitToolTip();

// If there is no tooltip defined then add it
if (m_ToolTip.GetToolCount() == 0)
{
CRect rectBtn;
GetClientRect(rectBtn);
m_ToolTip.AddTool(this,sText.c_str(), rectBtn, 1);
}

// Set text for tooltip
m_ToolTip.UpdateTipText(sText.c_str(), this, 1);
m_ToolTip.SetMaxTipWidth(180);
m_ToolTip.Activate(bActivate);
}

void CEditEx::InitToolTip()
{
if (m_ToolTip.m_hWnd == NULL)
{
// Create ToolTip control
m_ToolTip.Create(this);
// Create inactive
m_ToolTip.Activate(FALSE);
}
} // End of InitToolTip

// Activate the tooltip
void CEditEx::ActivateTooltip(bool bActivate)
{
// If there is no tooltip then do nothing
if (m_ToolTip.GetToolCount() == 0) return;

// Activate tooltip
m_ToolTip.Activate(bActivate);
} // End of EnableTooltip
Anco 2002-04-29
  • 打赏
  • 举报
回复
UP
aicai 2002-04-29
  • 打赏
  • 举报
回复
还是留下呢的e_mail!!!
aicai 2002-04-29
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include "MFECToolTip.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMFECToolTip

CMFECToolTip::CMFECToolTip()
{
m_nHeight = 0;
m_nWidth = 0;
m_nFontSize = 14; // original size
m_currentControlID = 0;
m_pParentWnd = NULL;

m_aControlInfo.RemoveAll();
}

CMFECToolTip::~CMFECToolTip()
{
ToolTipInfo *pInfo = NULL;
int nSize = m_aControlInfo.GetSize();
for( int i = 0; i < nSize; i++ )
{
pInfo = (ToolTipInfo*)m_aControlInfo.GetAt( i );
delete pInfo;
}

m_aControlInfo.RemoveAll();
}

void CMFECToolTip::Create(CWnd* pWnd)
{
m_pParentWnd = pWnd;
}

void CMFECToolTip::ErasePreviousToolTipDisplay( UINT nControlID )
{
//This assertion fails because you did not call Create() first.
//m_pParentWnd was not initialized.
ASSERT(m_pParentWnd);

//if erase already, do not do it again
if((m_nHeight == 0) || (m_nWidth == 0))
return;

CRect oInfoRect(0,0,0,0);

CalculateInfoBoxRect(nControlID, &oInfoRect);
m_pParentWnd->InvalidateRect(&oInfoRect);
m_pParentWnd->UpdateWindow();

m_nHeight = 0;
m_nWidth = 0;
}


void
CMFECToolTip::CalculateHeightAndWidth(CStringArray& straInfos)
{
//This assertion fails because you did not call Create() first.
//m_pParentWnd was not initialized.
ASSERT(m_pParentWnd);

int nMaxLength = 0;
int nLength;
int nLine = straInfos.GetSize();
m_nTotalLine = nLine; // holder for maximum line of information
for(int i=0; i<nLine; i++)
{
nLength = (straInfos[i]).GetLength();
if(nLength > nMaxLength)
nMaxLength = nLength;
}

m_maxCharInLine = nMaxLength; // holder for longest char info
m_nHeight = 12 + nLine * (m_nFontSize - 1); //m_nFontSize is the font size
m_nWidth = 10 + (int)(7.5 * nMaxLength); //aproximately 5 pixel per char
}


void CMFECToolTip::CalculateInfoBoxRect(UINT nControlID, CRect* pInfoRect)
{
ASSERT(m_pParentWnd);

CRect oRect(0,0,0,0);
CRect oParentWindowRect(0,0,0,0);

m_pParentWnd->GetWindowRect(&oParentWindowRect);
m_pParentWnd->ScreenToClient(&oParentWindowRect);

CWnd* pWnd = m_pParentWnd->GetDlgItem(nControlID);
ASSERT(pWnd);
pWnd->GetWindowRect(&oRect);
m_pParentWnd->ScreenToClient(&oRect);

//check if the box fit in the parent dialog
SetFontSize( 14 );
int nBottom = oRect.bottom - ((oRect.bottom - oRect.top)/2) + m_nHeight;
if(nBottom <= oParentWindowRect.bottom)
{
pInfoRect->top = oRect.bottom - (oRect.bottom - oRect.top)/2;
pInfoRect->bottom = pInfoRect->top + m_nHeight;
}
else
{
// change the size of the font as well as the info box if all
// info data cannot be viewed
if( m_nHeight > (oParentWindowRect.bottom - oParentWindowRect.top) )
{
SetFontSize( 12 );
m_nHeight = 12 + m_nTotalLine * (m_nFontSize - 1); //m_nFontSize is the font size
m_nWidth = 10 + (int)(7 * m_maxCharInLine);
}
// end
pInfoRect->bottom = oParentWindowRect.bottom - 1;
pInfoRect->top = pInfoRect->bottom - m_nHeight;
}

//check if the box fit in the parent dialog
int nRight = (oRect.left + oRect.right)/2 + m_nWidth;
if(nRight <= oParentWindowRect.right)
{
pInfoRect->left = (oRect.left + oRect.right)/2;
pInfoRect->right = pInfoRect->left + m_nWidth;
}
else
{
int nLeft = (oRect.left + oRect.right)/2 - m_nWidth;
if(nLeft <= oParentWindowRect.left)
{
pInfoRect->left = oParentWindowRect.left + 1;
pInfoRect->right = pInfoRect->left + m_nWidth;
}
else
{
pInfoRect->right = (oRect.left + oRect.right)/2;
pInfoRect->left = pInfoRect->right - m_nWidth;
}
}

ASSERT(pInfoRect->top <= pInfoRect->bottom);
ASSERT(pInfoRect->left <= pInfoRect->right);
}


void CMFECToolTip::ShowToolTip( UINT nControlID )
{
ToolTipInfo *pToolTip = IsControlIDExisting( nControlID );
if( pToolTip == NULL )
return;

DisplayInfo( pToolTip );
}


void CMFECToolTip::ShowToolTip( CPoint& point )
{
CWnd* pWnd = m_pParentWnd->ChildWindowFromPoint(point);
if( pWnd )
{
UINT nControlID = (UINT)pWnd->GetDlgCtrlID();
if( m_currentControlID != nControlID )
{
ErasePreviousToolTipDisplay( m_currentControlID );
ShowToolTip( nControlID );
}
}
}

void CMFECToolTip::DisplayInfo( ToolTipInfo* pToolTip )
{
if( pToolTip->nInfoSize <= 0 )
return;

ASSERT(m_pParentWnd);

CDC* pDC = m_pParentWnd->GetDC();

CRect oInfoRect;
CBrush oBrush, *pOldBrush, oBorderBrush;
int nX, nY;
TEXTMETRIC TM;
int nTextHigh;
CFont oFont, *pOldFont;
CWnd* pWnd = NULL;

pDC->SetBkMode(TRANSPARENT);

oBrush.CreateSolidBrush( pToolTip->nBackColor );

pOldBrush = pDC->SelectObject( &oBrush );
pDC->SetTextColor( pToolTip->nTextColor );

//calculate the width and height of the box dynamically
CalculateHeightAndWidth( pToolTip->nControlInfo );
CalculateInfoBoxRect( pToolTip->nControlID, &oInfoRect );

oFont.CreateFont(m_nFontSize, 0, 0, 0, FW_REGULAR, 0, 0, 0, 0, 0, 0, 0, 0, "Courier New");
pOldFont = pDC->SelectObject(&oFont);

pDC->FillRect(&oInfoRect, &oBrush);
pDC->SelectObject(pOldBrush);
oBrush.DeleteObject();

oBorderBrush.CreateSolidBrush( pToolTip->nTextColor );
pOldBrush = pDC->SelectObject(&oBorderBrush);
DrawEdge(pDC->m_hDC, &oInfoRect, BDR_RAISEDINNER | BDR_SUNKENOUTER, BF_RECT);

pDC->SetTextAlign(TA_LEFT);
pDC->GetTextMetrics(&TM);
nTextHigh = TM.tmHeight + TM.tmExternalLeading - 2;
nX = oInfoRect.left + 8;
nY = oInfoRect.top + 5;

for( register UINT i = 0; i < pToolTip->nInfoSize; i++)
{
pDC->TextOut(nX, nY, pToolTip->nControlInfo[i]);
nY += m_nFontSize - 1;
}

pDC->SelectObject(pOldBrush);
oBorderBrush.DeleteObject();
m_pParentWnd->ReleaseDC(pDC);
}


BOOL CMFECToolTip::AddControlInfo( UINT contolID, CStringArray& straInfo, COLORREF back, COLORREF text )
{
ToolTipInfo *pToolTip = new ToolTipInfo;
ASSERT( pToolTip != NULL );

int nSize = straInfo.GetSize();
if( pToolTip <= 0 ) // no information, don't add to the list and return FALSE
{
delete pToolTip;
return FALSE;
}

pToolTip->nControlInfo.RemoveAll();
pToolTip->nInfoSize = nSize;
pToolTip->nControlID = contolID;
for( register int i = 0; i < nSize; i++ )
{
pToolTip->nControlInfo.Add( straInfo[i] );
}
// initialize colors, use default if not supplied
pToolTip->nBackColor = back;
pToolTip->nTextColor = text;

// add to the list
m_aControlInfo.Add( pToolTip );

return TRUE;
}

ToolTipInfo* CMFECToolTip::IsControlIDExisting( UINT controlID )
{
ToolTipInfo *pToolTip = NULL;
int nSize = m_aControlInfo.GetSize();
for( register int i = 0; i < nSize; i++ )
{
pToolTip = (ToolTipInfo*)m_aControlInfo.GetAt( i );
if( pToolTip->nControlID == controlID ) // if found!
{
m_currentControlID = controlID;
return pToolTip;
}
}
m_currentControlID = 0; // if not found, reset the current control ID to refresh the display

return NULL;
}

BOOL CMFECToolTip::RemoveControlInfo( UINT controlID )
{
ToolTipInfo *pToolTip = NULL;
int nSize = m_aControlInfo.GetSize();
for( register int i = 0; i < nSize; i++ )
{
pToolTip = (ToolTipInfo*)m_aControlInfo.GetAt( i );
if( pToolTip->nControlID == controlID ) // if found!
{
if( m_currentControlID == controlID )
ErasePreviousToolTipDisplay( m_currentControlID );

m_aControlInfo.RemoveAt( i ); // remove the entry
delete pToolTip;
break; // break from the loop
}
}

return TRUE;
}

aicai 2002-04-29
  • 打赏
  • 举报
回复
Head.h
// Author: Ferdinand V. Abne
// Email : fabne@bigfoot.com
// Revision History:
// July 16, 1998 - Creation

#ifndef _MFECTOOLTIP_H_
#define _MFECTOOLTIP_H_


// default tooltip colors
#define IVORY RGB(255, 255, 220)
#define BLACK RGB(0, 0, 0)

// this structure holds all the tooltip information
struct ToolTipInfo : public CObject
{
UINT nControlID; // ID of the control
UINT nInfoSize; // number of lines in the info
CStringArray nControlInfo; // container of the information
COLORREF nTextColor; // text color
COLORREF nBackColor; // background color
};

class CMFECToolTip : public CWnd
{
// Construction
public:
CMFECToolTip(); // standard constructor
virtual ~CMFECToolTip(); // destructor

void Create(CWnd* pWnd);
void ErasePreviousToolTipDisplay( UINT );
void ShowToolTip( UINT ); // explicitly shown the tooltip given the control ID

// NOTE: the user must override the PreTranslateMessage in the calling window in order
// to handle mousemovent.
void ShowToolTip( CPoint& ); // called only during Mousemovement

// tooltip functions
BOOL AddControlInfo( UINT, CStringArray&, COLORREF back=IVORY, COLORREF text=BLACK );
BOOL RemoveControlInfo( UINT );

// inline functions
void SetFontSize( int size ) { m_nFontSize = size; }

// Implementation
protected:
CWnd* m_pParentWnd;
int m_nHeight;
int m_nWidth;
int m_nFontSize;

int m_nTotalLine;
int m_maxCharInLine;

virtual void CalculateInfoBoxRect(UINT nControlID, CRect* pInfoRect);
virtual void CalculateHeightAndWidth(CStringArray& straInfos);

private:
ToolTipInfo* IsControlIDExisting( UINT );
void DisplayInfo( ToolTipInfo* );

private:
CObArray m_aControlInfo;
UINT m_currentControlID;
};


#endif
Hover 2002-04-29
  • 打赏
  • 举报
回复
留下mail我给你代码

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧