Make a Customized ToolBar Dialog Work in MFC (VC6)

本文介绍如何通过处理TBN_QUERYINSERT、TBN_QUERYDELETE和TBN_GETBUTTONINFO通知消息来实现工具栏按钮的定制功能。文章详细展示了如何在MFC应用程序中添加相应处理函数,以实现工具栏按钮的增删及信息获取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Am I the only fool in this world? I searched the Web, posted questions on newsgroups, but found nothing and nobody answered me. It seems everybody knows how to do this except me. The customize toolbar dialogue either goes by with a flash or I could not add buttons back after they were deleted.

MSDN said you must answer several Notify messages. They are TBN_QUERYINSERT, TBN_QUERYDELETE, and TBN_GETBUTTONINFO, but there is no code showing how to do it.

I finally found the way after having a detailed look at the structure of CToolBarEx. It's simple but I don't know why nobody posts it.

Here is the way to accomplish the task:

  1. Let the wizard make a standard MFC .exe, create a menu—let's call it "Customize ToolBar"—and use the wizard to create a hndle in CFrameWindow. In the handle, write:
    CToolBarCtrl& myTBCtrl = m_wndToolBar.GetToolBarCtrl();
    myTBCtrl.Customize();
    

    Compile and link, run the .exe, and then click the "Customize ToolBar" menu option; this makes the Customize dialog flash when you click the "Customize ToolBar" menu option. That's because your frame window has no answering handler to the message of TBN_QUERYINSERT.

  2. Add a handler to TBN_QUERYINSERT in the frame windows. In the Mainfrm.h file, add

    afx_msg void QueryInsert(NMHDR * pNotifyStruct,
                             LRESULT * result);
    

    In the Mainfrm.cpp file's message map, add

    ON_NOTIFY(TBN_QUERYINSERT,AFX_IDW_TOOLBAR,QueryInsert)

    Also, in the Mainfrm.cpp file, add:

    void CMainFrame::QueryInsert(NMHDR * pNotifyStruct,
                                 LRESULT * result )
    {
       *result = TRUE;
    }
    

    Now, because the frame windows will answer the TBN_QUERYINSERT message, the Customize ToolBar dialog knows you are allowing the ToolBar change. You see that, when you click the "Customize ToolBar" menu, the dialog appears, but you cannot delete any button. Neither can you add any button except the separator. Why? Because there is no handler for TBN_QUERYDELETE in the frame window.

  3. Add a handler to TBN_QUERYDELETE in the frame window; that's the same code as adding TBN_QUERYINSERT. That's shown here:
    afx_msg void QueryDelete(NMHDR * pNotifyStruct,
                             LRESULT * result);
    ON_NOTIFY(TBN_QUERYDELETE,AFX_IDW_TOOLBAR,QueryDelete)
    void CMainFrame::QueryDelete(NMHDR * pNotifyStruct,
                                 LRESULT * result )
    {
       *result = TRUE;
    }
    

    Now, you see you could 'Remove' (delete) the button, but after that you could never get it back, even when you click 'Add'. Why? Actually, before the button adds back, the dialog sends another message—TBN_GETBUTTONINFO—to the frame window to search the button info you choose. If there is no info reply, there is no way to add back the button. So, we need the third handler for TBN_GETBUTTONINFO.

  4. How we get it to answer TBN_GETBUTTONINFO? We need collect all the toolbar button info before the dialog appears. Okay, let's do it after the frame windows create the toolbar.

    We add a private function in the frame window, and add two members into frame windows to save the info and the count of how many buttons you have in your CToolbar.

    In the frame windows Mainfrm.h file, add the function:

    private:
    void GetToolbarButtonInfo();
    

    Also, add the members:

    TBBUTTON m_TBinfo[256];    // suppose you have a toolbar
                               // button maxed to 256
    int      m_iTBCount;
    

    In the Mainfrm.cpp file, add:

    void CMainFrame::GetToolbarButtonInfo()
    {
       CToolBarCtrl& myTBCtrl = m_wndToolBar.GetToolBarCtrl();
    
       //get how many toolbar buttons you have when the frame loads
       m_iTBCount = myTBCtrl.GetButtonCount();
    
       //save loaded toolbar button info into a m_TBinfo array
       for (int i=0; i<=m_iTBCount; i++)
       {
          myTBCtrl.GetButton(i,&m_TBinfo[i]);
       }
    
    }
    

    We call GetToolbarButtonInfo() right after the toolbar was created in the CFrameWnd:: OnCreate(...).

    if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD |
                               WS_VISIBLE | CBRS_TOP |
                               CBRS_GRIPPER | CBRS_TOOLTIPS |
                               CBRS_FLYBY |
                               CBRS_SIZE_DYNAMIC) ||
       !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
    {
       TRACE0("Failed to create toolbar\n");
       return -1;    // fail to create
    }
    
    GetToolbarButtonInfo();
    

    (You see, we did not add a parameter of CCS_ADJUSTABLE, but the toolbar is still adjustable.)

  5. Now, we have the Toolbar information to answer the TBN_GETBUTTONINFO. Let's add the handler into the frame window:

    In Mainfrm.h, add:

    afx_msg void QueryInfo(NMHDR * pNotifyStruct,
                           LRESULT * result);
    

    In Mainfrm.cpp, add:

    ON_NOTIFY(TBN_GETBUTTONINFO,AFX_IDW_TOOLBAR,QueryInfo)
    void CMainFrame::QueryInfo(NMHDR * pNotifyStruct,
                               LRESULT * result )
    {
       TBNOTIFY* pTBntf = (TBNOTIFY *)pNotifyStruct;
    
       if((pTBntf->iItem>=0) && (pTBntf->iItem <= m_iTBCount))
       {
          pTBntf->tbButton = m_TBinfo[pTBntf->iItem];
          *result = TRUE;
       }
    
       else
       {
          *result = FALSE;
       }
    }
    

Compile, link, and run it. Does it work now?

转载于:https://www.cnblogs.com/henryzc/archive/2006/06/01/415257.html

内容概要:该PPT详细介绍了企业架构设计的方法论,涵盖业务架构、数据架构、应用架构和技术架构四大核心模块。首先分析了企业架构现状,包括业务、数据、应用和技术四大架构的内容和关系,明确了企业架构设计的重要性。接着,阐述了新版企业架构总体框架(CSG-EAF 2.0)的形成过程,强调其融合了传统架构设计(TOGAF)和领域驱动设计(DDD)的优势,以适应数字化转型需求。业务架构部分通过梳理企业级和专业级价值流,细化业务能力、流程和对象,确保业务战略的有效落地。数据架构部分则遵循五大原则,确保数据的准确、一致和高效使用。应用架构方面,提出了分层解耦和服务化的设计原则,以提高灵活性和响应速度。最后,技术架构部分围绕技术框架、组件、平台和部署节点进行了详细设计,确保技术架构的稳定性和扩展性。 适合人群:适用于具有一定企业架构设计经验的IT架构师、项目经理和业务分析师,特别是那些希望深入了解如何将企业架构设计与数字化转型相结合的专业人士。 使用场景及目标:①帮助企业和组织梳理业务流程,优化业务能力,实现战略目标;②指导数据管理和应用开发,确保数据的一致性和应用的高效性;③为技术选型和系统部署提供科学依据,确保技术架构的稳定性和扩展性。 阅读建议:此资源内容详尽,涵盖企业架构设计的各个方面。建议读者在学习过程中,结合实际案例进行理解和实践,重点关注各架构模块之间的关联和协同,以便更好地应用于实际工作中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值