《MFC应用程序设计中的常用控件(共15页).doc》由会员分享,可在线阅读,更多相关《MFC应用程序设计中的常用控件(共15页).doc(15页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精选优质文档-倾情为你奉上安康学院电子与信息工程学院实验报告课程名称VC+高级编程实验名称MFC应用程序设计中的常用控件(一)日期2018.5.29成绩姓名杨 旋学号班级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_TIMEenu
3、m 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(
4、)/ 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
5、* 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_S
6、YSCOMMAND()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:OnEnChangeEdi
7、t1)END_MESSAGE_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
8、, 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, reinter
9、pret_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, y, m_hIcon);elseCDialogEx:OnPaint();HCURSOR CMFCApplication4
10、Dlg:OnQueryDragIcon()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 =
11、 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 =
12、 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 =
13、 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.创建一个简单的调色板程序,程序界面中可以利用滚动条进行红、绿、蓝三种颜色分量值的调整,也可以直接在文本
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 = I
15、DD_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
16、ication5Dlg: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_sc
17、r1);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
18、()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 AppendMenu(MF_SEP
19、ARATOR);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 CMFCAppl
20、ication5Dlg: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 c
21、xIcon = 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 s
22、tatic_cast(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
23、= 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 255)iNowPos = 255;m_scr1.SetScrollPos(iNowPos);x1 = m_scr1.GetScrollPos();break;case SB_PAGEUP:iNowPos = m_scr1.G
24、etScrollPos();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_PAG
25、EUP:iNowPos = 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.GetScrollP
26、os();break;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
27、, 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, &CM
28、FCApplication6Dlg: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:OnLbnS
29、elchangeList1)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.AddString(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(LBlu
限制150内