《实验七MFC控件编程实验》由会员分享,可在线阅读,更多相关《实验七MFC控件编程实验(9页珍藏版)》请在金锄头文库上搜索。
1、实验七-MFC扌空件编程实验实验七MFC控件编程实验一、实验目的 熟悉Visual C+ 6.0开发环境;(2)掌握MFC环境下标准控件的使用方法;熟练掌握在Visual C+ 6.0开发环境调试程序的方法。二、实验内容创建如图所示的应用程序,在“形状”列表框中选择要绘制的图形,在“笔颜 色”下拉列表框中选择画笔的颜色,在“刷子颜色”下拉列表框中选择画刷的 颜色,在“线型”组框中选择画笔的线型,在“填充类型”中选择画刷填充类型,单 击“绘图”按钮在“绘图设置”编辑框中显示选择的绘图设置选项,单击“退出”按钮退 出程序。三、程序代码/ testDIg.cpp : impleme ntati on
2、 file/#include stdafx.h#in clude test.h#in clude testDlg.h#ifdef _DEBUG#defi ne new DEBUG_NEW#un def THIS_FILEstatic char THIS_FILE = _FILE_;#en dif/ CAboutDlg dialog used for App Aboutclass CAboutDIg : public CDialogpublic:CAboutDlg();/ Dialog DataAFX_DATA(CAboutDlg)enum IDD = IDD_ABOUTBOX ;AFX_DAT
3、A/ ClassWizard gen erated virtual fun ctio n overridesAFX_VIRTUAL(CAboutDlg)/ DDX/DDVprotected:virtual void DoDataExcha nge(CDataExcha nge* pDX); supportAFX_VIRTUAL/ Impleme ntati onprotected:AFX_MSG(CAboutDlg)AFX_MSG DECLARE_MESSAGE_MAP();CAboutDlg:CAboutDlg() : CDialog(CAboutDlg:IDD)AFX_DATA_INIT(
4、CAboutDlg) AFX_DATA_INITvoid CAboutDlg:DoDataExcha nge(CDataExcha nge* pDX) CDialog:DoDataExcha nge(pDX);AFX_DATA_MAP(CAboutDlg) AFX_DATA_MAPBEGIN_MESSAGE_MAP(CAboutDlg, CDialog) AFX_MSG_MAP(CAboutDlg)/ No message han dlersAFX_MSG_MAPEND_MESSAGE_MAP()/ CTestDIg dialogCTestDlg:CTestDlg(CWnd* pPare nt
5、 /*=NULL*/):CDialog(CTestDlg:IDD, pPare nt)AFX_DATA_INIT(CTestDlg)m_pe n = -1;m_brush = -1; m_Output = _T();AFX_DATA_INIT/ Note that LoadIc on does not require a subseque nt DestroyIc on in Win 32m_hlco n = AfxGetApp()-LoadIco n( IDR_MAINFRAME);void CTestDlg:DoDataExcha nge(CDataExcha nge* pDX)CDial
6、og:DoDataExcha nge(pDX); AFX_DATA_MAP(CTestDlg) DDX_Co ntrol(pDX, IDC_LIST1, mst);DDX_Co ntrol(pDX, IDC_COMBO2, m_cb2);DDX_Co ntrol(pDX, IDC_COMBO1, m_cb1);DDX_Radio(pDX, IDC_RADIO1, m_pe n);DDX_Radio(pDX, IDC_RADIO5, m_brush);DDX_Text(pDX, IDC_OUTPUT, m_Output); /AFX_DATA_MAPBEGIN_MESSAGE_MAP(CTest
7、Dlg, CDialog) AFX_MSG_MAP(CTestDlg) ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_EN_CHANGE(IDC_OUTPUT, On Cha ngeOutput)ON_BN_CLICKED(IDC_BUTTON1, On Button1)ON_LBN_SELCHANGE(IDC_LIST1, On Selcha ngeList1)ON_CBN_SELCHANGE(IDC_COMBO1, On Selcha ngeCombo1)ON_CBN_SELCHANGE(IDC_COMBO2, On Selc
8、ha ngeCombo2)ON_CBN_EDITCHANGE(IDC_COMBO2, On Selcha ngeCombo2) /AFX_MSG_MAPEND_MESSAGE_MAP()/ CTestDIg message han dlersBOOL CTestDlg:O nln itDialog()CDialog:O nlni tDialog();/ Add About. menu item to system menu./ IDM_ABOUTBOX must be in the system comma nd ran ge.ASSERT(IDM_ABOUTBOX & OxFFFO) = I
9、DM_ABOUTBOX);ASSERT(IDM_ABOUTBOX Appe ndMe nu(MF_SEPARATOR);pSysMe nu-Appe ndMe nu(MF_STRING, IDM_ABOUTBOX, strAboutMe nu);/ Set the icon for this dialog.The framework does this automatically/ when the applications main window is not a dialogSetIco n( m_hIco n, TRUE);/ Set big iconSetIc on( m_hIco n
10、, FALSE);/ Set small icon/ TODO: Add extra in itializati on herem_list.AddStri ng(Li ne);m_list.AddStr in g(Circle);m_list.AddStri ng(Recta ngle);m_list.AddStri ng(Rou ndRecta ngle);m_cb1 AddStri ng(Red);m_cb1 AddStri ng(Blue);m_cb1 AddStri ng(Black);return TRUE; / return TRUE uni ess you set the fo
11、cus to a con trol void CTestDlg:O nSysComma nd(UINT nID, LPARAM lParam)if (n ID & OxFFFO) = IDM_ABOUTBOX)CAboutDIg dlgAbout; dlgAbout.DoModal();elseCDialog:O nSysComma nd( nID, IParam);/ If you add a mi ni mize butt on to your dialog, you will n eed the code below/ to draw the icon. For MFC applicat
12、i ons using the docume nt/view model,/ this is automatically done for you by the framework.void CTestDlg:O nPai nt()if (IsIco nic()CPai ntDC dc(this); / device con text for pai nti ngSen dMessage(WMCONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);/ Cen ter icon in clie nt recta nglein t cxIcon = GetSyste
13、mMetrics(SM_CXICON);in t cyIcon = GetSystemMetrics(SM_CYICON);CRect rect;GetClie ntRect(&rect);int x = (rect.Width() - cxIcon + 1) / 2;int y = (rect.Height() - cyIcon + 1) / 2;/ Draw the icondc.DrawIc on(x, y, m_hlc on);elseCDialog:O nPai nt();/ The system calls this to obtain the cursor to display
14、while the user drags / the mini mized wi ndow.HCURSOR CTestDlg:O nQueryDraglco n()return (HCURSOR) m_hIco n;void CTestDlg:O nCha ngeOutput()/ TODO: If this is a RICHEDIT con trol, the control will not/ send this no tificati on uni ess you override theCDialog:O nlni tDialog()/ fun ctio n and call CRichEditCtrl().SetEve ntMask()/ with the ENM_CHANGE flag ORed into the mask./ TODO: Add your contro