文章編號: 194294 - 上次校閱: 2006年11月21日 - 版次: 2.2

如何將工具列和工具提示新增 ActiveX 控制項

系統提示本文適用於您使用的作業系統之外的作業系統。與您不相關的文章內容已停用。
全部展開 | 全部摺疊

結論

ActiveX 控制項可以為它的子視窗有工具列 (CToolBar 類別)。本文顯示建立這類工具列的方式,以及如何實作工具提示的工具列視窗上的按鈕也。

其他相關資訊

Visual C++ 提供兩個方法來建立工具列。發行項 desribes 如何建立使用資源編輯器的工具列資源。如果您已經點陣圖資源請參閱線上文件 」 轉換點陣圖來工具列 」 來將點陣圖資源轉換成工具列資源。

步驟如下所示:

  1. 使用 MFC ActiveX 控制項精靈來產生 MFC ActiveX 控制項。
  2. 控制項的專案中建立工具列資源。
  3. 在工具列中新增每個按鈕的工具提示字串資源。這些工具提示字串資源將會載入 TTN_NEEDTEXT 告知程式碼處理常式中。
  4. 將 WH_GETMESSAGE 攔截回呼函式加入至 ActiveX 控制項-衍生類別。勾點程序呼叫應用程式的 PreTranslateMessage() 負責的這會導致對啟動工具提示的 FilterToolTipMessage() 呼叫。勾點程序需要因為 ActiveX 控制項就如同沒有訊息幫浦找到一個 inproc 伺服器 [ASCII 151]:
          HHOOK hHook = NULL;
    
          // Hook procedure for WH_GETMESSAGE hook type.
          LRESULT CALLBACK GetMessageProc(int nCode, WPARAM wParam, LPARAM
             lParam)
          {
            // Switch the module state for the correct handle to be used.
            AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
    
            // If this is a keystrokes message, translate it in controls'
            // PreTranslateMessage().
            LPMSG lpMsg = (LPMSG) lParam;
    
            if( (nCode >= 0) &&
              PM_REMOVE == wParam &&
              AfxGetApp()->PreTranslateMessage(lpMsg))
            {
               lpMsg->message = WM_NULL;
               lpMsg->lParam = 0L;
               lpMsg->wParam = 0;
            }
    
    
           // Passes the hook information to the next hook procedure in
           // the current hook chain.
           return ::CallNextHookEx(hHook, nCode, wParam, lParam);
          }
    					
  5. 建立工具列視窗 (CToolBar 類別),也就是 ActiveX 控制項的子視窗。這是以 WM_CREATE 訊息的回應。在另外 WM_CREATE 訊息處理常式也是要安裝 WH_GETMESSAGE 勾點程序的好地方
          int CCToolBarCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
          {
             if (COleControl::OnCreate(lpCreateStruct) == -1)
                return -1;
    
             // Create a CToolBar window which is a child of ActiveX control.
             if (!m_ToolBar.Create(this,
                   WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_TOOLTIPS) ||
                  !m_ToolBar.LoadToolBar(IDR_TOOLBAR))
                {
                   TRACE0("Failed to create toolbar\n");
                   return -1;      // fail to create
                }
    
             // Toolbar has to have TBSTYLE_TOOLTIPS style. Otherwise,
             // notification handler for TTN_NEXTTEXT won't be called.
             m_ToolBar.ModifyStyle (0, TBSTYLE_TOOLTIPS);
    
             // Move the toolbar so it is VISIBLE on the screen.
             CRect rc;
             GetClientRect(&rc);
             rc.bottom = rc.top + 34;
             m_ToolBar.MoveWindow(&rc);
    
             // Because ActiveX control is an inproc server, it does not have a
             // message pump. So, messages to child windows created by the
             // ActiveX control are not going to be received by the control.
             // Thus, we set up a message hook to call PreTranslateMessage().
             // This results in the call to FilterToolTipMessage(), which
             // activates tooltips.
             hHook = ::SetWindowsHookEx(
                WH_GETMESSAGE,
                GetMessageProc,
                AfxGetInstanceHandle(),
                GetCurrentThreadId());
             ASSERT (hHook);
    
             return 0;
          }
    					
  6. 解除安裝訊息攔截函式,以回應 WM_DESTROY 訊息:
          void CCToolBarCtrl::OnDestroy()
          {
             // Remove the message hook function.
             VERIFY (::UnhookWindowsHookEx (hHook));
    
             COleControl::OnDestroy();
          }
    					
  7. 將 TTN_NEEDTEXTW (用於 Unicode 告知程式碼) 或 (針對 ANSI 告知程式碼) TTN_NEEDTEXTA 通知處理常式加入至 ActiveX 控制項衍生類別中。載入要顯示在此告知程式碼處理常式中的工具提示字串:
          BEGIN_MESSAGE_MAP(CCToolBarCtrl, COleControl)
             //{{AFX_MSG_MAP(CCToolBarCtrl)
             ON_WM_CREATE()
             ON_COMMAND(ID_BUTTON1, OnButton1) // first button on toolbar
             ON_COMMAND(ID_BUTTON2, OnButton2) // second button on toolbar
             ON_COMMAND(ID_BUTTON3, OnButton3) // third button on toolbar
             ON_WM_DESTROY()
             //}}AFX_MSG_MAP
             ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
    
             // ANSI notification code (for Windows 95)
             ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTA, 0, 0xFFFF, OnToolTipNotify)
    
             // Unicode notification code (for NT)
             ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTW, 0, 0xFFFF, OnToolTipNotify)
          END_MESSAGE_MAP()
    
          // Notification handler for tooltips - determine which tooltip
          // string resource to be displayed.
          BOOL CCToolBarCtrl::OnToolTipNotify(
             UINT id, NMHDR * pNMHDR, LRESULT * pResult)
          {
             TOOLTIPTEXTA* pTTTA = (TOOLTIPTEXTA*) pNMHDR;
             TOOLTIPTEXTW* pTTTW = (TOOLTIPTEXTW*) pNMHDR;
    
             int strid = 0;
             switch (pNMHDR->idFrom)
             {
             case ID_BUTTON1:
                strid = IDS_BUTTON1;
                break;
    
             case ID_BUTTON2:
                strid = IDS_BUTTON2;
                break;
    
             case ID_BUTTON3:
                strid = IDS_BUTTON3;
                break;
             }
    
      if (strid)
           {
              *pResult = 0;
      
              CString str;
              str.LoadString(strid);
      
              #define _countof(array) (sizeof(array)/sizeof(array[0]))
      
              #ifndef _UNICODE
              if (pNMHDR->code == TTN_NEEDTEXTA)
              lstrcpyn(pTTTA->szText, str, _countof(pTTTA->szText));
              else
              _mbstowcsz(pTTTW->szText, str, _countof(pTTTW->szText));
              #else
              if (pNMHDR->code == TTN_NEEDTEXTA)
              _wcstombsz(pTTTA->szText, str, _countof(pTTTA->szText));
              else
              lstrcpyn(pTTTW->szText, str, _countof(pTTTW->szText));
              #endif
      
               return TRUE;
            }
    
             return FALSE;
          }
     
    					
(c) [Microsoft Corporation 1998 年,[保留所有的權限]。由 Yeong Kah Tam,Microsoft Corporation 的貢獻。

這篇文章中的資訊適用於:
  • Microsoft Foundation Class Library 4.2?應用於:
    • Microsoft Visual C++ 4.0 Standard Edition
    • Microsoft Visual C++ 4.1 Subscription
    • Microsoft Visual C++ 4.2 Enterprise Edition
    • Microsoft Visual C++ 4.2 Professional Edition
    • Microsoft Visual C++ 5.0 Enterprise Edition
    • Microsoft Visual C++ 5.0 Professional Edition
關鍵字:?
kbmt kbhowto kbmfcctrlbar kbtoolbar KB194294 KbMtzh
機器翻譯機器翻譯
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。
按一下這裡查看此文章的英文版本:194294? (http://support.microsoft.com/kb/194294/en-us/ )
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。
Retired KB Article依現狀不再更新的知識庫內容免責聲明
本文旨在說明 Microsoft 不再提供支援的產品。因此,本文係依「現狀」提供,不會再更新。