VC++ 对话框下使用工具栏

 关于这一技术网上也有很多的记录,下面仅记录我测试OK的代码。

    在CXXDlg.h中添加如下成员变量:

  CToolBar m_ToolBar;

  CBitmap m_bmpTool;

  在CXXDlg.cpp的OnInitDialog中添加如下代码:

 1      UINT nIDs[] = 
 2      {
 3          IDC_OPEN,
 4          IDC_CLOSE,
 5          IDC_FIND,
 6          IDC_COPY,
 7      };
 8  
 9      m_ToolBar.Create(this);
10      m_bmpTool.LoadBitmapW(IDB_BITMAP2);
11      m_ToolBar.SetBitmap(m_bmpTool);
12      m_ToolBar.SetButtons(nIDs,4);
13      m_ToolBar.SetButtonText(0,_T("打开"));
14      m_ToolBar.SetButtonText(1,_T("关闭"));
15      m_ToolBar.SetButtonText(3,_T("复制"));
16      m_ToolBar.SetButtonText(2,_T("查找"));
17      m_ToolBar.SetSizes(CSize(55,55),CSize(32,32));
18  
19      CRect rcClientStart;  
20      CRect rcClientNow;  
21      GetClientRect(rcClientStart);  
22      RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0, reposQuery, rcClientNow);  
23    
24      CPoint ptOffset(rcClientNow.left - rcClientStart.left,  
25      (rcClientNow.top - rcClientStart.top));  
26    
27      CRect  rcChild;  
28      CWnd* pwndChild = GetWindow(GW_CHILD);  
29      while (pwndChild)  
30      {  
31          pwndChild->GetWindowRect(rcChild);  
32          ScreenToClient(rcChild);  
33          rcChild.OffsetRect(ptOffset);  
34          pwndChild->MoveWindow(rcChild, FALSE);  
35          pwndChild = pwndChild->GetNextWindow();  
36      }  
37    
38      CRect rcWindow;  
39      GetWindowRect(rcWindow);  
40      rcWindow.right += rcClientStart.Width() - rcClientNow.Width();  
41      rcWindow.bottom += (rcClientStart.Height() - rcClientNow.Height());  
42      MoveWindow(rcWindow, FALSE);  
43      
44      RepositionBars(AFX_IDW_CONTROLBAR_FIRST,  
45      AFX_IDW_CONTROLBAR_LAST, 0, reposExtra); 
46      CenterWindow();

    按钮的消息效应操作如下:

  消息映射如下:

  ON_COMMAND(IDC_OPEN,&CXXDlg::OnBtnOpen)
  ON_COMMAND(IDC_CLOSE,&CXXDlg::OnBtnClose)
  ON_COMMAND(IDC_FIND,&CXXDlg::OnBtnFind)
  ON_COMMAND(IDC_COPY,&CXXDlg::OnBtnCopy)

 当然这几个消息映射函数定义为了CXXDlg的成员函数,如下: 

 afx_msg void OnBtnOpen();
   afx_msg void OnBtnClose();
   afx_msg void OnBtnFind();
   afx_msg void OnBtnCopy();

 再去实现这些成员函数既可。

 实际效果如下图:

  

转载于:https://www.cnblogs.com/kanite/p/5038405.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值