中南大学数学院大三年级面向对象实验报告.docx

上传人:b****6 文档编号:7574105 上传时间:2023-01-25 格式:DOCX 页数:20 大小:60.68KB
下载 相关 举报
中南大学数学院大三年级面向对象实验报告.docx_第1页
第1页 / 共20页
中南大学数学院大三年级面向对象实验报告.docx_第2页
第2页 / 共20页
中南大学数学院大三年级面向对象实验报告.docx_第3页
第3页 / 共20页
中南大学数学院大三年级面向对象实验报告.docx_第4页
第4页 / 共20页
中南大学数学院大三年级面向对象实验报告.docx_第5页
第5页 / 共20页
点击查看更多>>
下载资源
资源描述

中南大学数学院大三年级面向对象实验报告.docx

《中南大学数学院大三年级面向对象实验报告.docx》由会员分享,可在线阅读,更多相关《中南大学数学院大三年级面向对象实验报告.docx(20页珍藏版)》请在冰豆网上搜索。

中南大学数学院大三年级面向对象实验报告.docx

中南大学数学院大三年级面向对象实验报告

中南大学数学院大三年级面向对象实验报告

 

C++面向对象程序设计实验报告

 

姓名:

班级:

学号:

{

//TODO:

Addyourmessagehandlercodehereand/orcalldefault

p2=point;

CClientDCdc(this);

OnPrepareDC(&dc);

dc.MoveTo(p1);

dc.LineTo(p2);

m=0;

CView:

:

OnLButtonUp(nFlags,point);

}

voidCZuoBiaoView:

:

OnMouseMove(UINTnFlags,CPointpoint)

{

//TODO:

Addyourmessagehandlercodehereand/orcalldefault

staticintn=0;

switch(++n)

{

case1:

p2=point;

break;

case2:

p1=p2;

p2=point;

n=0;

break;

}

CClientDCdc(this);

OnPrepareDC(&dc);

if(m==1)

{

dc.MoveTo(p1);

dc.LineTo(p2);

}

CView:

:

OnMouseMove(nFlags,point);

}

 

实验二,创建菜单

用MFC创建菜单按钮

具体的制作过程:

首先,生成一个MFCAppWizardEXE工程

生成一个以CButton为基类的新类,名为CMenuButton,然后用ClassWizard为其添加两个成员函数:

DrawItem()和PreSubclassWidnow();手工为CMenuButton类添加BOOL类型m_bDrawFocusRect成员变量,用于决定是否在按钮上画焦点矩形,添加SetDrawFocusRect()函数用于设置这个标志,默认为画焦点矩形;添加两个枚举类型的变量m_ArrowType和m_PopupPos,用于决定所画的箭头的类型和菜单弹出的位置。

箭头可为右箭头、下箭头、小右箭头、小下箭头、上箭头和左箭头;菜单的弹出位置可以为按钮的左上角、右上角、左下角和右下角。

最后手工添加两个函数,SetArrowType()和SetMenuPopupPos(),用于设置以上各种风格,其默值分别为画右箭头和在左下角弹出。

如果只需要菜单而不需要画箭头,只需置空BS_OWNERDRAW标志位即可,添加一个SetStyle()函数,用于设置是画箭头还是显示文本,其默认值是画箭头。

为方便处理按钮的BN_CLICKED通知消息,为CMenuButton类创建一个公有的成员函数OnClick(),以便在BN_CLICKED的消息处理器中调用。

它有两个参数,第一个是菜单资源的ID,第二个参数为子菜单的ID,默认为0。

如果只有一组子菜单,则可使用其默认值0。

OnClick()函数的返回值为所选的菜单项的命令ID,若未作任何有效选择,则返回0。

下面是程序代码。

头文件:

 

#if!

defined(_EWAY_MEMUBUTTON_H__INCLUDED_)

#define_EWAY_MEMUBUTTON_H__INCLUDED_

 

#if_MSC_VER>=1000

#pragmaonce

#endif//_MSC_VER>=1000

 

//MenuButton.h:

headerfile

//

 

classCMenuButton:

publicCButton

{

public:

CMenuButton();

virtual~CMenuButton();

DECLARE_DYNAMIC(CMenuButton)

 

enumArrowType

{

arrowRight,//向右的箭头;

arrowDown,//向下的箭头;

arrowSmallRight,//向右的小箭头;

arrowSmallDown,//向下的小箭头;

arrowUp,//向上的箭头;

arrowLeft//向左的箭头;

}m_ArrowType;

enumPopupPos

{

//名称为TopLeft等等,遵守英文习惯;

posTopLeft,//左上角;

posBottomLeft,//左下角;

posTopRight,//右上角;

posBottomRight,//右下角;

}m_PopupPos;

virtualUINTOnClick(UINTnIDMenuResource,UINTnSubMenu=0);

voidSetArrowType(CMenuButton:

:

ArrowTypetype=CMenuButton:

:

arrowRight);

voidSetDrawFocusRect(BOOLbDrawFocusRect=TRUE);

voidSetMenuPopupPos(CMenuButton:

:

PopupPospos=CMenuButton:

:

posBottomLeft);

voidSetStyle(BOOLbDrawArrow=TRUE);

 

//Overrides

//ClassWizardgeneratedvirtualfunctionoverrides

//{{AFX_VIRTUAL(CMenuButton)

public:

virtualvoidDrawItem(LPDRAWITEMSTRUCTlpDrawItemStruct);

protected:

virtualvoidPreSubclassWindow();

//}}AFX_VIRTUAL

 

protected:

BOOLm_bDrawFocusRect;

//{{AFX_MSG(CMenuButton)

//}}AFX_MSG

DECLARE_MESSAGE_MAP()

};

 

//{{AFX_INSERT_LOCATION}}

//MicrosoftDeveloperStudiowillinsertadditionaldeclarationsimmediatelybeforethepreviousline.

 

#endif//!

defined(_EWAY_MEMUBUTTON_H__INCLUDED_)

 

实现文件:

 

//MenuButton.cpp:

implementationfile

//

 

#include"stdafx.h"

#include"MenuButtonTest.h"

#include"MenuButton.h"

 

#ifdef_DEBUG

#definenewDEBUG_NEW

#undefTHIS_FILE

staticcharTHIS_FILE[]=__FILE__;

#endif

 

/////////////////////////////////////////////////////////////////////////////

//CMenuButton

 

IMPLEMENT_DYNAMIC(CMenuButton,CButton)

 

CMenuButton:

:

CMenuButton()

{

SetArrowType();

SetDrawFocusRect();

SetMenuPopupPos();

}

 

CMenuButton:

:

~CMenuButton()

{

}

 

 

BEGIN_MESSAGE_MAP(CMenuButton,CButton)

//{{AFX_MSG_MAP(CMenuButton)

//}}AFX_MSG_MAP

END_MESSAGE_MAP()

 

/////////////////////////////////////////////////////////////////////////////

//CMenuButtonmessagehandlers

 

voidCMenuButton:

:

DrawItem(LPDRAWITEMSTRUCTlpDrawItemStruct)

{

//使用FromeHandle()创建临时的对象,若使用Attach()则必需在最后使用Detach()

CDC*pDC=CDC:

:

FromHandle(lpDrawItemStruct->hDC);

 

//得到画笔的颜色;

CPenpen;

if((lpDrawItemStruct->itemState&ODS_DISABLED))

{

pen.CreatePen(PS_SOLID,0,:

:

GetSysColor(COLOR_GRAYTEXT));

}

else

{

pen.CreatePen(PS_SOLID,0,:

:

GetSysColor(COLOR_BTNTEXT));

}

CPen*pOldPen=pDC->SelectObject(&pen);

 

CFontfont;

font.CreateFont(12,0,0,0,FW_NORMAL,0,0,0,

DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,

CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,

DEFAULT_PITCH|FF_SWISS,"Marlett");

 

CFont*pOldFont=pDC->SelectObject(&font);

CSizesize=pDC->GetTextExtent("4",1);

 

//决定箭头的样子,具体的值可在“字符映射表”中查得;

CStringstrArrow;

switch(m_ArrowType)

{

caseCMenuButton:

:

arrowRight:

strArrow="4";

break;

caseCMenuButton:

:

arrowDown:

strArrow="6";

break;

caseCMenuButton:

:

arrowSmallRight:

strArrow="8";

break;

caseCMenuButton:

:

arrowSmallDown:

strArrow="9";

break;

caseCMenuButton:

:

arrowUp:

strArrow="5";

break;

caseCMenuButton:

:

arrowLeft:

strArrow="3";

break;

default:

ASSERT(FALSE);

}

 

//计算座标值,用于绘制箭头;

intx=(lpDrawItemStruct->rcItem.right-lpDrawItemStruct->rcItem.left-size.cx)/2;

inty=(lpDrawItemStruct->rcItem.bottom-lpDrawItemStruct->rcItem.top-size.cy)/2;

 

//画按钮与箭头;

if((lpDrawItemStruct->itemState&ODS_SELECTED))

{

pDC->DrawFrameControl(&lpDrawItemStruct->rcItem,DFC_BUTTON,DFCS_BUTTONPUSH|DFCS_PUSHED);

//在按钮被按下时,上面的字符要有一个向右和向下的偏移;

pDC->TextOut(++x,++y,strArrow);

}

else

{

pDC->DrawFrameControl(&lpDrawItemStruct->rcItem,DFC_BUTTON,DFCS_BUTTONPUSH);

pDC->TextOut(x,y,strArrow);

}

 

//如果需要,画焦点矩形;

if((lpDrawItemStruct->itemState&ODS_FOCUS)&&m_bDrawFocusRect)

{

CRectrectFocus(lpDrawItemStruct->rcItem);

rectFocus.DeflateRect(3,3);//看起来比较接近的值;

pDC->DrawFocusRect(rectFocus);

}

 

//仅将对象选回即可,不必调用DeleteTempMap();

pDC->SelectObject(pOldPen);

pDC->SelectObject(pOldFont);

}

 

UINTCMenuButton:

:

OnClick(UINTnIDMenuResource,UINTnSubMenu/*=0*/)

{

CMenumenu;

//装载菜单;

VERIFY(menu.LoadMenu(nIDMenuResource));

 

//得到子菜单;

CMenu*pPopup=menu.GetSubMenu(nSubMenu);//默认为第一组子菜单;

ASSERT(pPopup!

=NULL);

 

CRectrect;

GetWindowRect(rect);

 

POINTpoint;

//决定弹出菜单的位置;

switch(m_PopupPos)

{

caseCMenuButton:

:

posTopLeft:

//左上角;

point.x=rect.left;

point.y=rect.top;

break;

caseCMenuButton:

:

posBottomLeft:

//左下角;

point.x=rect.left;

point.y=rect.bottom;

break;

caseCMenuButton:

:

posTopRight:

//右上角;

point.x=rect.right;

point.y=rect.top;

break;

caseCMenuButton:

:

posBottomRight:

//右下角;

point.x=rect.right;

point.y=rect.bottom;

break;

default:

ASSERT(FALSE);

}

 

//弹出菜单;

UINTnMenuSel=pPopup->TrackPopupMenu((TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_NONOTIFY|TPM_RETURNCMD),point.x,point.y,this);

pPopup->DestroyMenu();

 

//返回被选择的菜单的ID,如果无任何有效的选择,将返回0;

returnnMenuSel;

}

 

voidCMenuButton:

:

PreSubclassWindow()

{

CButton:

:

PreSubclassWindow();

//默认值:

加入BS_OWNERDRAW风格;

ModifyStyle(0,BS_OWNERDRAW);

}

 

voidCMenuButton:

:

SetArrowType(CMenuButton:

:

ArrowTypetype)

{

m_ArrowType=type;

}

 

voidCMenuButton:

:

SetDrawFocusRect(BOOLbDrawFocusRect)

{

m_bDrawFocusRect=bDrawFocusRect;

}

 

voidCMenuButton:

:

SetMenuPopupPos(CMenuButton:

:

PopupPospos)

{

m_PopupPos=pos;

}

 

voidCMenuButton:

:

SetStyle(BOOLbDrawArrow)

{

if(bDrawArrow)

{

ModifyStyle(0,BS_OWNERDRAW,SWP_NOMOVE|SWP_NOZORDER|SWP_NOSIZE);

}

else

{

ModifyStyle(BS_OWNERDRAW,0,SWP_NOMOVE|SWP_NOZORDER|SWP_NOSIZE);

}

}

要使用这个类,为对话框添加CMenuButton类型的按钮成员变量,若需改变默认风格,则可在OnInitDialog中调用CMenuButton类的公有成员函数SetArrowType()、SetDrawFocusRect()、SetMenuPopupPos()或SetStyle(),在ClassWizard中为对话框添加按钮的BN_CLICKED消息处理函数,然后在其中调用CMenuButton类的OnClick()成员函数,并指定一个菜单ID给它,最后处理OnClick()函数的返回值即可。

下面是一个例子。

 

voidCMenuButtonTestDlg:

:

OnTest()

{

UINTnSel=m_btnTest.OnClick(IDR_POPUP);

switch(nSel)

{

caseID_APP_EXIT:

SendMessage(WM_CLOSE,0,0);

break;

caseID_POPUP_ITEM1:

AfxMessageBox("您选择了第一项!

");

break;

caseID_POPUP_ITEM2:

AfxMessageBox("您选择了第二项!

");

break;

caseID_POPUP_ITEM3:

AfxMessageBox("您选择了第三项!

");

break;

default:

//Donothing;

;

}

}

所用菜单的资源描述如下,外观可参见文首的图。

IDR_POPUPMENUDISCARDABLE

BEGIN

POPUP"_POPUP_"

BEGIN

MENUITEM"第一项",ID_POPUP_ITEM1

MENUITEM"第二项",ID_POPUP_ITEM2

MENUITEM"第三项",ID_POPUP_ITEM3

MENUITEMSEPARATOR

MENUITEM"退出(&X)",ID_APP_EXIT

END

END

 

BOOLCMenuButtonTestDlg:

:

OnInitDialog()

{

CDialog:

:

OnInitDialog();

//因为这几个值都是默认值,所以注释掉,仅为了演示用法;

//m_btnTest.SetDrawFocusRect(TRUE);

//m_btnTest.ArrowType(CMenuButton:

:

arrowRight);

//m_btnTest.SetStyle(TRUE);

//m_btnTest.SetMenuPopupPos(CMenuButton:

:

posBottomLeft);

returnTRUE;

}

值得补充说明的是,使用CMenuButton类的时候,并不一定需要在对话框模板中为按钮指定BS_OWNERDRAW风格,因为在缺省情况下,CMenuButton类的PreSubclassWindow()函数中已经自动加入了这一风格。

搜狗搜索猜测,此时您可能对以下内容感兴趣,点击看看!

关闭关闭提示关闭

确认取消

实验三,计算器

MFC实现简单功能计算器

第一步:

利用MFC的AppWizard生成一个基于对话框的程序(本程序工程名为Calc),切换到Resource,在Dialog中的IDD_CALC_DIALOG上双击,然后添控件按钮,做好外观布局,改好按钮ID和Caption;

第二步:

为控件按钮增加相应的变量和事件响应函数;

第三步:

在CCalcDlg类中添加相应变量;

第四步:

对控件按钮的事件响应函数做处理;

 

代码如下:

 

第一部分(利用ClassWizard添加,可不必手动修改),在CalcDlg.h中的代码修改如下:

//CalcDlg.h:

头文件

//

#pragmaonce

 

//CCalcDlg对话框

classCCalcDlg:

publicCDialog

{

//构造

public:

CCalcDlg(CWnd*pParent=NULL);//标准构造函数

//对话框数据

enum{IDD=IDD_CALC_DIALOG};

protected:

virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDV支持

 

//实现

protected:

HICONm_hIcon;

//生成的消息映射函数

virtualBOOLOnInitDialog();

afx_msgvoidOnSysCommand(UINTnID,LPARAMlParam);

afx_msgvoidOnPaint();

afx_msgHCURSOROnQueryDragIcon();

DECLARE_MESSAGE_MAP()

public:

afx_msgvoidOnBnClickedBtnnum0();

public:

afx_msgvoidOnBnClickedBtnnum1();

public:

afx_msgvoidOnBnClickedBtnnum2();

public:

afx_msgvoidOnBnClickedBtnnum3();

public:

afx_msgvoidOnBnClickedBtnnum4();

public:

afx_msgvoidOnBnClickedBtnnum5();

public:

afx_msgvoidOnBnClickedBtnnum6();

public:

afx_msgvoidOnBnClickedBtnnum7();

public:

afx_msgvoidOnBnClickedBtnnum8();

public:

afx_msgvoidOnBnClickedBtnnum9();

public:

afx_msgvoidOnBnClickedBtnopadd();

public:

afx_msgvoidOnBnClickedBtnopsub();

public:

afx_msgvoidOnBnClickedBtnopmulti();

public:

afx_msgvoidOnBnClickedBtnopdiv();

public:

afx_msgvoidOnBnClickedBtnopequal();

public:

afx_msgvoidOnBnClickedBtnclean();

public:

afx_msgvoidOnBnC

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 求职职场 > 面试

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1