重载一个CBitmapButton,填加WM_SETCURSOR响应函数BOOL CMyBmpBtn::OnSetCursor( CWnd* pWnd, UINT nHitTest, UINT message )
{
static HCURSOR m_hLinkCursor; if (m_hLinkCursor == NULL) // No cursor handle
{
// Get the windows directory
CString strWndDir;
GetWindowsDirectory(strWndDir.GetBuffer(100), 100);
strWndDir.ReleaseBuffer(); strWndDir += _T("\\winhlp32.exe");
// This retrieves cursor #106 from winhlp32.exe, which is a hand pointer
HMODULE hModule = LoadLibrary(strWndDir);
if (hModule) {
HCURSOR hHandCursor = ::LoadCursor(hModule, MAKEINTRESOURCE(106));
if (hHandCursor)
m_hLinkCursor = CopyCursor(hHandCursor);
}
FreeLibrary(hModule);
} ::SetCursor(m_hLinkCursor);
return TRUE;
}
//这样鼠标放上去就出现手型了
{
static HCURSOR m_hLinkCursor; if (m_hLinkCursor == NULL) // No cursor handle
{
// Get the windows directory
CString strWndDir;
GetWindowsDirectory(strWndDir.GetBuffer(100), 100);
strWndDir.ReleaseBuffer(); strWndDir += _T("\\winhlp32.exe");
// This retrieves cursor #106 from winhlp32.exe, which is a hand pointer
HMODULE hModule = LoadLibrary(strWndDir);
if (hModule) {
HCURSOR hHandCursor = ::LoadCursor(hModule, MAKEINTRESOURCE(106));
if (hHandCursor)
m_hLinkCursor = CopyCursor(hHandCursor);
}
FreeLibrary(hModule);
} ::SetCursor(m_hLinkCursor);
return TRUE;
}
//这样鼠标放上去就出现手型了
void CMyBmpBtn::PreSubclassWindow()
{
CRect rect;
GetClientRect(rect);
m_ToolTip.Create(this);
m_ToolTip.AddTool(this, _T("按时扩大解放和喀什的"), rect, 0);
}
BOOL CMyBmpBtn::PreTranslateMessage(MSG* pMsg)
{
m_ToolTip.RelayEvent(pMsg);
return CBitmapButton::PreTranslateMessage(pMsg);
}
BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
GetCursorPos( &ptCursor );
m_Button.GetWindowRect( &rtWindow );
if( rtWindow.PtInRect( *lpCursor ) )
......
{
TOOLTIPTEXT *pTTT = (TOOLTIPTEXT *)pNMHDR;
if( (HWND)pNMHDR->idFrom == m_Button.GetSafeHwnd() )
_tcscpy( pTTT->lpszText, strHint );
......
"a exception breakpoint!"另外再问你一下:我有好多个bitmapbutton,我要给它们都加上tooltip
是不是要加一个CMyBmpBtn::SetToolTip(LPCTSTR lpszTip);函数?
CMyBmpBtn btn;
btn.SetToolTip("提示");//初始化时候设置就成了---------------下面是代码---------------------
///////////////////////////////////
// CMyBmpBtn.h ////////////////////
///////////////////////////////////#if !defined(AFX_MYBMPBTN_H__E4B18361_7AAC_11D5_9D99_444553540000__INCLUDED_)
#define AFX_MYBMPBTN_H__E4B18361_7AAC_11D5_9D99_444553540000__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// MyBmpBtn.h : header file
///////////////////////////////////////////////////////////////////////////////
// CMyBmpBtn windowclass CMyBmpBtn : public CBitmapButton
{
// Construction
public:
CMyBmpBtn();// Attributes
public:// Operations
public:
CToolTipCtrl m_ToolTip;
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMyBmpBtn)
public:
virtual BOOL PreTranslateMessage(MSG* pMsg);
protected:
//}}AFX_VIRTUAL// Implementation
public:
void SetToolTip(CString str);
virtual ~CMyBmpBtn(); // Generated message map functions
protected:
//{{AFX_MSG(CMyBmpBtn)
afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
//}}AFX_MSG DECLARE_MESSAGE_MAP()
};///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_MYBMPBTN_H__E4B18361_7AAC_11D5_9D99_444553540000__INCLUDED_)/////////////////////////////////////////
// MyBmpBtn.cpp : implementation file
/////////////////////////////////////////#include "stdafx.h"
#include "MyBmpBtn.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CMyBmpBtnCMyBmpBtn::CMyBmpBtn()
{
}CMyBmpBtn::~CMyBmpBtn()
{
}
BEGIN_MESSAGE_MAP(CMyBmpBtn, CButton)
//{{AFX_MSG_MAP(CMyBmpBtn)
ON_WM_SETCURSOR()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CMyBmpBtn message handlersBOOL CMyBmpBtn::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
// TODO: Add your message handler code here and/or call default
static HCURSOR m_hLinkCursor; if (m_hLinkCursor == NULL) // No cursor handle
{
// Get the windows directory
CString strWndDir;
GetWindowsDirectory(strWndDir.GetBuffer(100), 100);
strWndDir.ReleaseBuffer(); strWndDir += _T("\\winhlp32.exe");
// This retrieves cursor #106 from winhlp32.exe, which is a hand pointer
HMODULE hModule = LoadLibrary(strWndDir);
if (hModule) {
HCURSOR hHandCursor = ::LoadCursor(hModule, MAKEINTRESOURCE(106));
if (hHandCursor)
m_hLinkCursor = CopyCursor(hHandCursor);
}
FreeLibrary(hModule);
} ::SetCursor(m_hLinkCursor);
return TRUE;
}BOOL CMyBmpBtn::PreTranslateMessage(MSG* pMsg)
{
m_ToolTip.RelayEvent(pMsg);
return CButton::PreTranslateMessage(pMsg);
}void CMyBmpBtn::SetToolTip(CString str)
{
CRect rect;
GetClientRect(rect);
m_ToolTip.Create(this);
m_ToolTip.AddTool(this, str, rect, 0); }////我用了着,还成:)
嘻,考完试偶就回来勒,到水员还报了个好,只是很快就结帖子了
回来准备好好学习科学技术,不灌水了:)
我也挺想大伙的,总能想起
void CMyBmpBtn::SetToolTip(CString str)
{
CRect rect;
GetClientRect(rect);
m_ToolTip.Create(this);
m_ToolTip.AddTool(this, str, rect, 0); }函数中,这样用在我的程序中,一启动就报错!
问题出在m_ToolTip.AddTool(this , str, rect, 0);这行,单步调试到
VC中TOOLTIP.CPP文件的138行:(lpRectTool == NULL) && (nIDTool == 0));处:我将rect, 改为NULL, 就OK了!!不知是什么原因?不管怎么说,多谢111222大虾!多谢!