实验六---对话框程序设计

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

实验六对话框、控件程序设计【实验目的】1、掌握基本对话框程序设计2、掌握典型控件程序设计3、理解计算机程序设计4、理解购书程序设计【实验内容】1、建立一个单文档应用程序,选择菜单后通过对话框输入一对坐标值,然后在视图区该坐标处输出你的姓名。插入菜单插入输入对话框(对话框、控件、类CInput、控件的成员变量m_x,m_y)定义消息响应函数参考代码:voidCXiTi5_1View::OnInput(){//TODO:Addyourcommandhandlercodehereintx,y;CClientDCdc(this);CInputdlg;if(dlg.DoModal()==IDOK){x=dlg.m_x;y=dlg.m_y;}dc.TextOut(x,y,自己姓名);}2、为应用程序工具栏添加两个工具按钮,选择第一个按钮,显示“打开文件”对话框,列出当前路径下的所有位图文件;选择第二个按钮,显示颜色对话框,并在试图区画一个该颜色的矩形。参考代码:voidCXiTi5_3View::OnMyopen(){//TODO:AddyourcommandhandlercodehereCStringFilePathName;CFileDialogdlg(TRUE,bmp,*.bmp,OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT,位图文件(*.bmp)|*.bmp||);dlg.DoModal();}voidCXiTi5_3View::OnColor(){//TODO:AddyourcommandhandlercodehereCOLORREFm_cc;CColorDialogdlg;//构建一个CColorDialog对象if(dlg.DoModal()==IDOK){CPennewpen,*oldpen;CClientDCdc(this);m_cc=dlg.GetColor();//得到在对话框中选择的颜色//用得到的颜色画矩形newpen.CreatePen(PS_SOLID,2,m_cc);oldpen=dc.SelectObject(&newpen);dc.Rectangle(50,100,200,200);dc.SelectObject(oldpen);}}3、设计简单计算器public:intm_OperationCount;intm_NumberCount;doublem_number[15];intm_Operation[15];voidcal();afx_msgvoidOnNumberKey(UINTnID);afx_msgvoidOnOperationKey(UINTnID);DECLARE_MESSAGE_MAP()CCalculatorDlg::CCalculatorDlg(CWnd*pParent/*=NULL*/):CDialog(CCalculatorDlg::IDD,pParent){//{{AFX_DATA_INIT(CCalculatorDlg)m_result=0.0;//}}AFX_DATA_INIT//NotethatLoadIcondoesnotrequireasubsequentDestroyIconinWin32m_hIcon=AfxGetApp()-LoadIcon(IDR_MAINFRAME);m_NumberCount=1;m_OperationCount=1;for(inti=0;i15;i++){m_number[i]=0;m_Operation[i]=999;}}BEGIN_MESSAGE_MAP(CCalculatorDlg,CDialog)//{{AFX_MSG_MAP(CCalculatorDlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()//}}AFX_MSG_MAPON_COMMAND_RANGE(IDC_NUMBER1,IDC_NUMBER0,OnNumberKey)ON_COMMAND_RANGE(IDC_MINUS,IDC_RESULT,OnOperationKey)END_MESSAGE_MAP()voidCCalculatorDlg::OnNumberKey(UINTnID){//处理单击数字按钮操作,记录输入的操作数intn=0;switch(nID)//根据单击的数字键ID,记录输入数字{caseIDC_NUMBER1:n=1;break;caseIDC_NUMBER2:n=2;break;caseIDC_NUMBER3:n=3;break;caseIDC_NUMBER4:n=4;break;caseIDC_NUMBER5:n=5;break;caseIDC_NUMBER6:n=6;break;caseIDC_NUMBER7:n=7;break;caseIDC_NUMBER8:n=8;break;caseIDC_NUMBER9:n=9;break;caseIDC_NUMBER0:n=0;break;}//计算操作数m_number[m_NumberCount]=m_number[m_NumberCount]*10+n;m_result=m_number[m_NumberCount];UpdateData(false);//在编辑框中显示操作数}voidCCalculatorDlg::OnOperationKey(UINTnID){//处理单击操作符按钮操作,记录输入的操作符inti;switch(nID)//根据单击的操作键ID,记录输入的加、减、乘、除操{//作符,处理取负、求平方根、求倒、倒退及清屏操作caseIDC_ADD:m_Operation[m_OperationCount]=1;break;caseIDC_SUBTRACT:m_Operation[m_OperationCount]=2;break;caseIDC_MULTIPLY:m_Operation[m_OperationCount]=3;break;caseIDC_DIVIDE:m_Operation[m_OperationCount]=4;break;caseIDC_MINUS://取负m_number[m_NumberCount]=-m_number[m_NumberCount];break;caseIDC_SQRT://求平方根m_number[m_NumberCount]=sqrt(m_number[m_NumberCount]);break;caseIDC_RECIPROCAL://求倒数m_number[m_NumberCount]=(double)1/m_number[m_NumberCount];break;caseIDC_BACK://倒退m_number[m_NumberCount]=(int)m_number[m_NumberCount]/10;m_result=m_number[m_NumberCount];UpdateData(false);break;caseIDC_CLEAR://清屏for(i=1;i11;i++){m_number[i]=0;m_Operation[m_OperationCount]=999;m_NumberCount=1;m_OperationCount=1;m_result=0;UpdateData(false);}break;caseIDC_RESULT://计算最后结果cal();break;}if(m_Operation[m_OperationCount]5){m_NumberCount++;m_OperationCount++;}}voidCCalculatorDlg::cal(){for(inti=1;i15;i++)switch(m_Operation[i])//先处理乘、除运算{case3:m_number[i]=m_number[i+1]=m_number[i]*m_number[i+1];break;case4:m_number[i]=m_number[i+1]=(double)m_number[i]/m_number[i+1];break;}m_result=m_number[1];for(i=1;i15;i++)//处理加减运算,计算最后结果if(m_Operation[i]==1)m_result=m_result+m_number[i+1];elseif(m_Operation[i]==2)m_result=m_result-m_number[i+1];UpdateData(false);//在编辑框中显示最后结果}4、编写一个简单的统计购书应付款的应用程序。该程序完成在组合框中选择要购买书籍的类别,然后再从列表框中选择要购买的书籍,并统计出应付的钱数。structcommodity{char*strType;char*strName;intbIsBuy;intnPrice;intcount;}comm[]={建筑类,建筑环境学,0,36,1,建筑类,建筑制图,0,28,1,建筑类,建筑结构,0,45,1,建筑类,建筑设备,0,62,1,社科类,民法,0,55,1,社科类,哲学,0,68,1,社科类,社会学,0,45,1,社科类,中国近代史,0,28,1,医学类,内科,0,76,1,医学类,临床医学,0,56,1,医学类,解剖学,0,39,1,医学类,护理学,0,90,1};在OnInitDialog()中添加:m_cmbType.SetCurSel(0);//设置组合框中初始显示的书籍类别for(inti=0;i4;i++)//设置列表框中初始显示的书籍名称m_lstName.AddString(comm[i].strName);m_spin.SetRange(1,100);//设置购书最高数量m_spin.SetPos(1);//设置购书数量初始显示值消息响应函数:书籍类别组合框:voidCSaleListDlg::OnSelchangeSaleType(){//TODO:AddyourcontrolnotificationhandlercodehereCStringstrtype;intmindex=m_cmbType.GetCurSel();//获取类别选择项位置序号m_lstName.ResetContent();//清除书名列表框中所有的列表项m_cmbType.GetLBText(mindex,strtype);//获取类别选择项文本for(inti=0;i12;i++)if(strtype==comm[i].strType)m_lstName.AddString(comm[i].strName);//将所选类别的书籍名称添加到列表框}图书名称列表框:voidCSaleListDlg::OnSelchangeSaleList(){//TODO:AddyourcontrolnotificationhandlercodehereUpdateData();for(inti=0;i12;i++){if(m_strName==comm[i].strName){CStringstr;str.Format(%d元,comm[i].nPrice);//获得价格m_strPrice=str;m_blsBuy=comm[i].bIsBuy;//是否已购所选书籍m_spin.SetPos(comm[i].count);//设置购书数量UpdateData(false);//更新显示break;}}}购买复选框:voidCSaleListDlg::OnIsBuy(){//TODO:AddyourcontrolnotificationhandlercodehereUpdateData(true);for(inti=0;i12;i++)if(m_strName==comm[i].strNam

1 / 8
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功