MFC基于对话框的菜单栏,工具栏和状态栏的创建
2015-06-16 11:54
766 查看
一、菜单栏,菜单栏的创建很简单,新建一个菜单资源,打开对话框属性,在菜单栏选项中选择新建的资源ID即可。
二、状态栏:由于状态栏和工具栏都是派生自CWND类,所以可以用create,进行创建
在**dlg类头文件中声明状态栏对象
CStatusBar m_stb;
在OnInitDialog函数中创建状态栏的显示
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 default CTime 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 4000 ;" /><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="https://img-blog.csdn.net/20150616124031054?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGFtYmVyY3Nkbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /> </span></span>
相关文章推荐
- ruby实现网页图片抓取
- 项目中发现Element类下无getTextContent函数
- POJ 1860 Bellman-Ford算法
- 12-1路径初识
- 逆向思维 PMC云化数据中心的“技术”革命
- Android日志打印命令
- hive到hbase创建映射表以及添加数据
- IOS三方开源框架总结
- hibernate.c3p0
- FreeMarker 全文讲解
- sqlserver 中的CASE用法
- tips 前端 点击事件
- MyEclipse 2015 CI
- python virtualenv
- Codeforces Round #135 (Div. 2) D Choosing Capital for Treeland
- ThreadLocal
- 栅格数据与矢量数据
- 手动修复OneDrive的DNS污染屏蔽的方法
- MySQL常用查询语句
- java中堆和栈的区别