请教如何在工具栏中加文本编辑框?

解决方案 »

  1.   

    在CMainFrame中添加一个CEdit变量:
    CEdit m_Edit;
    在CMainFrame::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("未能创建工具栏\n");
    return -1;      // 未能创建
    }
    后面添加:
    CToolBarCtrl &pTool = m_wndToolBar.GetToolBarCtrl();
    TBBUTTON tButton;
    ZeroMemory(&tButton, sizeof(TBBUTTON));
    tButton.fsStyle = TBSTYLE_SEP;
    tButton.idCommand = IDC_EDITBOX;
    pTool.InsertButton(pTool.CommandToIndex(ID_FILE_PRINT), &tButton);
    CRect rcButton;
    pTool.GetItemRect(pTool.CommandToIndex(IDC_EDITBOX), rcButton);
    int nWidth = 200;
    for (int i = 0; i < nWidth / rcButton.Width(); i++)
    {
    pTool.InsertButton(pTool.CommandToIndex(ID_FILE_PRINT), &tButton);
    }
    pTool.InsertButton(pTool.CommandToIndex(ID_FILE_PRINT), &tButton);
    rcButton.right += nWidth;
    m_Edit.CreateEx(WS_EX_CLIENTEDGE, "EDIT", "Editbox", WS_VISIBLE | WS_CHILD, rcButton, &pTool, IDC_EDITBOX);
    这里:IDC_EDITBOX是你的Edit Box的控件ID,可以在资源编辑器里添加;nWidth是要添加的编辑框的宽度;ID_FILE_PRINT是要添加的编辑框的位置后面的按钮ID。
      

  2.   

    再说一下这样添加的编辑框消息的处理:
    BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
    {
    // TODO: 在此添加专用代码和/或调用基类
    HWND hEdit = m_wndToolBar.GetDlgItem(IDC_EDITBOX)->GetSafeHwnd();
    if (pMsg->hwnd == hEdit)
    {
    if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN)
    {
    CString strEdit;
    m_wndToolBar.GetDlgItem(IDC_EDITBOX)->GetWindowText(strEdit);
    AfxMessageBox(strEdit);//这里得到编辑框的内容随便怎么搞了。
    }
    } return CFrameWnd::PreTranslateMessage(pMsg);
    }