如何向 ActiveX 控件添加工具栏和工具提示

概要

<script type="text/javascript">loadTOCNode(1, 'summary');</script>
作为其子窗口的 ActiveX 控件可有工具栏 (CToolBar 类)。 本文说明如何创建这样一个工具栏和也如何实现工具提示有关按钮工具栏窗口上。

更多信息

<script type="text/javascript">loadTOCNode(1, 'moreinformation');</script>
VisualC++ 提供两种方法来创建工具栏。 文章 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.添加到 ActiveX 控件派生类 TTN_NEEDTEXTW (对于 Unicode 通知代码) 或 TTN_NEEDTEXTA (对于 ANSI 通知代码) 通知处理程序。 加载此通知代码处理中要显示工具提示字符串:
      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;
      }
 
					
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值