实验七-MFC-控件编程实验
. . . . 实验七 MFC 控件编程实验一、实验目的(1) 熟悉Visual C++ 6.0 开发环境;(2) 掌握MFC 环境下标准控件的使用方法;(3) 熟练掌握在Visual C++ 6.0 开发环境调试程序的方法二、实验容创建如下图的应用程序,在“形状”列表框中选择要绘制的图形,在“笔颜色”下拉列表框中选择画笔的颜色,在“刷子颜色”下拉列表框中选择画刷的颜色,在“线型”组框中选择画笔的线型,在“填充类型”中选择画刷填充类型,单击“绘图”按钮在“绘图设置”编辑框中显示选择的绘图设置选项,单击“退出”按钮退出程序三、程序代码// testDlg.cpp : implementation file//#include "stdafx.h"#include "test.h"#include "testDlg.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif// CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog{public: CAboutDlg();// Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL// Implementationprotected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP()};CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD){ //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT}void CAboutDlg::DoDataExchange(CDataExchange* pDX){ CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CTestDlg dialogCTestDlg::CTestDlg(CWnd* pParent /*=NULL*/) : CDialog(CTestDlg::IDD, pParent){ //{{AFX_DATA_INIT(CTestDlg) m_pen = -1; m_brush = -1; m_Output = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);}void CTestDlg::DoDataExchange(CDataExchange* pDX){ CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CTestDlg) DDX_Control(pDX, IDC_LIST1, m_list); DDX_Control(pDX, IDC_COMBO2, m_cb2); DDX_Control(pDX, IDC_COMBO1, m_cb1); DDX_Radio(pDX, IDC_RADIO1, m_pen); DDX_Radio(pDX, IDC_RADIO5, m_brush); DDX_Text(pDX, IDC_OUTPUT, m_Output); //}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CTestDlg, CDialog) //{{AFX_MSG_MAP(CTestDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_EN_CHANGE(IDC_OUTPUT, OnChangeOutput) ON_BN_CLICKED(IDC_BUTTON1, OnButton1) ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1) ON_CBN_SELCHANGE(IDC_COMBO1, OnSelchangeCombo1) ON_CBN_SELCHANGE(IDC_COMBO2, OnSelchangeCombo2) ON_CBN_EDITCHANGE(IDC_COMBO2, OnSelchangeCombo2) //}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CTestDlg message handlersBOOL CTestDlg::OnInitDialog(){ CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here m_list.AddString("Line"); m_list.AddString("Circle"); m_list.AddString("Rectangle"); m_list.AddString("RoundRectangle"); m_cb1.AddString("Red"); m_cb1.AddString("Blue"); m_cb1.AddString("Black"); return TRUE; // return TRUE unless you set the focus to a control}void CTestDlg::OnSysCommand(UINT nID, LPARAM lParam){ if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); }}// If you add a minimize button to your dialog, you will need the code below// to draw the icon. For MFC applications using the document/view model,// this is automatically done for you by the framework.void CTestDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle 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; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); }}// The system calls this to obtain the cursor to display while the user drags// the minimized window.HCURSOR CTestDlg::OnQueryDragIcon(){ return (HCURSOR) m_hIcon;}void CTestDlg::OnChangeOutput() { // TODO: If this is a RICHEDIT control, the control will not // send this notification unless you override the CDialog::OnInitDialog() // function and call CRichEditCtrl().SetEventMask() // with the ENM_CHANGE flag ORed into the mask. // TODO: Add your control notification handler code here}CString msg1;CString msg2; CString msg3;void CTestDlg::OnSelchangeList1() { // TODO: Add your control notification handler code here m_list.GetText(m_list.GetCurSel(),msg1);}void CTestDlg::OnSelchangeCombo1() { // TODO: Add your control notification handler code here m_cb1.GetLBText(m_cb1.GetCurSel(),msg2);}void CTestDlg::OnSelchangeCombo2() { // TODO: Add your control notification handler code here m_cb2.GetLBText(m_cb2.GetCurSel(),msg3);}void CTestDlg::OnButton1() { // TODO: Add your control notification handler code here UpdateData(TRUE); // DDX:界面控件的值传给部变量m_Output="形状:";m_Output+=msg1;m_Output+=" 笔颜色:";m_Output+=msg2;m_Output+=" 刷子颜色:";m_Output+=msg3;m_Output += " 线型:"; switch(m_pen) { case 0: m_Output += "Solid"; break;case 1: m_Output += "Dash"; break;case 2: m_Output += "Dot"; break;case 3: m_Output += "DashDot"; break;}m_Output += " 填充类型:";switch(m_brush){case 0: m_Output += "SolidBrush";break;case 1: m_Output += "Cross";break; case 2: m_Output += "FDiagonal"; break;case 3: m_Output += "BDiagonal"; break;}UpdateData(FALSE); // DDX:部变量的值传给界面控件}四、运行结果8 / 8。




