MFC.如何释放m_wndtoolbar.LoadToolBar()已加载的IDR_TOOLBAR1

ToujoursMoi 2015-04-16 10:55:11
BOOL CCutLineAIDC::OnInitDialog()
{
CDialog::OnInitDialog();

// TODO: Add extra initialization here

CRect rect;
GetClientRect(rect);

if (!m_hpglView.Create(rect, this))
return -1;

//CToolBar p_wndtoolbar = new CToolBar; //20150415
//*m_wndtoolbar = p_wndtoolbar; //20150415
//20150323rwt toolbar
if ((!m_wndtoolbar.CreateEx( this,TBSTYLE_FLAT , WS_CHILD | WS_VISIBLE | CBRS_ALIGN_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS,
CRect(4,4,0,0))) || (!m_wndtoolbar.LoadToolBar(IDR_TOOLBAR1)))
{
TRACE0("failed to create toolbar\n");
return false;
}

//* 20150415-rwt
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
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 claculated 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);
//*/


//m_wndtoolbar.MoveWindow(0,0, 100, 40,TRUE);//20150415-rwt
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}

m_wndtoolbar.LoadToolBar(IDR_TOOLBAR1) 第一次执行返回值是对的,第二次在执行的时候 返回值就为0了,工具条在打开的窗口中就显示不出来了,如果屏蔽到if()里的return,工具条 框架能显示出来,但是图标都没有了,求大神帮忙,不知道是不是没有释放掉加载的toolbar,如何释放,
...全文
297 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
ToujoursMoi 2015-04-17
  • 打赏
  • 举报
回复
引用 5 楼 zgl7903 的回复:
m_wndtoolbar.CreateEx 之前调用一次 m_wndtoolbar.DestroyWindow( ); 试试看
我把m_wndtoolbar.DestroyWindow( )放到 destroy的消息映射函数里了,还是不行, 调用m_wndtoolbar.DestroyWindow( )之前要像获得函数句柄一样,获得一些东西么?
ToujoursMoi 2015-04-17
  • 打赏
  • 举报
回复
引用 8 楼 VisualEleven 的回复:
你可以使用CToolBar::GetToolBarCtrl()得到CToolBarCtrl对象,然后利用CToolBarCtrl::AddButtons()/DeleteButton/HideButton()等来实现你的功能,没有必要重新Load.
请问CToolBarCtrl::AddButtons(),怎么使用,里面的参数不会写,我只有 工具栏的 ID,和工具栏上各个图标的ID
ToujoursMoi 2015-04-16
  • 打赏
  • 举报
回复
引用 2 楼 VisualEleven 的回复:
为什么会执行两次OnInitDialog函数???
按钮触发 打开新窗口,里面显示图形,工具条上加了一些处理图形的功能
ToujoursMoi 2015-04-16
  • 打赏
  • 举报
回复
按钮触发 打开新窗口,里面显示图形,工具条上加了一些处理图形的功能
Eleven 2015-04-16
  • 打赏
  • 举报
回复
为什么会执行两次OnInitDialog函数???
ToujoursMoi 2015-04-16
  • 打赏
  • 举报
回复
自己顶一下,小弟新手,求大神指导
ToujoursMoi 2015-04-16
  • 打赏
  • 举报
回复
引用 8 楼 VisualEleven 的回复:
你可以使用CToolBar::GetToolBarCtrl()得到CToolBarCtrl对象,然后利用CToolBarCtrl::AddButtons()/DeleteButton/HideButton()等来实现你的功能,没有必要重新Load.
关键还是释放 工具栏的问题吧? 用CToolBarCtrl的方法在dialog关闭之后需不需要手动删除这个button呢?不删除的话,第二次调用initdialog的话 还是会产生类似的问题吧?我先动手试一下。
Eleven 2015-04-16
  • 打赏
  • 举报
回复
你可以使用CToolBar::GetToolBarCtrl()得到CToolBarCtrl对象,然后利用CToolBarCtrl::AddButtons()/DeleteButton/HideButton()等来实现你的功能,没有必要重新Load.
ToujoursMoi 2015-04-16
  • 打赏
  • 举报
回复
没有释放toolbar的 函数么?
ToujoursMoi 2015-04-16
  • 打赏
  • 举报
回复
引用 5 楼 zgl7903 的回复:
m_wndtoolbar.CreateEx 之前调用一次 m_wndtoolbar.DestroyWindow( ); 试试看
我了解你的用意,试了下,但是效果不行啊,第二次调用还是不显示
zgl7903 2015-04-16
  • 打赏
  • 举报
回复
m_wndtoolbar.CreateEx 之前调用一次 m_wndtoolbar.DestroyWindow( ); 试试看
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CFrameWnd::OnCreate(lpCreateStruct) == -1) return -1; //this->Create(NULL,"元器件绘制系统",WS_MAXIMIZE); 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 } if (!m_wndToolBarDraw.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) || !m_wndToolBarDraw.LoadToolBar(IDR_DRAW_ICCHIP)) { TRACE0("Failed to create toolbar\n"); return -1; // fail to create } if (!m_wndStatusBar.Create(this) || !m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT))) { TRACE0("Failed to create status bar\n"); return -1; // fail to create } // TODO: Delete these three lines if you don't want the toolbar to // be dockable m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY); EnableDocking(CBRS_ALIGN_ANY); DockControlBar(&m_wndToolBar); m_wndToolBarDraw.EnableDocking(CBRS_ALIGN_ANY); DockControlBar(&m_wndToolBarDraw); this->FloatControlBar(&m_wndToolBarDraw,CPoint(30,120)); //m_wndToolBarDraw.SetSizes(CSize(25+7,25+6),CSize(25,25)); // CG: The following line was added by the Splash Screen component. // CSplashWnd::ShowSplashScreen(this); // CG: The following line was added by the Splash Screen component. CSplashWnd::ShowSplashScreen(this); return 0; }
// MainFrm.cpp : implementation of the CMainFrame class // #include "stdafx.h" #include "五子棋.h" #include "MainFrm.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif HCURSOR m_hcursor; ///////////////////////////////////////////////////////////////////////////// // CMainFrame IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd) BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) //{{AFX_MSG_MAP(CMainFrame) ON_WM_CREATE() ON_WM_SETCURSOR() //}}AFX_MSG_MAP END_MESSAGE_MAP() static UINT indicators[] = { ID_SEPARATOR, // status line indicator ID_INDICATOR_CAPS, ID_INDICATOR_NUM, ID_INDICATOR_SCRL, }; ///////////////////////////////////////////////////////////////////////////// // CMainFrame construction/destruction CMainFrame::CMainFrame() { // TODO: add member initialization code here } CMainFrame::~CMainFrame() { } int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CFrameWnd::OnCreate(lpCreateStruct) == -1) return -1; 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 } if (!m_wndStatusBar.Create(this) || !m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT))) { TRACE0("Failed to create status bar\n"); return -1; // fail to create } // TODO: Delete these three lines if you don't want the toolbar to // be dockable m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY); EnableDocking(CBRS_ALIGN_ANY); DockControlBar(&m_wndToolBar); return 0; } BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) { if( !CFrameWnd::PreCreateWindow(cs) ) return FALSE; // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return TRUE; } ///////////////////////////////////////////////////////////////////////////// // CMainFrame diagnostics #ifdef _DEBUG void CMainFrame::AssertValid() const { CFrameWnd::AssertValid(); } void CMainFrame::Dump(CDumpContext& dc) const { CFrameWnd::Dump(dc); } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CMainFrame message handlers BOOL CMainFrame::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) { // TODO: Add your message handler code here and/or call default BOOL br = CFrameWnd::OnSetCursor( pWnd, nHitTest,message) ; if(star==1) { if(flag==1) { m_hcursor= AfxGetApp()->LoadCursor(IDC_hei); SetCursor(m_hcursor); br = TRUE; } else if(flag==2) { m_hcursor= AfxGetApp()->LoadCursor(IDC_bai); SetCursor(m_hcursor); br = TRUE; } } return br; // return CFrameWnd::OnSetCursor(pWnd, nHitTest, message); }
// MainFrm.cpp : implementation of the CMainFrame class // #include "stdafx.h" #include "Snake.h" #include "MainFrm.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CMainFrame IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd) BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) //{{AFX_MSG_MAP(CMainFrame) ON_WM_CREATE() //}}AFX_MSG_MAP END_MESSAGE_MAP() static UINT indicators[] = { ID_SEPARATOR, // status line indicator ID_INDICATOR_CAPS, ID_INDICATOR_NUM, ID_INDICATOR_SCRL, }; ///////////////////////////////////////////////////////////////////////////// // CMainFrame construction/destruction CMainFrame::CMainFrame() { // TODO: add member initialization code here } CMainFrame::~CMainFrame() { } int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CFrameWnd::OnCreate(lpCreateStruct) == -1) return -1; 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 } if (!m_wndStatusBar.Create(this) || !m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT))) { TRACE0("Failed to create status bar\n"); return -1; // fail to create } // TODO: Delete these three lines if you don't want the toolbar to // be dockable m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY); EnableDocking(CBRS_ALIGN_ANY); DockControlBar(&m_wndToolBar); return 0; } BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) { if( !CFrameWnd::PreCreateWindow(cs) ) return FALSE; // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return TRUE; } ///////////////////////////////////////////////////////////////////////////// // CMainFrame diagnostics #ifdef _DEBUG void CMainFrame::AssertValid() const { CFrameWnd::AssertValid(); } void CMainFrame::Dump(CDumpContext& dc) const { CFrameWnd::Dump(dc); } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CMainFrame message handlers //DEL void CMainFrame::OnGameStart() //DEL { //DEL // TODO: Add your command handler code here //DEL IniGame(); //DEL m_nGameStatus = 1; //DEL SetTimer(1,100,NULL); //DEL Invalidate(); //DEL }
// MainFrm.cpp : implementation of the CMainFrame class // #include "stdafx.h" #include "Menu.h" #include "MainFrm.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CMainFrame IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd) BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) //{{AFX_MSG_MAP(CMainFrame) ON_WM_CREATE() ON_COMMAND(IDM_TEST, OnTest) ON_UPDATE_COMMAND_UI(ID_EDIT_CUT, OnUpdateEditCut) ON_UPDATE_COMMAND_UI(ID_FILE_NEW, OnUpdateFileNew) ON_COMMAND(IDM_SHOW, OnShow) //}}AFX_MSG_MAP END_MESSAGE_MAP() static UINT indicators[] = { ID_SEPARATOR, // status line indicator ID_INDICATOR_CAPS, ID_INDICATOR_NUM, ID_INDICATOR_SCRL, }; ///////////////////////////////////////////////////////////////////////////// // CMainFrame construction/destruction CMainFrame::CMainFrame() { // TODO: add member initialization code here m_bAutoMenuEnable = FALSE; } CMainFrame::~CMainFrame() { } int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CFrameWnd::OnCreate(lpCreateStruct) == -1) return -1; 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 } if (!m_wndStatusBar.Create(this) || !m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT))) { TRACE0("Failed to create status bar\n"); return -1; // fail to create } // TODO: Delete these three lines if you don't want the toolbar to // be dockable m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY); EnableDocking(CBRS_ALIGN_ANY); DockControlBar(&m_wndToolBar); /* GetMenu()->GetSubMenu(0)->CheckMenuItem(0, MF_BYPOSITION | MF_CHECKED); GetMenu()->GetSubMenu(0)->CheckMenuItem(ID_FILE_NEW, MF_BYCOMMAND | MF_CHECKED); GetMenu()->GetSubMenu(0)->SetDefaultItem(1,TRUE); GetMenu()->GetSubMenu(0)->SetDefaultItem(ID_FILE_OPEN,FALSE); GetMenu()->GetSubMenu(0)->SetDefaultItem(5,TRUE); CString str; str.Format("x=%d, y=%d",GetSystemMetrics(SM_CXMENUCHECK),GetSystemMetrics(SM_CYMENUCHECK)); MessageBox(str); m_bitmap.LoadBitmap(IDB_BITMAP1); GetMenu()->GetSubMenu(0)->SetMenuItemBitmaps(0,MF_BYPOSITION, &m_bitmap, &m_bitmap); */ GetMenu()->GetSubMenu(0)->EnableMenuItem(1, MF_BYPOSITION | MF_DISABLED); SetMenu(NULL); CMenu menu; menu.LoadMenu(IDR_MAINFRAME); SetMenu(&menu); menu.Detach(); return 0; } BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) { if( !CFrameWnd::PreCreateWindow(cs) ) return FALSE; // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return TRUE; } ///////////////////////////////////////////////////////////////////////////// // CMainFrame diagnostics #ifdef _DEBUG void CMainFrame::AssertValid() const { CFrameWnd::AssertValid(); } void CMainFrame::Dump(CDumpContext& dc) const { CFrameWnd::Dump(dc); } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CMainFrame message handlers void CMainFrame::OnTest() { // TODO: Add your command handler code here MessageBox("MainFrame Clicked"); } void CMainFrame::OnUpdateEditCut(CCmdUI* pCmdUI) { // TODO: Add your command update UI handler code here if(2 == pCmdUI->m_nIndex) pCmdUI->Enable(); } void CMainFrame::OnUpdateFileNew(CCmdUI* pCmdUI) { // TODO: Add your command update UI handler code here // if(ID_FILE_NEW == pCmdUI->m_nID); /* if(0 == pCmdUI->m_nIndex) pCmdUI->Enable(FALSE); */ } void CMainFrame::OnShow() { // TODO: Add your command handler code here MessageBox("Main show"); }

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧