MFC的对话框工具栏、状态栏、和菜单栏的编辑

来源:互联网 发布:淘宝哪家卖外设保真 编辑:程序博客网 时间:2024/04/27 21:30

</pre>一、菜单栏,菜单栏的创建很简单,新建一个菜单资源,打开对话框属性,在菜单栏选项中选择新建的资源ID即可。<p></p><p>二、状态栏:由于状态栏和工具栏都是派生自CWND类,所以可以用create,进行创建</p><p></p><ol><li>                     在**dlg类头文件中声明状态栏对象   <span style="white-space:pre"></span>CStatusBar m_stb;</li><li>                     在OnInitDialog函数中创建状态栏的显示</li></ol><pre name="code" class="cpp">UINT array[2]={12301,12302};//状态栏分为两个m_stb.Create(this);m_stb.SetIndicators(array,sizeof(array)/sizeof(UINT));//显示状态栏CRect rect;GetWindowRect(rect);m_stb.SetPaneInfo(0,array[0],SBPS_NORMAL,rect.Width()/3);//第一栏占三分子一m_stb.SetPaneInfo(1,array[1],SBPS_STRETCH,rect.Width()/3*2);//<span style="font-family: Arial, Helvetica, sans-serif;">第一栏占三分子二</span>m_stb.SetPaneText(0,"lamber");        RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0); m_stb.GetStatusBarCtrl().SetBkColor(RGB(100,100,100));//设置背景色SetTimer(10,1000,NULL);//设置状态栏显示时间,创建定时器
ontimer里面的函数如下
void CCSlm_SoftwareDlg::OnTimer(UINT nIDEvent) {// TODO: Add your message handler code here and/or call defaultCTime stm;stm=CTime::GetCurrentTime();// CString str;// str.Format("%s",stm.Format("%y-%m-%d %H:%M:%S"));m_stb.SetPaneText(1,stm.Format("%H:%M:%S"));CDialog::OnTimer(nIDEvent);}//最终效果如下图
  • 下面编写工具栏                                                                                   三、工具栏:
  • 同上声明变量:
  • CToolBar m_toolbar;
  • 同样在初始化函数中创建工具栏,我的工具栏ID号为IDR_TOOLBAR2,
  • if (!m_Toolbar.CreateEx( this,TBSTYLE_FLAT ,  WS_CHILD | WS_VISIBLE | CBRS_ALIGN_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS,CRect(4,4,0,0)) ||!m_Toolbar.LoadToolBar(IDR_TOOLBAR2) ){TRACE0("failed to create toolbar/n");return FALSE;}m_Toolbar.ShowWindow(SW_SHOW);    RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0)


  • 接下来添加提示信息
  • <span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">在**Dlg类定义中</span><span style="color:#999999;"><span style="font-family: Arial; font-size: 14px; line-height: 26px;">手</span><span style="font-family: Arial; font-size: 14px; line-height: 26px;">工</span><span style="font-family: Arial; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">添加</span><span style="font-family: Arial; font-size: 14px; line-height: 26px;">消</span><span style="font-family: Arial; font-size: 14px; line-height: 26px;">息</span></span><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">映射函数的定义, //{{AFX_MSG(CDlgMenuToolbarDlg)</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">virtual BOOL OnInitDialog();</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">afx_msg void OnPaint();</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color:#ff0000;">afx_msg BOOL OnToolTipText(UINT, NMHDR* pNMHDR, LRESULT* pResult);</span></span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">//}}AFX_MSG</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">DECLARE_MESSAGE_MAP()</span>
  • <span style="font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">在**Dlg.cpp<span style="background-color: rgb(204, 204, 204);">中</span></span><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; background-color: rgb(204, 204, 204);">添加</span><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="background-color: rgb(204, 204, 204);">消</span>息映射,请看如下代码中的黑体部分 BEGIN_MESSAGE_MAP(CDlgMenuToolbarDlg, CDialog)</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">//{{AFX_MSG_MAP(CDlgMenuToolbarDlg)</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">ON_WM_PAINT()</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color:#ff0000;">ON_NOTIFY_EX( TTN_NEEDTEXT, 0, OnToolTipText )</span></span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">//}}AFX_MSG_MAP</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">END_MESSAGE_MAP()</span><span style="color:#333333;"></span></span>
  • <span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">在**Dlg.cpp</span><span style="font-family: Arial; font-size: 14px; line-height: 26px; background-color: rgb(255, 102, 255);">添加</span><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">函数的实现代码 //</span><span style="font-family: Arial; font-size: 14px; line-height: 26px; color: white; background-color: rgb(136, 0, 0);">工具栏</span><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">提示</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">BOOL CDlgMenuToolbarDlg::OnToolTipText(UINT, NMHDR* pNMHDR, LRESULT* pResult)</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">{</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">ASSERT(pNMHDR->code == TTN_NEEDTEXTA || pNMHDR->code == TTN_NEEDTEXTW);</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">// UNICODE消息</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">TOOLTIPTEXTA* pTTTA = (TOOLTIPTEXTA*)pNMHDR;</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">TOOLTIPTEXTW* pTTTW = (TOOLTIPTEXTW*)pNMHDR;</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">//TCHAR szFullText[512];</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">CString strTipText;</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">UINT nID = pNMHDR->idFrom;</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">if (pNMHDR->code == TTN_NEEDTEXTA && (pTTTA->uFlags & TTF_IDISHWND) ||</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">pNMHDR->code == TTN_NEEDTEXTW && (pTTTW->uFlags & TTF_IDISHWND))</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">{</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">// idFrom为</span><span style="font-family: Arial; font-size: 14px; line-height: 26px; color: white; background-color: rgb(136, 0, 0);">工具</span><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">条的HWND </span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">nID = ::GetDlgCtrlID((HWND)nID);</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">}</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">if (nID != 0) //不为分隔符</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">{</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">strTipText.LoadString(nID);</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">strTipText = strTipText.Mid(strTipText.Find(''/n'',0)+1);</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">#ifndef _UNICODE</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">if (pNMHDR->code == TTN_NEEDTEXTA)</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">{</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">lstrcpyn(pTTTA->szText, strTipText, sizeof(pTTTA->szText));</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">}</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">else</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">{</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">_mbstowcsz(pTTTW->szText, strTipText, sizeof(pTTTW->szText));</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">}</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">#else</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">if (pNMHDR->code == TTN_NEEDTEXTA)</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">{</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">_wcstombsz(pTTTA->szText, strTipText,sizeof(pTTTA->szText));</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">}</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">else</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">{</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">lstrcpyn(pTTTW->szText, strTipText, sizeof(pTTTW->szText));</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">}</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">#endif</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">*pResult = 0;</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">// 使</span><span style="font-family: Arial; font-size: 14px; line-height: 26px; color: white; background-color: rgb(136, 0, 0);">工具</span><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">条提示窗口在最上面</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">::SetWindowPos(pNMHDR->hwndFrom, HWND_TOP, 0, 0, 0, 0,SWP_NOACTIVATE|</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">SWP_NOSIZE|SWP_NOMOVE|SWP_NOOWNERZORDER); </span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">return TRUE;</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">}</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">return TRUE;</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">}</span></span>
  • <span style="font-family:Arial;color:#333333;"><span style="font-size: 14px; line-height: 26px;">最后一步,你必须打开添加的工具栏资源属性,查看工具栏的按钮下面会有一个提示,写上你需要的提示信息即可</span></span>
  • <span style="font-family:Arial;color:#333333;"><span style="font-size: 14px; line-height: 26px;"><img src="http://img.blog.csdn.net/20150616124031054?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGFtYmVyY3Nkbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /></span></span>

0 0