C中的工具条上如何加入下拉菜单按钮??并如何映射消息使下拉菜单可用???

解决方案 »

  1.   

    利用MFC编程在工具条中增加组合框控件
    武汉华中理工大学 
    刘卫忠  
            Windows UI技术一直是程序设计人员尤其是用户接口程序设计人员最关心的技术。显然新颖美观的界面会给用户留下深刻的印象。Windows操作系统的流行也在于其用户界面的不断创新与改进。Office 97、Visual Studio 97、Outlook、IE等每一个新的软件的推出都能够给人新的感觉。尤其是这些软件工具条十分丰富美观,如3D形式的工具条、飞行Tips,这些小技巧既富于吸引力,又方便了使用者。我们知道,Visual C++所提供的标准工具条中基本上只有一些简单的诸如文档打开、存储以及一些编辑功能按钮,而Visual C++自身的工具条功能十分丰富,尤其是工具条中内嵌的组合框控件十分方便。实际上利用MFC编程在工具条中增加一些有趣的控件还是比较容易的,下面我们就谈谈如何利用MFC编程在工具条中增加组合框控件。
    1. 创建AppWizard工程文件设为toolbar,采用系统缺省设置。 2. 打开资源编辑器,选择工具条,在工具条中增加一个按钮,设置按钮 I D 为 I D_MY_COMBO_BOX 。 移动该按钮以使按钮处于适当的位置。 3. 从 C ToolBar 派 生出一个新类 C MyToolBar , 在 C MyToolBar 类 中增加一个成员变量 m_wndMyComboBox , 代码如下:  class CMyToolBar : public CToolBar
    {
    public:
        CComboBox m_wndMyComboBox;
    };  注意要将代码放在工程文件mainframe.h中“#endif // _MSC_VER > 1000”语句之后,类CMainFrame定义之前。 4. 将工程文件mainframe.h中的CToolBar变量m_wndToolBar用CMyToolBar变量m_wndToolBar代替。同时增加函数CreateMyComboToolBar()声明。代码如下:  protected: // control bar embedded members
         CStatusBar m_wndStatusBar;
    CMyToolBar m_wndToolBar;
    int CreateMyComboToolBar();5. 在工程文件mainframe.cpp中的OnCreate()函数返回语句之前调用函数CreateMyToolBar();代码如下所示:   if(!CreateMyComboToolBar())
        MessageBox("Create My Combo ToolBar Failure");6. 以文本方式打开资源头文件resource.h,增加一个控制ID如下,注意该控制的ID值不要与其它控制或资源的ID值重复:       #define  IDC_MY_COMBO 1111 7. 在工程文件mainframe.cpp中增加函数CreateMyToolBar();代码如下: int MainFrame::CreateMyToolBar()
    {
        #define COMBO_BOX_WIDTH 80 //the width of the combo box
        //set up the ComboBox control as a select box
    //First get the index of the button's
     position in the toolbar
    int index = 0;
    CRecr rect;
    while(m_wndToolBar.GetItemID(index)!
    = ID_MY_COMBO_BOX) index++;
    //next convert that button to a 
    seperator and get its position
    m_wndToolBar.SetButtonInfo(index,
     ID_MY_COMBO_BOX, TBBS_SEPARATOR, COMBO_BOX_WIDTH);
        m_wndToolBar.GetItemRect(index, &rect);
    //expand the rectangle to allow the 
    combo box room to drop down
        rect.top+=2;
        rect.bottom += 200;
        // then .Create the combo box and show it
        if (!m_wndToolBar.m_ wndMyComboBox.Create(
            WS_CHILD|WS_VISIBLE | CBS_AUTOHSCROLL | 
            CBS_DROPDOWNLIST | CBS_HASSTRINGS ,
            rect, &m_wndToolBar, IDC_MY_COMBO))
        {
            TRACE0("Failed to create combo-box\n");
            return FALSE;
        }
        m_wndToolBar.m_wndMyComboBox.ShowWindow(SW_SHOW);
        //adding string to the combo box
        m_wndToolBar.m_wndMyComboBox.AddString("Fisrt Select");
        m_wndToolBar.m_wndMyComboBox.AddString("Second Select");
        m_wndToolBar.m_wndMyComboBox.AddString("Third Select");
        m_wndToolBar.m_wndMyComboBox.AddString("Fourth Select");
        m_wndToolBar.m_wndMyComboBox.AddString("Fifth Select");
        m_wndToolBar.m_wndMyComboBox.SetCurSel(0);
    }编译并运行该应用,就会发现工具条中多了一个组合框控件。 从上面的例程可以看出,在工具条中增加新的控件,关键在于重写主框架的OnCreate()函数。掌握了这一点,就可以在工具条中增加各种各样的控件了,如增加无限输入(Radio)控件、检查框(Check)控件等。有兴趣的读者可以自己去尝试一下加深理解。 
     
      

  2.   

    我知道如何添加象IE的前进、后退按钮的方法,我的问题是如何获得工具栏上的各个按钮的坐标,只要得到的特定按钮的坐标,就可以显示一个下拉菜单了!我使用了相关的CToolBar的相关函数,还是不能队获得按钮的坐标尽心很好的转换,使得我的下拉菜单显示在错误的位置上!
    咱们可以合作!
    在CMainFrame::OnCreate():
    ...
        m_wndToolBar.GetToolBarCtrl().SetExtendedStyle(TBSTYLE_EX_DRAWDDARROWS);
    i=m_wndToolBar.CommandToIndex(ID_FILE_NEW);
    UINT id,style;
    int image;
    m_wndToolBar.GetButtonInfo(i,id,style,image);
    m_wndToolBar.SetButtonInfo(i,ID_FILE_NEW,TBSTYLE_BUTTON | TBSTYLE_DROPDOWN,image); 
    ...
    相应消息映射:
    afx_msg void OnDropDown(NMHDR* pNotifyStruct,LRESULT* result);
    。。
    ON_NOTIFY(TBN_DROPDOWN,AFX_IDW_TOOLBAR,OnDropDown)
    void CMainFrame::OnDropDown(NMHDR* pNotifyStruct,LRESULT* result)
    {
    NMTOOLBAR* pInfo = (NMTOOLBAR*)pNotifyStruct;
    switch(pInfo->iItem)
        {
        case ID_FILE_NEW:
      CRect rect;
      m_wndStatusBar.GetItemRect(0,&rect);
      CString str;
      str.Format("The size of the item File new Button is :(%d,%d)",rect.Width(),rect.Height());
      MessageBox(str);
      
        break;    // TODO: Add cases for other drop-down buttons
        }
    }
      

  3.   

    TO:楼主
    问题解决了!
    他奶奶的!
    原来获得按钮的坐标的方法这样简单!只需要用函数 GetCursorPos(&pt);就行了
    --------------------
    如果需要,可以将我的工程发给你!
    南京 宋业文
      

  4.   

    to:楼主:
    你必须同时设置两个消息映射,一个是原先的按钮的消息映射,(必须!)
    另一个就是响应消息:
    ON_NOTIFY(TBN_DROPDOWN,AFX_IDW_TOOLBAR,OnDropDown)
    如果你的工具栏是自定义的,必须设置自己的工具栏的ID以代替上面的默认的ID:AFX_IDW_TOOLBAR
    ----------------------
    将你的信箱留下,发个工程给你!
      

  5.   

    你的工程还在吗?我也想要
    [email protected]