重载一个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;
}
//这样鼠标放上去就出现手型了

解决方案 »

  1.   

    CToolTipCtrl m_ToolTip;   
    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);
    }
      

  2.   

    zzh,如何设置?多谢111222,但这样出来整个view中都出现手型了,我只要点击bitmap按钮时出现!
      

  3.   

    我是在控件的父窗口里响应OnSetCursor函数,然后判断光标位置是否在控件中
    BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
    {
    GetCursorPos( &ptCursor );
    m_Button.GetWindowRect( &rtWindow );
    if( rtWindow.PtInRect( *lpCursor ) )
    ......
      

  4.   

    至于ToolTip,可以在父窗口中响应TTN_NEEDTEXT消息就可以了。ON_NOTIFY_EX( TTN_NEEDTEXT, 0, OnToolTipNotify )BOOL OnToolTipNotify( UINT id, NMHDR * pNMHDR, LRESULT * pResult )
    {
    TOOLTIPTEXT *pTTT = (TOOLTIPTEXT *)pNMHDR;
    if( (HWND)pNMHDR->idFrom == m_Button.GetSafeHwnd() )
        _tcscpy( pTTT->lpszText, strHint );
    ......
      

  5.   

    111222,多谢,关于set cursor的问题,我已经用你的方法解决了!但为什么tooltip用这段代码,程序运行时就会报错!
    "a exception breakpoint!"另外再问你一下:我有好多个bitmapbutton,我要给它们都加上tooltip
    是不是要加一个CMyBmpBtn::SetToolTip(LPCTSTR lpszTip);函数?
      

  6.   

    我写了个类,包括cursor和tooltip用法:  #include "CMyBmpBtn.h"
           
           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);      }////我用了着,还成:)
      

  7.   

    SmartHeart:
          嘻,考完试偶就回来勒,到水员还报了个好,只是很快就结帖子了
          回来准备好好学习科学技术,不灌水了:)
          我也挺想大伙的,总能想起
      

  8.   

    多谢111222,50分送上!有个小问题:
    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大虾!多谢!
      

  9.   

    BOOL AddTool( CWnd* pWnd, LPCTSTR lpszText = LPSTR_TEXTCALLBACK, LPCRECT lpRectTool = NULL, UINT nIDTool = 0 );抱歉,我没有看MSDN里面AddTool函数的原形,其中第3个参数指定是NULL