MFC应用程序设计中的常用控件_第1页
MFC应用程序设计中的常用控件_第2页
MFC应用程序设计中的常用控件_第3页
MFC应用程序设计中的常用控件_第4页
MFC应用程序设计中的常用控件_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、安康学院电子与信息工程学院实验报告课程名称VC+高级编程实验名称MFC应用程序设计中的常用控件(一)日期2018.5.29成绩姓名杨 旋学号2015024027班级15级计本1班一、实验目的:掌握利用MFC进行Windows应用程序设计时,常用控件的基本方法。二、实验题目内容及要求:1. 编写一个用于进行成绩计算的程序。具体要求如下:界面中显示五个裁判所给出的等级制成绩(每个裁判利用一组单选按钮进行打分,成绩分为“优秀”、“良好”、“中等”、“及格”、“不及格”的档次),单击界面中的“计算”按钮之后,计算出百分制的平均成绩显示在界面中的一个文本框中,在计算时,每个档次的成绩对应的百分制成绩为:

2、“优秀”95分“良好”85分“中等”75分“及格”65分“不及格”45分要求程序的运行界面自主设计,在完成上述基本功能前提下,可以进一步进行功能扩展。代码#include stdafx.h#include MFCApplication4.h#include MFCApplication4Dlg.h#include afxdialogex.h#ifdef _DEBUG#define new DEBUG_NEW#endifclass CAboutDlg : public CDialogExpublic:CAboutDlg();#ifdef AFX_DESIGN_TIMEenum IDD = IDD

3、_ABOUTBOX ;#endifprotected:virtual void DoDataExchange(CDataExchange* pDX); protected:DECLARE_MESSAGE_MAP();CAboutDlg:CAboutDlg() : CDialogEx(IDD_ABOUTBOX)void CAboutDlg:DoDataExchange(CDataExchange* pDX)CDialogEx:DoDataExchange(pDX);BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)END_MESSAGE_MAP()/ CMFCAppl

4、ication4Dlg 对话框CMFCApplication4Dlg:CMFCApplication4Dlg(CWnd* pParent /*=NULL*/): CDialogEx(IDD_MFCAPPLICATION4_DIALOG, pParent), m_po1(0), m_po2(0), m_po3(0), m_po4(0), m_po5(0), m_result(0)m_hIcon = AfxGetApp()-LoadIcon(IDR_MAINFRAME);void CMFCApplication4Dlg:DoDataExchange(CDataExchange* pDX)CDial

5、ogEx:DoDataExchange(pDX);DDX_Radio(pDX, IDC_RADIO1, m_po1);DDX_Radio(pDX, IDC_RADIO6, m_po2);DDX_Radio(pDX, IDC_RADIO11, m_po3);DDX_Radio(pDX, IDC_RADIO16, m_po4);DDX_Radio(pDX, IDC_RADIO21, m_po5);DDX_Text(pDX, IDC_EDIT1, m_result);BEGIN_MESSAGE_MAP(CMFCApplication4Dlg, CDialogEx)ON_WM_SYSCOMMAND()

6、ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_RADIO1, &CMFCApplication4Dlg:OnBnClickedRadio1)ON_BN_CLICKED(IDC_RADIO6, &CMFCApplication4Dlg:OnBnClickedRadio6)ON_BN_CLICKED(IDC_BUTTON1, &CMFCApplication4Dlg:OnBnClickedButton1)ON_EN_CHANGE(IDC_EDIT1, &CMFCApplication4Dlg:OnEnChangeEdit1)END_MESS

7、AGE_MAP()/ CMFCApplication4Dlg 消息处理程序BOOL CMFCApplication4Dlg:OnInitDialog()CDialogEx:OnInitDialog();ASSERT(IDM_ABOUTBOX & 0xFFF0) = IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX AppendMenu(MF_SEPARATOR);pSysMenu-AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);SetIcon(m_hIcon, TRUE);SetIcon(m_hIcon, FALSE);re

8、turn TRUE; void CMFCApplication4Dlg:OnSysCommand(UINT nID, LPARAM lParam)if (nID & 0xFFF0) = IDM_ABOUTBOX)CAboutDlg dlgAbout;dlgAbout.DoModal();elseCDialogEx:OnSysCommand(nID, lParam);void CMFCApplication4Dlg:OnPaint()if (IsIconic()CPaintDC dc(this); SendMessage(WM_ICONERASEBKGND, reinterpret_cast(d

9、c.GetSafeHdc(), 0);int cxIcon = GetSystemMetrics(SM_CXICON);int cyIcon = GetSystemMetrics(SM_CYICON);CRect rect;GetClientRect(&rect);int x = (rect.Width() - cxIcon + 1) / 2;int y = (rect.Height() - cyIcon + 1) / 2;dc.DrawIcon(x, y, m_hIcon);elseCDialogEx:OnPaint();HCURSOR CMFCApplication4Dlg:OnQuery

10、DragIcon()return static_cast(m_hIcon);int x1,x2,x3,x4,x5;void CMFCApplication4Dlg:OnBnClickedRadio1()switch (m_po1)case 0:x1 = 95;break;case 1:x1 = 85;break;case 2:x1 = 75;break;case 3:x1 = 65;break;case 4:x1 = 45;break;void CMFCApplication4Dlg:OnBnClickedRadio6()switch (m_po2)case 0:x2 = 95;break;c

11、ase 1:x2 = 85;break;case 2:x2 = 75;break;case 3:x2 = 65;break;case 4:x2 = 45;break;void CMFCApplication4Dlg:OnBnClickedButton1()UpdateData(TRUE);switch (m_po1)case 0:x1 = 95;break;case 1:x1 = 85;break;case 2:x1 = 75;break;case 3:x1 = 65;break;case 4:x1 = 45;break;switch (m_po2)case 0:x2 = 95;break;c

12、ase 1:x2 = 85;break;case 2:x2 = 75;break;case 3:x2 = 65;break;case 4:x2 = 45;break;switch (m_po3)case 0:x3 = 95;break;case 1:x3= 85;break;case 2:x3 = 75;break;case 3:x3 = 65;break;case 4:x3 = 45;break;switch (m_po4)case 0:x4 = 95;break;case 1:x4 = 85;break;case 2:x4 = 75;break;case 3:x4 = 65;break;c

13、ase 4:x4 = 45;break;switch (m_po5)case 0:x5 = 95;break;case 1:x5 = 85;break;case 2:x5 = 75;break;case 3:x5 = 65;break;case 4:x5 = 45;break;m_result = (x1 + x2+x3+x4+x5)/5;UpdateData(FALSE);void CMFCApplication4Dlg:OnEnChangeEdit1()运行结果:2.创建一个简单的调色板程序,程序界面中可以利用滚动条进行红、绿、蓝三种颜色分量值的调整,也可以直接在文本框中输入红、绿、蓝三种

14、颜色分量值,在颜色分量值调整变化的同时,变化的颜色效果通过一个圆形色块进行现式。在完成上述基本功能前提下,可以进一步进行功能扩展。代码:#include stdafx.h#include MFCApplication5.h#include MFCApplication5Dlg.h#include afxdialogex.h#ifdef _DEBUG#define new DEBUG_NEW#endifclass CAboutDlg : public CDialogExpublic:CAboutDlg();#ifdef AFX_DESIGN_TIMEenum IDD = IDD_ABOUTBOX

15、 ;#endifprotected:virtual void DoDataExchange(CDataExchange* pDX); protected:DECLARE_MESSAGE_MAP();CAboutDlg:CAboutDlg() : CDialogEx(IDD_ABOUTBOX)void CAboutDlg:DoDataExchange(CDataExchange* pDX)CDialogEx:DoDataExchange(pDX);BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)END_MESSAGE_MAP()CMFCApplication5Dlg

16、:CMFCApplication5Dlg(CWnd* pParent : CDialogEx(IDD_MFCAPPLICATION5_DIALOG, pParent), m_red(0), m_yellow(0), m_blue(0)m_hIcon = AfxGetApp()-LoadIcon(IDR_MAINFRAME);void CMFCApplication5Dlg:DoDataExchange(CDataExchange* pDX)CDialogEx:DoDataExchange(pDX);DDX_Control(pDX, IDC_SCROLLBAR3, m_scr1);DDX_Con

17、trol(pDX, IDC_SCROLLBAR1, m_scr2);DDX_Control(pDX, IDC_SCROLLBAR2, m_scr3);DDX_Text(pDX, IDC_EDIT1, m_red);DDX_Text(pDX, IDC_EDIT2, m_yellow);DDX_Text(pDX, IDC_EDIT3, m_blue);BEGIN_MESSAGE_MAP(CMFCApplication5Dlg, CDialogEx)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_WM_VSCROLL()ON_BN_CLI

18、CKED(IDC_BUTTON1, &CMFCApplication5Dlg:OnBnClickedButton1)ON_BN_CLICKED(IDC_BUTTON2, &CMFCApplication5Dlg:OnBnClickedButton2)END_MESSAGE_MAP()BOOL CMFCApplication5Dlg:OnInitDialog()CDialogEx:OnInitDialog();ASSERT(IDM_ABOUTBOX & 0xFFF0) = IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX AppendMenu(MF_SEPARATOR);pSy

19、sMenu-AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);SetIcon(m_hIcon, TRUE);SetIcon(m_hIcon, FALSE);m_scr1.SetScrollRange(0, 255);m_scr1.SetScrollPos(0);m_scr2.SetScrollRange(0, 255);m_scr2.SetScrollPos(0);m_scr3.SetScrollRange(0, 255);m_scr3.SetScrollPos(0);return TRUE; /void CMFCApplication5Dlg

20、:OnSysCommand(UINT nID, LPARAM lParam)if (nID & 0xFFF0) = IDM_ABOUTBOX)CAboutDlg dlgAbout;dlgAbout.DoModal();elseCDialogEx:OnSysCommand(nID, lParam);void CMFCApplication5Dlg:OnPaint()if (IsIconic()CPaintDC dc(this); SendMessage(WM_ICONERASEBKGND, reinterpret_cast(dc.GetSafeHdc(), 0);int cxIcon = Get

21、SystemMetrics(SM_CXICON);int cyIcon = GetSystemMetrics(SM_CYICON);CRect rect;GetClientRect(&rect);int x = (rect.Width() - cxIcon + 1) / 2;int y = (rect.Height() - cyIcon + 1) / 2;/ 绘制图标dc.DrawIcon(x, y, m_hIcon);elseCDialogEx:OnPaint();HCURSOR CMFCApplication5Dlg:OnQueryDragIcon()return static_cast(

22、m_hIcon);int x1, x2, x3;void CMFCApplication5Dlg:OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)int iNowPos;if (pScrollBar = &m_scr1)switch (nSBCode)case SB_THUMBTRACK:m_scr1.SetScrollPos(nPos);x1 = m_scr1.GetScrollPos();case SB_LINEDOWN:iNowPos = m_scr1.GetScrollPos();iNowPos = iNowPos +

23、 1;if (iNowPos 255)iNowPos = 255;m_scr1.SetScrollPos(iNowPos);x1 = m_scr1.GetScrollPos();break;case SB_LINEUP:iNowPos = m_scr1.GetScrollPos();iNowPos = iNowPos - 1;if (iNowPos 255)iNowPos = 255;m_scr1.SetScrollPos(iNowPos);x1 = m_scr1.GetScrollPos();break;case SB_PAGEUP:iNowPos = m_scr1.GetScrollPos

24、();iNowPos = iNowPos - 3;if (iNowPos 255)iNowPos = 255;m_scr2.SetScrollPos(iNowPos);x2 = m_scr2.GetScrollPos();break;case SB_LINEUP:iNowPos = m_scr2.GetScrollPos();iNowPos = iNowPos - 1;if (iNowPos 255)iNowPos = 255;m_scr2.SetScrollPos(iNowPos);x2 = m_scr2.GetScrollPos();break;case SB_PAGEUP:iNowPos

25、 = m_scr1.GetScrollPos();iNowPos = iNowPos - 3;if (iNowPos 255)iNowPos = 255;m_scr3.SetScrollPos(iNowPos);x3 = m_scr3.GetScrollPos();break;case SB_LINEUP:iNowPos = m_scr3.GetScrollPos();iNowPos = iNowPos - 1;if (iNowPos 255)iNowPos = 255;m_scr3.SetScrollPos(iNowPos);x3 = m_scr3.GetScrollPos();break;

26、case SB_PAGEUP:iNowPos = m_scr3.GetScrollPos();iNowPos = iNowPos - 3;if (iNowPos LoadIcon(IDR_MAINFRAME);void CMFCApplication6Dlg:DoDataExchange(CDataExchange* pDX)CDialogEx:DoDataExchange(pDX);DDX_Radio(pDX, IDC_RADIO1, m_PenStyle);DDX_Radio(pDX, IDC_RADIO5, m_BrushStyle);DDX_Control(pDX, IDC_COMBO

27、1, m_PenColor);DDX_Control(pDX, IDC_COMBO2, m_BrushColor);DDX_Control(pDX, IDC_LIST1, m_List);BEGIN_MESSAGE_MAP(CMFCApplication6Dlg, CDialogEx)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_RADIO1, &CMFCApplication6Dlg:OnBnClickedRadio1)ON_BN_CLICKED(IDC_RADIO5, &CMFCApplicati

28、on6Dlg:OnBnClickedRadio5)ON_CBN_SELCHANGE(IDC_COMBO1, &CMFCApplication6Dlg:OnCbnSelchangeCombo1)ON_CBN_SELCHANGE(IDC_COMBO2, &CMFCApplication6Dlg:OnCbnSelchangeCombo2)ON_BN_CLICKED(IDC_BUTTON1, &CMFCApplication6Dlg:OnBnClickedButton1)ON_LBN_SELCHANGE(IDC_LIST1, &CMFCApplication6Dlg:OnLbnSelchangeLis

29、t1)END_MESSAGE_MAP()BOOL CMFCApplication6Dlg:OnInitDialog()CDialogEx:OnInitDialog();ASSERT(IDM_ABOUTBOX & 0xFFF0) = IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX AppendMenu(MF_SEPARATOR);pSysMenu-AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);SetIcon(m_hIcon, TRUE);SetIcon(m_hIcon, FALSE);m_PenColor.AddStrin

30、g(LRed);m_PenColor.AddString(LYellow);m_PenColor.AddString(LBlue);m_PenColor.SelectString(-1,LRED);m_BrushColor.AddString(LRed);m_BrushColor.AddString(LYellow);m_BrushColor.AddString(LBlue);m_BrushColor.SelectString(-1, LRED);m_List.AddString(LLine);m_List.AddString(LCircle);m_List.AddString(LRectan

31、gle);m_List.AddString(LRoundRectangle);m_List.SelectString(-1, LLine);return TRUE; void CMFCApplication6Dlg:OnSysCommand(UINT nID, LPARAM lParam)if (nID & 0xFFF0) = IDM_ABOUTBOX)CAboutDlg dlgAbout;dlgAbout.DoModal();elseCDialogEx:OnSysCommand(nID, lParam);void CMFCApplication6Dlg:OnPaint()if (IsIcon

32、ic()CPaintDC dc(this); SendMessage(WM_ICONERASEBKGND, reinterpret_cast(dc.GetSafeHdc(), 0); int cxIcon = GetSystemMetrics(SM_CXICON);int cyIcon = GetSystemMetrics(SM_CYICON);CRect rect;GetClientRect(&rect);int x = (rect.Width() - cxIcon + 1) / 2;int y = (rect.Height() - cyIcon + 1) / 2;dc.DrawIcon(x

33、, y, m_hIcon);elseCDialogEx:OnPaint();HCURSOR CMFCApplication6Dlg:OnQueryDragIcon()return static_cast(m_hIcon);int m_penstyle, m_brushstyle;COLORREF m_pencolor, m_brushcolor;void CMFCApplication6Dlg:OnBnClickedRadio1()switch (m_PenStyle)case 0:m_penstyle = PS_SOLID;break;case 1:m_penstyle = PS_DASH;

34、break;case 2:m_penstyle = PS_DOT;break;case 3:m_penstyle = PS_DASHDOT;break;void CMFCApplication6Dlg:OnBnClickedRadio5()switch (m_BrushStyle)case 0:m_brushstyle = PS_SOLID;break;case 1:m_brushstyle = HS_CROSS;break;case 2:m_brushstyle = HS_FDIAGONAL;break;case 3:m_brushstyle = HS_BDIAGONAL;break;voi

35、d CMFCApplication6Dlg:OnCbnSelchangeCombo1()int i;i = m_PenColor.GetCurSel();if (i = 0)m_pencolor = RGB(255, 0, 0);else if(i = 1)m_pencolor = RGB(0, 255, 0);else if(i = 2)m_pencolor = RGB(0, 0, 255);void CMFCApplication6Dlg:OnCbnSelchangeCombo2()int i;i = m_BrushColor.GetCurSel();if (i = 0)m_brushcolor = RGB(255, 0, 0);else if

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论