我是菜鸟,刚学vc,用mfc下的向导编一个基于单文档的简单程序,我又添加一个工具栏,
在cmainframe中添加变量vm_toolbar1,在mainfrm.cpp中的cmainframe类的oncreate中
写:if(!m_toolbar1.crete(this)||!m_toolbar1.loadtoolbar(idr_toolbar1))
{trace0("failed to create toolbar\n");
return -1;}编译,程序可以运行,能看见我添加的工具栏,我删除原mfc自动产生的工具栏,
在编译,却说:建立空文档失败!程序的没有运行!请帮我这个菜鸟一下,我太菜(请写的详细些!)

解决方案 »

  1.   

    你如果是想将你的工具条作为主工具条的话,将你的工具条的ID改名为IDR_MAINFRAME就可以了。因为系统认识的主工具条的ID是IDR_MAINFRAME
      

  2.   

    谢谢!顺利运行!
    可我是看人民邮电的一本vc书,只教在基于单文档的简单程序的工具栏,
    我试着作在基于对话框的简单程序中添加工具栏,ID改名为IDR_MAINFRAME,并定义m_wndToolBar
    为CToolBar,在cmytoolDlg中m_wndToolBar.CreateEx(this,WS_CHILD||CBRS_TOP,OX9100);
    编译说有一个错误. 不知能否指条光明道路?
      

  3.   

    呵呵,对话框中定义工具条就不是这么简单的了。给你个例子吧。
    //初始化工具条
    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
    }
        // Create the toolbar. To understand the meaning of the styles used, you 
      
    CRect rcClientStart;
    CRect rcClientNow;
    GetClientRect(rcClientStart); // To reposition and resize the control bar
    RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST,0, reposQuery, rcClientNow);
    CPoint ptOffset(rcClientNow.left - rcClientStart.left,rcClientNow.top-rcClientStart.top); CRect rcChild;
    CWnd* pwndChild = GetWindow(GW_CHILD); while (pwndChild)
    {
    pwndChild->GetWindowRect(rcChild);
    ScreenToClient(rcChild);
    rcChild.OffsetRect(ptOffset);
    pwndChild->MoveWindow(rcChild, FALSE);
    pwndChild = pwndChild->GetNextWindow();
    } CRect rcWindow;
    GetWindowRect(rcWindow);
    rcWindow.right += rcClientStart.Width() - rcClientNow.Width();
    rcWindow.bottom += rcClientStart.Height() - rcClientNow.Height();
    MoveWindow(rcWindow, FALSE);         // And position the control bars
    RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);
    由于对话框中的工具条要占对话框的空间,所以中间那段程序是调整对话框中控件的位置的。
      

  4.   

    happyparrot先生:
       很不好意思,打扰了,我是想工具栏是后添加的,单击其,用类向导,建立到已有的mytoolbarDlg上,然后,在object ids 中选mytoolbarDlg,在message中选Create建立函数,在这函数里,
    把先生的代码复制进去,编译,无错,但就是没有显示工具栏.
      请有空时赐教.谢谢!
      

  5.   

    我是在win98下,试验,不成功,我今天,在win2000里,成功!
    今天,在win2000里,才仔细看先生的代码,(菜鸟光顾复制粘贴,)在win98里,
    "pwndChild->GetWindowRect(rcChild);"的指针,我粘贴时电脑出了错.
    谢谢!祝你快乐!!