MFC应用程序设计中的常用控件(共15页).doc
精选优质文档-倾情为你奉上安康学院电子与信息工程学院实验报告课程名称VC+高级编程实验名称MFC应用程序设计中的常用控件(一)日期2018.5.29成绩姓名杨 旋学号班级15级计本1班一、实验目的:掌握利用MFC进行Windows应用程序设计时,常用控件的基本方法。二、实验题目内容及要求:1. 编写一个用于进行成绩计算的程序。具体要求如下:界面中显示五个裁判所给出的等级制成绩(每个裁判利用一组单选按钮进行打分,成绩分为“优秀”、“良好”、“中等”、“及格”、“不及格”的档次),单击界面中的“计算”按钮之后,计算出百分制的平均成绩显示在界面中的一个文本框中,在计算时,每个档次的成绩对应的百分制成绩为:“优秀”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_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()/ CMFCApplication4Dlg 对话框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)CDialogEx: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()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_MESSAGE_MAP()/ CMFCApplication4Dlg 消息处理程序BOOL CMFCApplication4Dlg:OnInitDialog()CDialogEx:OnInitDialog();ASSERT(IDM_ABOUTBOX & 0xFFF0) = IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX < 0xF000);CMenu* pSysMenu = GetSystemMenu(FALSE);if (pSysMenu != NULL)BOOL bNameValid;CString strAboutMenu;bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);ASSERT(bNameValid);if (!strAboutMenu.IsEmpty()pSysMenu->AppendMenu(MF_SEPARATOR);pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);SetIcon(m_hIcon, TRUE);SetIcon(m_hIcon, FALSE);return 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<WPARAM>(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, y, m_hIcon);elseCDialogEx:OnPaint();HCURSOR CMFCApplication4Dlg:OnQueryDragIcon()return static_cast<HCURSOR>(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;case 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;case 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;case 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.创建一个简单的调色板程序,程序界面中可以利用滚动条进行红、绿、蓝三种颜色分量值的调整,也可以直接在文本框中输入红、绿、蓝三种颜色分量值,在颜色分量值调整变化的同时,变化的颜色效果通过一个圆形色块进行现式。在完成上述基本功能前提下,可以进一步进行功能扩展。代码:#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 ;#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: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_Control(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_CLICKED(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 < 0xF000);CMenu* pSysMenu = GetSystemMenu(FALSE);if (pSysMenu != NULL)BOOL bNameValid;CString strAboutMenu;bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);ASSERT(bNameValid);if (!strAboutMenu.IsEmpty()pSysMenu->AppendMenu(MF_SEPARATOR);pSysMenu->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: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<WPARAM>(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, y, m_hIcon);elseCDialogEx:OnPaint();HCURSOR CMFCApplication5Dlg:OnQueryDragIcon()return static_cast<HCURSOR>(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 + 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 < 0)iNowPos = 0;m_scr1.SetScrollPos(iNowPos);x1 = m_scr1.GetScrollPos();break;case SB_PAGEDOWN:iNowPos = m_scr1.GetScrollPos();iNowPos = iNowPos + 3;if (iNowPos > 255)iNowPos = 255;m_scr1.SetScrollPos(iNowPos);x1 = m_scr1.GetScrollPos();break;case SB_PAGEUP:iNowPos = m_scr1.GetScrollPos();iNowPos = iNowPos - 3;if (iNowPos < 0)iNowPos = 0;m_scr1.SetScrollPos(iNowPos);x1 = m_scr1.GetScrollPos();break; else if (pScrollBar = &m_scr2)switch (nSBCode)case SB_THUMBTRACK:m_scr2.SetScrollPos(nPos);x2 = m_scr2.GetScrollPos();case SB_LINEDOWN:iNowPos = m_scr2.GetScrollPos();iNowPos = iNowPos + 1;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 < 0)iNowPos = 0;m_scr2.SetScrollPos(iNowPos);x2 = m_scr2.GetScrollPos();break;case SB_PAGEDOWN:iNowPos = m_scr2.GetScrollPos();iNowPos = iNowPos + 3;if (iNowPos > 255)iNowPos = 255;m_scr2.SetScrollPos(iNowPos);x2 = m_scr2.GetScrollPos();break;case SB_PAGEUP:iNowPos = m_scr1.GetScrollPos();iNowPos = iNowPos - 3;if (iNowPos < 0)iNowPos = 0;m_scr2.SetScrollPos(iNowPos);x2 = m_scr2.GetScrollPos();break;else if (pScrollBar = &m_scr3)switch (nSBCode)case SB_THUMBTRACK:m_scr3.SetScrollPos(nPos);x3 = m_scr3.GetScrollPos();case SB_LINEDOWN:iNowPos = m_scr3.GetScrollPos();iNowPos = iNowPos + 1;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 < 0)iNowPos = 0;m_scr3.SetScrollPos(iNowPos);x3 = m_scr3.GetScrollPos();break;case SB_PAGEDOWN:iNowPos = m_scr3.GetScrollPos();iNowPos = iNowPos + 3;if (iNowPos > 255)iNowPos = 255;m_scr3.SetScrollPos(iNowPos);x3 = m_scr3.GetScrollPos();break;case SB_PAGEUP:iNowPos = m_scr3.GetScrollPos();iNowPos = iNowPos - 3;if (iNowPos < 0)iNowPos = 0;m_scr3.SetScrollPos(iNowPos);x3 = m_scr3.GetScrollPos();break;CDialogEx:OnVScroll(nSBCode, nPos, pScrollBar);void CMFCApplication5Dlg:OnBnClickedButton1()/ TODO: 在此添加控件通知处理程序代码CClientDC hDC(this);CPen *penold, pennew;CBrush *brushold, brushnew, brushback;CRect rectclient;GetClientRect(&rectclient); CRectrectdraw(rectclient.right - 170, 10, rectclient.right - 10, 170);brushback.CreateSolidBrush(RGB(x1, x2, x3);brushold = hDC.SelectObject(&brushback);penold = hDC.SelectObject(&pennew);hDC.SelectObject(&brushnew);hDC.Ellipse(rectdraw);hDC.SelectObject(penold);hDC.SelectObject(brushold);void CMFCApplication5Dlg:OnBnClickedButton2()/ TODO: 在此添加控件通知处理程序代码UpdateData(TRUE);CClientDC hDC(this);CPen *penold, pennew;CBrush *brushold, brushnew, brushback;CRect rectclient;GetClientRect(&rectclient);CRectrectdraw(rectclient.right - 170, 10, rectclient.right - 10, 170);brushback.CreateSolidBrush(RGB(m_red, m_yellow, m_blue);brushold = hDC.SelectObject(&brushback);penold = hDC.SelectObject(&pennew);hDC.SelectObject(&brushnew);hDC.Ellipse(rectdraw);hDC.SelectObject(penold);hDC.SelectObject(brushold);UpdateData(FALSE);运行结果:3.完成教材第255页习题9-17。代码#include "stdafx.h"#include "MFCApplication6.h"#include "MFCApplication6Dlg.h"#include "afxdialogex.h"#ifdef _DEBUG#define new DEBUG_NEW#endifclass CAboutDlg : public CDialogExpublic:CAboutDlg();#ifdef AFX_DESIGN_TIMEenum IDD = IDD_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()CMFCApplication6Dlg:CMFCApplication6Dlg(CWnd* pParent CDialogEx(IDD_MFCAPPLICATION6_DIALOG, pParent), m_PenStyle(0), m_BrushStyle(0)m_hIcon = AfxGetApp()->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_COMBO1, 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, &CMFCApplication6Dlg: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:OnLbnSelchangeList1)END_MESSAGE_MAP()BOOL CMFCApplication6Dlg:OnInitDialog()CDialogEx:OnInitDialog();ASSERT(IDM_ABOUTBOX & 0xFFF0) = IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX < 0xF000);CMenu* pSysMenu = GetSystemMenu(FALSE);if (pSysMenu != NULL)BOOL bNameValid;CString strAboutMenu;bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);ASSERT(bNameValid);if (!strAboutMenu.IsEmpty()pSysMenu->AppendMenu(MF_SEPARATOR);pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);SetIcon(m_hIcon, TRUE);SetIcon(m_hIcon, FALSE);m_PenColor.AddString(L"Red");m_PenColor.AddString(L"Yellow");m_PenColor.AddString(L"Blue");m_PenColor.SelectString(-1,L"RED");m_BrushColor.AddString(L"Red");m_BrushColor.AddString(L"Yellow");m_BrushColor.AddString(L"Blu