实验五 菜单工具栏状态栏程序设计.docx
- 文档编号:10713729
- 上传时间:2023-05-27
- 格式:DOCX
- 页数:17
- 大小:18.25KB
实验五 菜单工具栏状态栏程序设计.docx
《实验五 菜单工具栏状态栏程序设计.docx》由会员分享,可在线阅读,更多相关《实验五 菜单工具栏状态栏程序设计.docx(17页珍藏版)》请在冰点文库上搜索。
实验五菜单工具栏状态栏程序设计
实验五菜单、工具栏、状态栏程序设计
【实验目的】
1、掌握主菜单程序设计方法
2、学会工具栏程序设计
3、掌握快捷菜单程序设计方法
4、学会增加状态栏窗格
【实验内容】
1、建立一个单文档应用程序,添加菜单显示,其中包含“文本”和“图形”2个菜单项,分别输出文本和图形。
参考代码:
voidCSDIDispView:
:
OnText()
{
//TODO:
Addyourcommandhandlercodehere
CClientDCdc(this);
dc.TextOut(20,20,"我已经学会了如何设计菜单程序!
");
}
voidCSDIDispView:
:
OnPicture()
{
//TODO:
Addyourcommandhandlercodehere
CClientDCdc(this);
CBrush*BrushOld,BrushNew;
BrushNew.CreateSolidBrush(RGB(255,0,0));
BrushOld=dc.SelectObject(&BrushNew);//选用画刷
dc.Rectangle(50,50,250,150);
dc.SelectObject(BrushOld);//还原画刷
BrushNew.DeleteObject();//释放画刷
}
2、为上述应用程序添加菜单控制功能,选中“文本”时,“图形”菜单项无效;当选中“图形”时,“文本”菜单项无效。
参考代码:
voidCSDIDispView:
:
OnText()
{
//TODO:
Addyourcommandhandlercodehere
CClientDCdc(this);
dc.TextOut(20,20,"我已经学会了如何设计菜单程序!
");
m_picture=false;
}
voidCSDIDispView:
:
OnPicture()
{
//TODO:
Addyourcommandhandlercodehere
CClientDCdc(this);
CBrush*BrushOld,BrushNew;
BrushNew.CreateSolidBrush(RGB(255,0,0));
BrushOld=dc.SelectObject(&BrushNew);//选用画刷
dc.Rectangle(50,50,250,150);
dc.SelectObject(BrushOld);//还原画刷
BrushNew.DeleteObject();//释放画刷
m_text=false;
}
voidCSDIDispView:
:
OnUpdateText(CCmdUI*pCmdUI)
{
//TODO:
AddyourcommandupdateUIhandlercodehere
pCmdUI->Enable(m_text);
}
voidCSDIDispView:
:
OnUpdatePicture(CCmdUI*pCmdUI)
{
//TODO:
AddyourcommandupdateUIhandlercodehere
pCmdUI->Enable(m_picture);
}
3、为上述应用程序工具栏添加两个工具按钮,单击第一个按钮,在视图窗口中弹出“打开文件”对话框,单击第二个按钮,在消息框中显示文本信息。
参考代码:
voidCXiTi4_3View:
:
OnText()
{
//TODO:
Addyourcommandhandlercodehere
MessageBox("我已经学会了使用默认工具栏了!
");
}
4、创建一个单文档应用程序,在状态栏中显示鼠标光标的坐标。
(1)在MainFrm.h中将状态栏对象修改为public
public:
CStatusBarm_wndStatusBar;
(2)在MainFrm.cpp中添加状态栏窗格ID_INDICATOR_COR
(3)在串表中设置添加窗格的字符串格式
(4)添加WM_MOUSEMOVE消息及其相应函数
voidCXiTi4_4View:
:
OnMouseMove(UINTnFlags,CPointpoint)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
CStringstr;
CMainFrame*pFrame=(CMainFrame*)AfxGetApp()->m_pMainWnd;
CStatusBar*pStatus=&pFrame->m_wndStatusBar;
str.Format("%d,%d",point.x,point.y);
pStatus->SetPaneText(pStatus->CommandToIndex(ID_INDICATOR_COR),str);
CView:
:
OnMouseMove(nFlags,point);
}
5、建立一个单文档应用程序,添加“时间”主菜单项,包含时、分、秒三个菜单项,分别在视图窗口中显示当前系统时间的时、分和秒。
添加相应的工具栏和快捷菜单,并在状态栏中显示系统时间。
(1)添加菜单项、添加工具栏
插入工具栏并编辑,添加工具栏类
定义工具栏对象:
CToolBarm_wndToolBar,m_wndToolBar1;
加载工具栏:
OnCreate():
if(!
m_wndToolBar1.CreateEx(this,TBSTYLE_FLAT,WS_CHILD|WS_VISIBLE|CBRS_TOP
|CBRS_GRIPPER|CBRS_TOOLTIPS|CBRS_FLYBY|CBRS_SIZE_DYNAMIC)||
!
m_wndToolBar1.LoadToolBar(IDR_TOOLBAR1))
{
TRACE0("Failedtocreatetoolbar\n");
return-1;//failtocreate
}
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
m_wndToolBar1.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
DockControlBar(&m_wndToolBar1);
设置时钟:
SetTimer(1,1000,NULL);
(2)添加状态栏
添加状态栏窗格:
ID_INDICATOR_TIME,
设置字符串格式
状态栏数据成员设置为公有成员
voidCMainFrame:
:
OnTimer(UINTnIDEvent)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
CTimetime;
time=CTime:
:
GetCurrentTime();//获得系统时间
CStringstr=time.Format("%H:
%M:
%S");//将系统时间转换成时:
分:
秒格式的字符串
m_wndStatusBar.SetPaneText(m_wndStatusBar.CommandToIndex(ID_INDICATOR_TIME),str);//更新时间窗格显示的时间内容
CFrameWnd:
:
OnTimer(nIDEvent);
}
voidCMainFrame:
:
OnClose()
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
KillTimer
(1);//关闭计时器
CFrameWnd:
:
OnClose();
}
(3)添加菜单消息响应
定义CTime类的对象time
添加菜单消息响应函数:
voidCSTimeView:
:
OnSecond()
{
//TODO:
Addyourcommandhandlercodehere
time=CTime:
:
GetCurrentTime();//获得系统时间
CStringstr=time.Format("%S");//将系统时间转换成秒格式的字符串
CClientDCdc(this);
dc.TextOut(100,100,"现在是"+str+"秒");
}
voidCSTimeView:
:
OnHour()
{
//TODO:
Addyourcommandhandlercodehere
time=CTime:
:
GetCurrentTime();//获得系统时间
CStringstr=time.Format("%H");//将系统时间转换成时格式的字符串
CClientDCdc(this);
dc.TextOut(100,100,"现在是"+str+"时");
}
voidCSTimeView:
:
OnMinute()
{
//TODO:
Addyourcommandhandlercodehere
time=CTime:
:
GetCurrentTime();//获得系统时间
CStringstr=time.Format("%M");//将系统时间转换成分格式的字符串
CClientDCdc(this);
dc.TextOut(100,100,"现在是"+str+"分");
}
(4)添加快捷菜单
设计快捷菜单
添加WM_ContextMenu消息响应函数:
voidCSTimeView:
:
OnContextMenu(CWnd*,CPointpoint)
{
//CG:
ThisblockwasaddedbythePop-upMenucomponent
{
if(point.x==-1&&point.y==-1){
//keystrokeinvocation
CRectrect;
GetClientRect(rect);
ClientToScreen(rect);
point=rect.TopLeft();
point.Offset(5,5);
}
CMenumenu;
VERIFY(menu.LoadMenu(CG_IDR_POPUP_STIME_VIEW));
CMenu*pPopup=menu.GetSubMenu(0);
ASSERT(pPopup!
=NULL);
CWnd*pWndPopupOwner=this;
while(pWndPopupOwner->GetStyle()&WS_CHILD)
pWndPopupOwner=pWndPopupOwner->GetParent();
pPopup->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,point.x,point.y,
pWndPopupOwner);
}
}
6、绘制正弦和余弦曲线,设置线型和颜色的变换。
1)
添加菜单
添加工具栏资源
添加快捷菜单资源
2)加载工具栏:
Mainfrm.h:
CToolBarm_wndToolBar1;
Mainfrm.cpp:
Oncreate:
if(!
m_wndToolBar1.CreateEx(this,TBSTYLE_FLAT,WS_CHILD|WS_VISIBLE|CBRS_TOP
|CBRS_GRIPPER|CBRS_TOOLTIPS|CBRS_FLYBY|CBRS_SIZE_DYNAMIC)||
!
m_wndToolBar1.LoadToolBar(IDR_TOOLBAR1))
{
TRACE0("Failedtocreatetoolbar\n");
return-1;//failtocreate
}
//TODO:
Deletethesethreelinesifyoudon'twantthetoolbarto
//bedockable
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
m_wndToolBar1.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
DockControlBar(&m_wndToolBar1);
3)加载快捷菜单:
添加视图类得wm_context消息
voidCP1View:
:
OnContextMenu(CWnd*pWnd,CPointpoint)
{
//TODO:
Addyourmessagehandlercodehere
CMenumenu,*pPopup;
menu.LoadMenu(IDR_MENU1);//加载快捷菜单
pPopup=menu.GetSubMenu(0);
CWnd*pWndPopupOwner=this;
pWndPopupOwner=pWndPopupOwner->GetParent();
//显示快捷菜单
pPopup->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,point.x,point.y,pWndPopupOwner);
4)添加视图类的成员变量及成员函数
Cview.h
constdoublePI=3.1416;
public:
intm_able;
intm_typ;
intm_wid;
COLORREFm_col;
doublecalsin(intx);
doublecalcos(intx);
voiddrawcurve(CDC*pDC);
voiddrawline(CDC*pDC,intx1,inty1,intx2,inty2);
voiddrawaxis(CDC*pDC);
Cview.cpp
CCurveView:
:
CCurveView()
{
//TODO:
addconstructioncodehere
m_typ=0;
m_col=RGB(0,0,0);
m_wid=1;
m_able=0;
}
voidCCurveView:
:
OnDraw(CDC*pDC)
{
CCurveDoc*pDoc=GetDocument();
ASSERT_VALID(pDoc);
//TODO:
adddrawcodefornativedatahere
drawcurve(pDC);
}
doubleCCurveView:
:
calsin(intx)
{
doubley;
y=sin(x*PI/180);
returny;
}
doubleCCurveView:
:
calcos(intx)
{
doubley;
y=cos(x*PI/180);
returny;
}
voidCCurveView:
:
drawaxis(CDC*pDC)
{
CStringstr;
CFontmyfont,*oldfont;
myfont.CreatePointFont(80,"Arial",pDC);
oldfont=pDC->SelectObject(&myfont);
drawline(pDC,50,10,50,290);
drawline(pDC,45,150,780,150);
for(inti=50;i<780;i=i+90)
{
drawline(pDC,i,145,i,150);
str.Format("%d",i-50);
if((i-50)!
=0)pDC->TextOut(i,155,str);
}
for(i=0;i<11;i++)
{
drawline(pDC,50,50+20*i,55,50+20*i);
str.Format("%.2f",1-0.2*i);
if(i!
=5)pDC->TextOut(15,45+20*i,str);
elsepDC->TextOut(15,45+20*i,"0");
}
pDC->SelectObject(oldfont);
myfont.DeleteObject();
}
voidCCurveView:
:
drawline(CDC*pDC,intx1,inty1,intx2,inty2)
{
pDC->MoveTo(x1,y1);
pDC->LineTo(x2,y2);
}
voidCCurveView:
:
drawcurve(CDC*pDC)
{
drawaxis(pDC);
CPointpoint[750];
CPenmypen,*oldpen;
mypen.CreatePen(PS_SOLID,m_wid,m_col);
oldpen=pDC->SelectObject(&mypen);
for(inti=0;i<722;i++)
{
point[i].x=i+50;
if(m_typ==1)
point[i].y=(int)(150-calsin(i)*100);
elseif(m_typ==2)
point[i].y=(int)(150-calcos(i)*100);
}
for(i=0;i<721;i++)
drawline(pDC,point[i].x,point[i].y,point[i+1].x,point[i+1].y);
pDC->SelectObject(oldpen);
mypen.DeleteObject();
}
5)添加菜单消息响应函数
voidCCurveView:
:
OnColb()
{
//TODO:
Addyourcommandhandlercodehere
m_col=RGB(0,0,255);
Invalidate();
}
voidCCurveView:
:
OnUpdateColb(CCmdUI*pCmdUI)
{
//TODO:
AddyourcommandupdateUIhandlercodehere
pCmdUI->Enable(m_able);
if(m_col==RGB(0,0,255))
pCmdUI->SetCheck(true);
else
pCmdUI->SetCheck(false);
}
voidCCurveView:
:
OnColr()
{
//TODO:
Addyourcommandhandlercodehere
m_col=RGB(255,0,0);
Invalidate();
}
voidCCurveView:
:
OnUpdateColr(CCmdUI*pCmdUI)
{
//TODO:
AddyourcommandupdateUIhandlercodehere
pCmdUI->Enable(m_able);
if(m_col==RGB(255,0,0))
pCmdUI->SetCheck(true);
else
pCmdUI->SetCheck(false);
}
voidCCurveView:
:
OnCos()
{
//TODO:
Addyourcommandhandlercodehere
m_typ=2;
m_able=1;
Invalidate();
}
voidCCurveView:
:
OnSin()
{
//TODO:
Addyourcommandhandlercodehere
m_typ=1;
m_able=1;
Invalidate();
}
voidCCurveView:
:
OnWidsp2()
{
//TODO:
Addyourcommandhandlercodehere
m_wid=2;
Invalidate();
}
voidCCurveView:
:
OnUpdateWidsp2(CCmdUI*pCmdUI)
{
//TODO:
AddyourcommandupdateUIhandlercodehere
pCmdUI->Enable(m_able);
if(m_wid==2)
pCmdUI->SetCheck(true);
else
pCmdUI->SetCheck(false);
}
voidCCurveView:
:
OnWidsp3()
{
//TODO:
Addyourcommandhandlercodehere
m_wid=3;
Invalidate();
}
voidCCurveView:
:
OnUpdateWidsp3(CCmdUI*pCmdUI)
{
//TODO:
AddyourcommandupdateUIhandlercodehere
pCmdUI->Enable(m_able);
if(m_wid==3)
pCmdUI->SetCheck(true);
else
pCmdUI->SetCheck(false);
}
6)添加状态栏窗格
mainfrm.cpp:
ID_INDICATOR_PROMPT
Maifrm.cpp:
public:
CStatusBarm_wndStatusBar;
在View.h添加宏:
afx_msgvoidOnUpdatePrompt(CCmdUI*pCmdUI);
在view.cpp添加消息映射及消息处理函数:
ON_UPDATE_COMMAND_UI(ID_INDICATOR_PROMPT,OnUpdatePrompt)
#include"MainFrm.h"
voidCCurveView:
:
OnUpdatePrompt(CCmdUI*pCmdUI)
{
//TODO:
AddyourcommandupdateUIhandlercode
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验五 菜单工具栏状态栏程序设计 实验 菜单 工具栏 状态栏 程序设计