我用网上流传的那种方法增加工具栏
if(!m_wndToolBar.Create(this) || !m_wndToolBar.LoadToolBar(IDR_TOOLBAR))
{
TRACE0("Failed to Create Dialog Toolbar\n");
EndDialog(IDCANCEL);
}
当我将对话框类设置成指针的时候,发现第一次打开对话框可以显示工具栏,以后打开对话框的时候LoadToolBar()这个函数返回值为false,请问这是为什么呢?
LoadToolBar这个函数有什么讲究吗?

解决方案 »

  1.   

    呵呵,这个很简单的
    if(!m_wndToolBar.Create(this)  ¦ ¦ !m_wndToolBar.LoadToolBar(IDR_TOOLBAR))
    {
    TRACE0("Failed to Create Dialog Toolbar\n");
    EndDialog(IDCANCEL);
    }  //butD = TRUE;
    CRect rcClientOld; 
    // Old Client Rect
    CRect rcClientNew; 
    // New Client Rect with Tollbar Added
    GetClientRect(rcClientOld); 
    // Retrive the Old Client WindowSize
    // Called to reposition and resize control bars in the client area of a window
    // The reposQuery FLAG does not really traw the Toolbar.  It only does the calculations.
    // And puts the new ClientRect values in rcClientNew so we can do the rest of the Math.
    RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0,reposQuery,rcClientNew); // All of the Child Windows (Controls) now need to be moved so the Tollbar does not cover them up.
    // Offest to move all child controls after adding Tollbar
    CPoint ptOffset(rcClientNew.left-rcClientOld.left,
    rcClientNew.top-rcClientOld.top); CRect rcChild;
    CWnd* pwndChild = GetWindow(GW_CHILD);  //Handle to the Dialog Controls
    while(pwndChild) // Cycle through all child controls
    {
    pwndChild->GetWindowRect(rcChild); //  Get the child control RECT
    ScreenToClient(rcChild); 
    rcChild.OffsetRect(ptOffset); // Changes the Child Rect by the values of the calculated offset
    pwndChild->MoveWindow(rcChild,FALSE); // Move the Child Control
    pwndChild = pwndChild->GetNextWindow();
    } CRect rcWindow;
    GetWindowRect(rcWindow); // Get the RECT of the Dialog
    rcWindow.right += rcClientOld.Width() - rcClientNew.Width(); // Increase width to new Client Width
    rcWindow.bottom += rcClientOld.Height() - rcClientNew.Height(); // Increase height to new Client Height
    MoveWindow(rcWindow,FALSE); // Redraw Window
    // Now we REALLY Redraw the Toolbar
    RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);
    }
      

  2.   

    //////////////////////////////////////////////////////////////////////////
    //显示ToolBar
    if   (!m_btnToolBar.CreateEx(this, 
    TBSTYLE_FLAT,
    WS_CHILD | WS_VISIBLE | CBRS_ALIGN_TOP | CBRS_GRIPPER |CBRS_TOOLTIPS)
    || !m_btnToolBar.LoadToolBar(IDR_TOOLBAR_Server)   
    )  
    {  
    return   FALSE;  
    }  
    m_btnToolBar.ShowWindow(SW_SHOW);  
    RepositionBars(AFX_IDW_CONTROLBAR_FIRST,   AFX_IDW_CONTROLBAR_LAST,   0); 
    这是我用的ToolBar的代码 没有出现问题.
      

  3.   

    你没有回答一楼的问题你这段代码是添加到对话框的OnInitDialog()函数中么?
      

  4.   

    你用 GetLastError()取回错误码看一下是什么问题就可以