MFC中没有提供供对话框使用的工具条类,而我们时常需要开发以对话框为框架的程序。下面我使用简单的代码说明这种方法。 step1: 在资源编辑器中插入工具条资源,并为每个按钮创建ID。将它命名为IDC_TOOLBAR1 step2: 在对话框变量中添加一个工具条变量。 CToolBar m_wndToolBar;step3: 在CDialog::OnInitDialog中添加如下代码: // 创建工具条并调入资源 if(!m_wndToolBar.Create(this) || !m_wndToolBar.LoadToolBar(IDR_TOOLBAR1)) { TRACE0("Failed to Create Dialog Toolbar\n"); EndDialog(IDCANCEL); }CRect rcClientOld; // 久客户区RECT CRect rcClientNew; // 加入TOOLBAR后的CLIENT RECT GetClientRect(rcClientOld); // // 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. //重新计算RECT大小 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. //所有的子窗口将被移动,以免被TOOLBAR覆盖 // 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); //得到子窗口 while(pwndChild) // 处理所有子窗口 {//移动所有子窗口 pwndChild->GetWindowRect(rcChild); ScreenToClient(rcChild); rcChild.OffsetRect(ptOffset); pwndChild->MoveWindow(rcChild,FALSE); pwndChild = pwndChild->GetNextWindow(); }CRect rcWindow; GetWindowRect(rcWindow); // 得到对话框RECT rcWindow.right += rcClientOld.Width() - rcClientNew.Width(); // 修改对话框尺寸 rcWindow.bottom += rcClientOld.Height() - rcClientNew.Height(); MoveWindow(rcWindow,FALSE); // Redraw WindowRepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);
用CToolBarCtrl,动态Create,自己定义大小、图标,和消息ID。
本文转自www.vccode.com简要说明: MFC中没有提供供对话框使用的工具条类,而我们时常需要开发以对话框为框架的程序。下面我使用简单的代码说明这种方法。 step1: 在资源编辑器中插入工具条资源,并为每个按钮创建ID。将它命名为IDC_TOOLBAR1 step2: 在对话框变量中添加一个工具条变量。 CToolBar m_wndToolBar; step3: 在CDialog::OnInitDialog中添加如下代码: // 创建工具条并调入资源 if(!m_wndToolBar.Create(this) || !m_wndToolBar.LoadToolBar(IDR_TOOLBAR1)) { TRACE0("Failed to Create Dialog Toolbar\n"); EndDialog(IDCANCEL); }CRect rcClientOld; // 久客户区RECT CRect rcClientNew; // 加入TOOLBAR后的CLIENT RECT GetClientRect(rcClientOld); // // 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. //重新计算RECT大小 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. //所有的子窗口将被移动,以免被TOOLBAR覆盖 // 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); //得到子窗口 while(pwndChild) // 处理所有子窗口 {//移动所有子窗口 pwndChild->GetWindowRect(rcChild); ScreenToClient(rcChild); rcChild.OffsetRect(ptOffset); pwndChild->MoveWindow(rcChild,FALSE); pwndChild = pwndChild->GetNextWindow(); }CRect rcWindow; GetWindowRect(rcWindow); // 得到对话框RECT rcWindow.right += rcClientOld.Width() - rcClientNew.Width(); // 修改对话框尺寸 rcWindow.bottom += rcClientOld.Height() - rcClientNew.Height(); MoveWindow(rcWindow,FALSE); // Redraw WindowRepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);
不用那么烦! 做一个菜单在对话框初始化中加入如下代码 CMenu menu; menu.LoadMenu(ID); OK
step1:
在资源编辑器中插入工具条资源,并为每个按钮创建ID。将它命名为IDC_TOOLBAR1 step2:
在对话框变量中添加一个工具条变量。
CToolBar m_wndToolBar;step3:
在CDialog::OnInitDialog中添加如下代码:
// 创建工具条并调入资源
if(!m_wndToolBar.Create(this) || !m_wndToolBar.LoadToolBar(IDR_TOOLBAR1))
{
TRACE0("Failed to Create Dialog Toolbar\n");
EndDialog(IDCANCEL);
}CRect rcClientOld; // 久客户区RECT
CRect rcClientNew; // 加入TOOLBAR后的CLIENT RECT
GetClientRect(rcClientOld); //
// 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.
//重新计算RECT大小
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.
//所有的子窗口将被移动,以免被TOOLBAR覆盖
// 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); //得到子窗口
while(pwndChild) // 处理所有子窗口
{//移动所有子窗口
pwndChild->GetWindowRect(rcChild);
ScreenToClient(rcChild);
rcChild.OffsetRect(ptOffset);
pwndChild->MoveWindow(rcChild,FALSE);
pwndChild = pwndChild->GetNextWindow();
}CRect rcWindow;
GetWindowRect(rcWindow); // 得到对话框RECT
rcWindow.right += rcClientOld.Width() - rcClientNew.Width(); // 修改对话框尺寸
rcWindow.bottom += rcClientOld.Height() - rcClientNew.Height();
MoveWindow(rcWindow,FALSE); // Redraw WindowRepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);
MFC中没有提供供对话框使用的工具条类,而我们时常需要开发以对话框为框架的程序。下面我使用简单的代码说明这种方法。 step1:
在资源编辑器中插入工具条资源,并为每个按钮创建ID。将它命名为IDC_TOOLBAR1 step2:
在对话框变量中添加一个工具条变量。
CToolBar m_wndToolBar; step3:
在CDialog::OnInitDialog中添加如下代码:
// 创建工具条并调入资源
if(!m_wndToolBar.Create(this) || !m_wndToolBar.LoadToolBar(IDR_TOOLBAR1))
{
TRACE0("Failed to Create Dialog Toolbar\n");
EndDialog(IDCANCEL);
}CRect rcClientOld; // 久客户区RECT
CRect rcClientNew; // 加入TOOLBAR后的CLIENT RECT
GetClientRect(rcClientOld); //
// 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.
//重新计算RECT大小
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.
//所有的子窗口将被移动,以免被TOOLBAR覆盖
// 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); //得到子窗口
while(pwndChild) // 处理所有子窗口
{//移动所有子窗口
pwndChild->GetWindowRect(rcChild);
ScreenToClient(rcChild);
rcChild.OffsetRect(ptOffset);
pwndChild->MoveWindow(rcChild,FALSE);
pwndChild = pwndChild->GetNextWindow();
}CRect rcWindow;
GetWindowRect(rcWindow); // 得到对话框RECT
rcWindow.right += rcClientOld.Width() - rcClientNew.Width(); // 修改对话框尺寸
rcWindow.bottom += rcClientOld.Height() - rcClientNew.Height();
MoveWindow(rcWindow,FALSE); // Redraw WindowRepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);
做一个菜单在对话框初始化中加入如下代码
CMenu menu;
menu.LoadMenu(ID);
OK
m_pImageList = new CImageList;
SetupImages(m_pImageList);TBBUTTON tb;m_ToolBar = new CToolBarCtrl;
m_ToolBar->Create(WS_CHILD|WS_VISIBLE|WS_BORDER|TBSTYLE_FLAT,
CRect(0,0,0,0), this, 0);m_ToolBar->SetImageList(m_pImageList);
tb.iBitmap = 0;
tb.iString = NULL;
tb.fsState = TBSTATE_ENABLED;
tb.fsStyle = TBSTYLE_BUTTON;
tb.idCommand = ID_HELP_ABOUT;m_ToolBar->AddButtons(1, &tb);
tb.iBitmap = 1;
tb.idCommand = ID_BOLD;
m_ToolBar->AddButtons(1, &tb);
tb.iBitmap = 2;
tb.idCommand = ID_DRAWING;
m_ToolBar->AddButtons(1, &tb);TBBUTTON tb1;
tb1.fsStyle = TBSTYLE_SEP;
m_ToolBar->AddButtons(1, &tb1);tb.iBitmap = 3;
tb.idCommand = ID_STRIKEOUT;
tb.iString = NULL;
m_ToolBar->AddButtons(1, &tb);创建状态条的代码:
m_StatusBar = new CStatusBarCtrl;
m_StatusBar->Create(WS_CHILD|WS_VISIBLE|SBT_OWNERDRAW,
CRect(0,0,0,0), this, 0);int strPartDim[4]= {180, 260, 340, -1};
m_StatusBar->SetParts(4, strPartDim);m_StatusBar->SetText(_T("Dialog / StatusBar / Toolbar"),0,0);
m_StatusBar->SetText(_T("Example"), 1, 0);CString string;
string.LoadString(IDS_MYCOMPUTER);
m_StatusBar->SetText(string, 3 ,SBT_NOBORDERS );
m_StatusBar->SetIcon(3,
SetIcon(AfxGetApp()->LoadIcon(IDI_COMP),
FALSE));