当前位置:文档之家› VC++对话框添加工具栏

VC++对话框添加工具栏

VC++对话框添加工具栏
VC++对话框添加工具栏

VC++对话框添加工具栏

1、添加工具栏资源ID为IDR_TOOLBAR

2、在对话框的类定义中加:

CToolBar m_ToolBar;

3、在OnInitDialog中或其它合适的消息响应中加如下代码:(函数可查看MSDN)

m_ToolBar.Create(this); //创建工具栏

m_ToolBar.LoadToolBar(IDR_TOOLBAR);//加载工具栏

//得出控件条大小.

CRect rect;

CRect rectNow;

GetClientRect(rect);

RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0,reposQuery,rect Now);

//放置控件条位置

CPoint ptOffset(rectNow.left-rect.left,rectNow.top-rect.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);

rcWindow.right+=rect.Width()-rectNow.Width();

rcWindow.bottom+=rect.Height()-rectNow.Height();

MoveWindow(rcWindow, FALSE);

//控件条定位

RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);

//对框居中

CenterWindow();

4、手工添加处理函数

afx_msg void OnBtnXXX();//消息响应函数声明

ON_COMMAND(ID_BTN_XXX/*工具按钮ID*/,OnBtnXXX/*函数名*/)//消息映射

void CXXXDlg::OnBtnXXX(){}//消息处理函数

//方法二

在CDIALOGTOOLBARDL类声明中添加成员变量

CToolBar m_wndtoolbar:

在初始化对话框中添加BOOL CMy4Dlg::OnInitDialog()

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("faled");

return FALSE;

}

m_wndtoolbar.ShowWindow(SW_SHOW);

RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLB AR_LAST,0);

利用CLASSWIZARD添加消息响应函数(每一个图标就是一个按钮,有ID)

相关主题
文本预览
相关文档 最新文档