Blood-HaZaRd
Blood-HaZaRd

Reputation: 2124

Add a toolbar to a CDialog Window

I created a SDI application and I created a Dialog Window. I want to add a toolbar to that dialog, so I coded like this :

int CAddNewEmployee::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CDialog::OnCreate(lpCreateStruct) == -1)
      return -1;

   CToolBar cToolBar;
   cToolBar.Create(this);
   cToolBar.LoadToolBar(IDR_MAINFRAME1);
   cToolBar.ShowWindow(SW_SHOW);
   cToolBar.SetBarStyle(CBRS_ALIGN_TOP | CBRS_TOOLTIPS | CBRS_FLYBY);
   RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);

   return 0;

}

but when I run the application nothing happen, there is no toolbar shown. I don't know if the code is right and how to make it shown.

Thank you.

Upvotes: 4

Views: 1894

Answers (1)

Sheng Jiang 蒋晟
Sheng Jiang 蒋晟

Reputation: 15261

Your toolbar is destroyed at the end of the function as you made it a local variable. Make it a member variable instead.

Reference: How to add control bars to dialog boxes in MFC

Upvotes: 3

Related Questions