简体   繁体   English

MFC 窗口工具栏创建失败,没有错误消息

[英]MFC Window toolbar creation fails with no error message

I am trying to dynamically create a toolbar for my MFC application.我正在尝试为我的 MFC 应用程序动态创建一个工具栏。 I have to following approach for this task.我必须遵循此任务的方法。 However, the toolbar doesn't show up in my window and I get no error message...但是,工具栏没有显示在我的窗口中,我也没有收到错误消息...

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) {
  // ...
  CToolBar menubar;
  if(!menubar.CreateEx(this, TBSTYLE_FLAT | TBSTYLE_LIST, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_FLYBY | CBRS_SIZE_DYNAMIC)) {
    return -1;
  }

  TBBUTTON buttons[5] = {
    { -1, ID_TOOLBAR_FILE, TBSTATE_ENABLED, BTNS_AUTOSIZE | BTNS_DROPDOWN, { 0 }, 0, (INT_PTR)L"File" },
    { -1, ID_TOOLBAR_EDIT, TBSTATE_ENABLED, BTNS_AUTOSIZE | BTNS_DROPDOWN, { 0 }, 0, (INT_PTR)L"Edit" },
    { -1, ID_TOOLBAR_VIEW, TBSTATE_ENABLED, BTNS_AUTOSIZE | BTNS_DROPDOWN, { 0 }, 0, (INT_PTR)L"View" },
    { -1, ID_TOOLBAR_LAYOUT, TBSTATE_ENABLED, BTNS_AUTOSIZE | BTNS_DROPDOWN, { 0 }, 0, (INT_PTR)L"Layout" },
    { -1, ID_TOOLBAR_HELP, TBSTATE_ENABLED, BTNS_AUTOSIZE | BTNS_DROPDOWN, { 0 }, 0, (INT_PTR)L"Help" }
  };


  ::SendMessage(menubar.GetSafeHwnd(), TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
  ::SendMessage(menubar.GetSafeHwnd(), TB_ADDBUTTONS, (WPARAM)5, (LPARAM)&buttons);

  ::SendMessage(menubar.GetSafeHwnd(), TB_AUTOSIZE, 0, 0);
  ::ShowWindow(menubar.GetSafeHwnd(), SW_SHOW);
}

This will not display my toolbar.这不会显示我的工具栏。 Why is this happening?为什么会这样?

I found out that declaring the toolbar as class member solves the problem.我发现将工具栏声明为类成员可以解决问题。 So here is what I did:所以这就是我所做的:

class CMainFrame : public CFramWnd {
protected:
  CToolBar m_wndToolBar;
  // ...
}

And the rest of the code is practically unchanged.其余的代码几乎没有变化。 I am still not sure why it works this way.我仍然不确定为什么它会这样工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM