我在对话框中想给工具栏按钮加工具提示,
试了网上的几个方法都没有试出来,
望能够指条明路。

解决方案 »

  1.   

    Enabling Tooltips for Toolbars in Dialogs
    Introduction
    When I posted an article discussing the subject of placing a toolbar right in the middle of elsewhere, Daniel Kaminsky pointed out, that the tooltips were not displayed. So I sat down, and placed the line EnableToolTips() in my dialogs OnInitDialog function.
    And what shall I tell you, it did not work.
    Every dialog and every view showed the toolbar, but no tooltips, or occasionally only very odd ones. Having a look at the help, there it was stated.
    Simply calling EnableToolTips is not enough to display tool tips for your child controls unless the parent window is derived from CFrameWnd. And because a CDialog or CView is not a descendant of CFrameWnd the tooltips will not get displayed.
    But a solution was also at the bottom of this article, which I implemented without further thinking. It worked well except to the fact that it showed something like :"Your mouse is hovering over the control with the ID 1234". That was something I never used as tooltip text.
    Because I wanted the text, stored in the resource of the toolbar being displayed, I looked up the source codes of the MFC. The solution provided here is solely from the peoples of Microsoft. My only creative part, is finding the places to copy! Step1
    Create a toolbar and attach it to your dialog. Use the normal method of creating a variable of type CToolBar, and ... (or look at the source). In the creation process during OnInit...(), make sure that the barstyle flag CBRS_TOOLTIPS is set.
    m_wndToolBar.SetBarStyle(CBRS_ALIGN_ANY | CBRS_TOOLTIPS | CBRS_FLYBY); 
    Then call EnableToolTips(true); If you compile now, and run the application, you can see the tooltips not to work. Step2
    Now comes the fun part.
    Create a message map entry in your CPP-file.
    ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTW, 0, 0xFFFF, OnToolTipNotify)
    ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTA, 0, 0xFFFF, OnToolTipNotify)
    Or copy these two lines just before the END_MESSAGE_MAP() line in your dialogs cpp-file. According to the documentation you must create an entry for both TTN_NEEDTEXTW and TTN_NEEDTEXTA notifications. In your header file declare a function prototype for OnToolTipNotify:
    afx_msg BOOL OnToolTipNotify(UINT id, NMHDR *pNMHDR,LRESULT *pResult); 
    Or copy this line just before the DECLARE_MESSAGE_MAP() line in your dialogs h-file. If you compile now, the compiler will tell you, that it needs a function named OnToolTipNotify. Step 3
    Now comes the copy part.
    Write a new line in your dialogs implementation file (*.cpp):
    BOOL CMyDlgOrView:OnToolTipNotify(UINT, NMHDR* pNMHDR, LRESULT* pResult)
    Open WINFRM.CPP in your MFC-Source directory. Copy the complete function body from 
    BOOL CFrameWnd::OnToolTipText(UINT, NMHDR* pNMHDR, LRESULT* pResult)
    and paste it just beneath under the new written line in your dialogs/view implementation file.
    Do not compile! I told you not to!
    Add #include <afxpriv.h> either at the beginning of the dialogs header file or elsewhere in stdafx.h. Thus the functions AfxExtractSubString and AfxLoadString are known.
    Find the following lines from AFXISAPI.H (source directory for MFC->Include)
    #ifndef _countof
    #define _countof(array) (sizeof(array)/sizeof(array[0]))
    #endif
    and paste them into stdafx.h. Compile, and see the tooltips work 
      

  2.   

    Give you the detail code.
    ----------------------------->
    #include <afxpriv.h>
    #ifndef _countof
    #define _countof(array) (sizeof(array)/sizeof(array[0]))
    #endif
    afx_msg BOOL OnToolTipNotify(UINT id, NMHDR *pNMHDR,LRESULT *pResult);
    DECLARE_MESSAGE_MAP()---------------->Below is the implementation file<--------------
    BEGIN_MESSAGE_MAP(CDlgTooltip, CDialog)
    //{{AFX_MSG_MAP(CDlgTooltip)
    //}}AFX_MSG_MAP
       ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTW, 0, 0xFFFF, OnToolTipNotify)
       ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTA, 0, 0xFFFF, OnToolTipNotify)
    END_MESSAGE_MAP()BOOL CDlgTooltip::OnInitDialog() 
    {
      CDialog::OnInitDialog();
    // place toolbar in the dialogues window
      m_wndToolBar.Create(this);
      m_wndToolBar.LoadToolBar(IDR_MAINFRAME);
      m_wndToolBar.SetBarStyle(CBRS_ALIGN_ANY | CBRS_TOOLTIPS | CBRS_FLYBY);
      RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);
      m_wndToolBar.ShowWindow(SW_SHOW);  // Must be done to make tooltips work
      EnableToolTips(true);
      
      return TRUE;  // return TRUE unless you set the focus to a control
                    // EXCEPTION: OCX-Eigenschaftenseiten sollten FALSE zur點kgeben
    }BOOL CDlgTooltip::OnToolTipNotify(UINT id, NMHDR *pNMHDR,LRESULT *pResult)
    {
       // need to handle both ANSI and UNICODE versions of the message
       TOOLTIPTEXTA* pTTTA = (TOOLTIPTEXTA*)pNMHDR;
       TOOLTIPTEXTW* pTTTW = (TOOLTIPTEXTW*)pNMHDR;
       TCHAR szFullText[256];
       CString strTipText;
       UINT nID = pNMHDR->idFrom;
       if (pNMHDR->code == TTN_NEEDTEXTA && (pTTTA->uFlags & TTF_IDISHWND) ||
          pNMHDR->code == TTN_NEEDTEXTW && (pTTTW->uFlags & TTF_IDISHWND))
       {
          // idFrom is actually the HWND of the tool
          nID = ::GetDlgCtrlID((HWND)nID);
       }   if (nID != 0) // will be zero on a separator
    {
    // don't handle the message if no string resource found
    if (AfxLoadString(nID, szFullText) == 0)
    return FALSE; // this is the command id, not the button index
    AfxExtractSubString(strTipText, szFullText, 1, '\n');
    }#ifndef _UNICODE
    if (pNMHDR->code == TTN_NEEDTEXTA)
    lstrcpyn(pTTTA->szText, strTipText, _countof(pTTTA->szText));
    else
    _mbstowcsz(pTTTW->szText, strTipText, _countof(pTTTW->szText));
    #else
    if (pNMHDR->code == TTN_NEEDTEXTA)
    _wcstombsz(pTTTA->szText, strTipText, _countof(pTTTA->szText));
    else
    lstrcpyn(pTTTW->szText, strTipText, _countof(pTTTW->szText));
    #endif
       *pResult = 0;   return TRUE;    // message was handled
    }
      

  3.   

    1。首先要确保你的工具条在Create或者CreateEx时具有了CBRS_TOOLTIPS属性。
    2。假设你的那个按钮的ID是ID_&&&,那么就再增加一个具有相同ID的字符串资源;
       例如:如果该字符串的值为 aaaa\nbbbb,那么,鼠标移过时的提示应该是"aaaa",同时,状态栏上显示bbbb。