




已阅读5页,还剩26页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
6.6.2创建应用程序的过程1使用MFC AppWizard创建应用程序框架工程名是pop3,应用程序的类型是基于对话框的,对话框的标题是“接收电子邮件客户端程序”,需要Windows Sockets的支持,其它部分接受系统的默认设置就可以。应用程序包括两个类:应用程序类:CPop3App,基类是CWinApp,对应的文件是pop3.h和pop3.cpp。对话框类:CPop3Dlg,基类是CDialog,对应的文件是pop3Dlg.h和pop3Dlg.cpp。2为对话框添加控件在程序的主对话框界面中按照图6-13添加相应的可视控件对象,并按照表6-13修改控件的属性。表6-13 对话框中的控件属性控件类型控件IDCaption静态文本 static textIDC_STATIC服务器地址静态文本 static textIDC_STATIC用户名静态文本 static textIDC_STATIC密码编辑框 edit boxIDC_SERVER编辑框 edit boxIDC_USER编辑框 edit boxIDC_PASSCHECK BOXIDC_DEL是否删除邮件RichEditIDC_INFO命令按钮 buttonIDC_CONN连接命令按钮 buttonIDC_VIEW查看邮件命令按钮 buttonIDC_DISC断开命令按钮 buttonIDCANCAL退出CChooseDlgCComboBoxIDC_MSGLISTCViewDlg编辑框 edit boxIDC_MSGTEXT命令按钮 buttonIDC_SAVE存储3定义控件的成员变量按照表6-14,用类向导(Class Wizard)为对话框中的控件对象定义相应的成员变量。表6-14 控件对象的成员变量控件IDControl IDs变量名称Member Variable Name变量类别Category变量类型Variable TypeIDC_SERVERserverValueCStringIDC_USERuserValueCStringIDC_PASSpassValueCStringIDC_DELdelValueBOOLIDC_INFOinfoValueCStringctllnfoControlCRichEditCtrlCChooseDlgIDC_MSGLISTctlListControlCComboBoxCViewDlgIDC_MSGTEXTtextValueCString4添加成员变量的初始化代码在FtpDlg.cpp文件的OnInitDialog()函数中添加成员变量的初始化代码。对服务器名,登录用户名,登录口令的控件变量赋初值。BOOL CFtpDlg:OnInitDialog(). / 前面是MFC应用程序向导和类向导自动生成的代码/ TODO: Add extra initialization herem_strFtp=_T(); / 初始化服务器域名m_strName=_T(); / 初始化登录用户名m_strPwd=_T(); / 初始化登录口令UpdateData(FALSE); /更新界面return TRUE; / return TRUE unless you set the focus to a control5为对话框中的控件对象添加事件响应函数按照表6-15,用类向导(Class Wizard )为对话框中的控件对象添加事件响应函数。表6-15 对话框控件的事件响应函数控件类型对象标识 ObjectID消息 Message函数Member functions命令按钮IDC_CONNBN_CLICKEDOnConn命令按钮IDC_VIEWBN_CLICKEDOnView命令按钮IDC_DISCBN_CLICKEDOnDiscCChooseDlg命令按钮IDOKBN_CLICKEDOnOKCViewDlg命令按钮IDC_SAVEBN_CLICKEDOnSave6为CFtpDlg类添加其它的成员函数BOOL CFtpDlg: Download (CString strSName, CString strDName);BOOL CFtpDlg: Upload (CString strSName, CString strDName);分别用于文件的下载和上传。7手工添加包含语句在CFtpDlg类的FtpDlg.cpp文件中添加对于Afxinet.h的包含命令,来获得对于MFC WinInet类的支持。8添加事件函数和成员函数的代码9进行测试4创建从CAsyncSocket类继承的派生类(1)为了能够捕获并响应socket事件,应创建用户自己的套接字类,它应当从CAsyncSocket类派生,还能将套接字事件传递给对话框,以便执行用户自己的事件处理函数。选择菜单“插入/新建类”,进入“New Class”对话框,如图5-12所示。图5-12 添加自己的套接字类选择或输入以下信息:Class Type:选择MFC ClassClass Infoumation下的Name: 输入mySockClass Infoumation下的Base class:选择CAsyncSocket点击“OK”按钮,系统会自动生成CMySocket类对应的包含文件mySock.h和mySock.cpp文件,在VC界面的Class View中就可以看到这个类。(2)利用类向导ClassWizard为这个套接字类添加响应消息的事件处理成员函数。点菜单View/ClassWizard.,进入类向导对话框,选择Message Maps(消息映射)卡,确认Class name是mySock,从Messages(消息)栏中选择事件消息,然后点击Add Function按钮,就会看到在Member Function栏中添加了相应的事件处理函数。如图5-13所示,此程序中需要添加OnConnect,OnClose和OnReceive三个函数。这一步会在CMySocket类的mySock.h中自动生成这些函数的声明,在mySock.cpp中生成这些函数的框架,以及消息映射的相关代码。可参看后面的程序清单。图5-13 为套接字类添加响应消息的事件处理成员函数(3)为套接字类添加一般的成员函数和成员变量在VC+的界面中,在工作区窗口选择ClassView卡,用右键单击CMySocket类,会弹出快捷菜单,选择其中的Add Member Function 可以为该类添加成员函数;选择Add Member Variable可以为该类添加成员变量。如图5-14所示。图5-15和图5-16是添加操作的对话框。图5-14 为指定的类添加成员变量或成员函数对这个套接字类,添加一个私有的成员变量,是一个对话框类的指针。private:CPop3Dlg * m_pDlg;图5-15 为套接字类添加一般的成员变量再添加一个成员函数:void SetParent(CPop3Dlg * pDlg);图5-16 为套接字类添加一般的成员函数这一步同样会在mySock.h中生成变量或函数的声明,在mySock.cpp中生成函数的框架代码。如果熟悉的话,这一步的代码也可以直接手工添加。(4)手工添加其他代码在VC+的界面中,在工作区窗口选择FileView卡,双击要编辑的文件,在右面的窗口中就会展示该文件的代码,可以编辑添加。对于mySock.h,应在文件开头,添加对于此应用程序对话框类的声明。class CPop3Dlg;对于mySock.cpp,有四处添加: 应在文件开头,添加包含文件说明。这是因为此套接字类用到了对话框类的变量。#include “pop3Dlg.h” 在构造函数中,添加对于对话框指针成员变量的初始化代码:mySock:mySock() m_pDlg = NULL; 在析构函数中,添加对于对话框指针成员变量的初始化代码:mySock:mySock() m_pDlg = NULL; 为成员函数setParent和事件处理函数OnConnect,OnClose和OnReceive添加代码。详见后面的程序清单。/ pop3.h : main header file for the POP3 application#if !defined(AFX_POP3_H_INCLUDED_)#define AFX_POP3_H_INCLUDED_#if _MSC_VER 1000#pragma once#endif / _MSC_VER 1000#ifndef _AFXWIN_H_#error include stdafx.h before including this file for PCH#endif#include resource.h / main symbols/ CPop3App:/ See pop3.cpp for the implementation of this classclass CPop3App : public CWinApppublic:CPop3App();/ Overrides/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CPop3App)public:virtual BOOL InitInstance();/AFX_VIRTUAL/ Implementation/AFX_MSG(CPop3App)/ NOTE - the ClassWizard will add and remove member functions here./ DO NOT EDIT what you see in these blocks of generated code !/AFX_MSGDECLARE_MESSAGE_MAP();/AFX_INSERT_LOCATION/ Microsoft Visual C+ will insert additional declarations immediately before the previous line.#endif / !defined(AFX_POP3_H_INCLUDED_)/ pop3.cpp : Defines the class behaviors for the application.#include stdafx.h#include pop3.h#include pop3Dlg.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CPop3AppBEGIN_MESSAGE_MAP(CPop3App, CWinApp)/AFX_MSG_MAP(CPop3App)/ NOTE - the ClassWizard will add and remove mapping macros here./ DO NOT EDIT what you see in these blocks of generated code!/AFX_MSGON_COMMAND(ID_HELP, CWinApp:OnHelp)END_MESSAGE_MAP()/ CPop3App constructionCPop3App:CPop3App()/ TODO: add construction code here,/ Place all significant initialization in InitInstance/ The one and only CPop3App objectCPop3App theApp;/ CPop3App initializationBOOL CPop3App:InitInstance()if (!AfxSocketInit()AfxMessageBox(IDP_SOCKETS_INIT_FAILED);return FALSE;AfxEnableControlContainer(); /MFC自动创建的AfxInitRichEdit(); /自己加的/ Standard initialization/ If you are not using these features and wish to reduce the size/ of your final executable, you should remove from the following/ the specific initialization routines you do not need.#ifdef _AFXDLLEnable3dControls(); / Call this when using MFC in a shared DLL#elseEnable3dControlsStatic(); / Call this when linking to MFC statically#endifCPop3Dlg dlg;m_pMainWnd = &dlg;int nResponse = dlg.DoModal();if (nResponse = IDOK)/ TODO: Place code here to handle when the dialog is/ dismissed with OKelse if (nResponse = IDCANCEL)/ TODO: Place code here to handle when the dialog is/ dismissed with Cancel/ Since the dialog has been closed, return FALSE so that we exit the/ application, rather than start the applications message pump.return FALSE;/ pop3Dlg.h : header file#if !defined(AFX_POP3DLG_H_INCLUDED_)#define AFX_POP3DLG_H _INCLUDED_#include pop31.h#include resource.h#if _MSC_VER 1000#pragma once#endif / _MSC_VER 1000/ CPop3Dlg dialogclass CPop3Dlg : public CDialog/ Construction public:CPop3 pop3;void Dispatch(LONG param);CPop3Dlg(CWnd* pParent = NULL);/ standard constructor/ Dialog Data/AFX_DATA(CPop3Dlg)enum IDD = IDD_POP3_DIALOG ;CRichEditCtrl ctlInfo;CString info;CString pass;CString server;CString user;BOOL del;/AFX_DATA/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CPop3Dlg)protected:virtual void DoDataExchange(CDataExchange* pDX); / DDX/DDV support/AFX_VIRTUAL/ Implementationprotected:HICON m_hIcon;/ Generated message map functions/AFX_MSG(CPop3Dlg)virtual BOOL OnInitDialog();afx_msg void OnPaint();afx_msg HCURSOR OnQueryDragIcon();afx_msg void OnConn();afx_msg void OnDisc();afx_msg void OnView();/AFX_MSGDECLARE_MESSAGE_MAP();/AFX_INSERT_LOCATION/ Microsoft Visual C+ will insert additional declarations immediately before the previous line.#endif / !defined(AFX_POP3DLG_H_INCLUDED_)/ pop3Dlg.cpp : implementation file#include stdafx.h#include pop3.h#include pop31.h#include Gniazdo.h#include ChooseDlg.h#include pop3Dlg.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CPop3Dlg dialogCPop3Dlg:CPop3Dlg(CWnd* pParent /*=NULL*/): CDialog(CPop3Dlg:IDD, pParent)/AFX_DATA_INIT(CPop3Dlg)info = _T();pass = _T(wxpwxp);server = _T();user = _T(busywxp);del = FALSE;/AFX_DATA_INIT/ Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()-LoadIcon(IDR_MAINFRAME);void CPop3Dlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CPop3Dlg)DDX_Control(pDX, IDC_INFO, ctlInfo);DDX_Text(pDX, IDC_INFO, info);DDX_Text(pDX, IDC_PASS, pass);DDX_Text(pDX, IDC_SERVER, server);DDX_Text(pDX, IDC_USER, user);DDX_Check(pDX, IDC_DEL, del);/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CPop3Dlg, CDialog)/AFX_MSG_MAP(CPop3Dlg)ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_CONN, OnConn)ON_BN_CLICKED(IDC_DISC, OnDisc)ON_BN_CLICKED(IDC_VIEW, OnView)/AFX_MSG_MAPEND_MESSAGE_MAP()/ CPop3Dlg message handlersBOOL CPop3Dlg:OnInitDialog()CDialog:OnInitDialog();/ Set the icon for this dialog. The framework does this automatically/ when the applications main window is not a dialogSetIcon(m_hIcon, TRUE); / Set big iconSetIcon(m_hIcon, FALSE); / Set small icon / TODO: Add extra initialization hereCHARFORMAT cf; /设置默认的字符格式cf.cbSize=sizeof(cf);cf.dwMask=CFM_COLOR | CFM_FACE;cf.dwEffects=0;cf.crTextColor=RGB(255,0,0);strcpy(cf.szFaceName,Verdana);CRichEditCtrl* re=(CRichEditCtrl*)GetDlgItem(IDC_INFO);re-SetDefaultCharFormat(cf);return TRUE; / return TRUE unless you set the focus to a control/ 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 CPop3Dlg:OnPaint() if (IsIconic()CPaintDC dc(this); / device context for paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);/ Center icon in client rectangleint 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 icondc.DrawIcon(x, y, m_hIcon);elseCDialog:OnPaint();/ The system calls this to obtain the cursor to display while the user drags/ the minimized window.HCURSOR CPop3Dlg:OnQueryDragIcon()return (HCURSOR) m_hIcon;/important function!/receives messages from CPop3 class!void CPop3Dlg:Dispatch(LONG param)CString s;switch(param)case S_CONNECT: /we are connectedinfo+=Connected to +server; /显示已连接到服务器info+=.rn;GetDlgItem(IDC_CONN)-EnableWindow(FALSE); /禁用连接按钮GetDlgItem(IDC_DISC)-EnableWindow(TRUE); /启用断开按钮GetDlgItem(IDC_VIEW)-EnableWindow(FALSE); /禁用查看邮件按钮break;case S_RECEIVE: /we are receiving some datapop3.GetLastMsg(s); /取来服务器发来的数据info+=s; /显示该数据break; case S_CLOSE: /we are closing connectioninfo+=pop3.GetError();info+=Connection closedrn; /显示连接关闭了GetDlgItem(IDC_CONN)-EnableWindow(TRUE); /启用连接按钮GetDlgItem(IDC_DISC)-EnableWindow(FALSE); /禁用断开按钮break;case S_GETNUMMSGS: /we can get number of messagess.Format(There are %d messagesrn,pop3.GetNumMsg();info+=s; /显示信件的数量break;case S_GETSIZEMSGS: /now size of messagess.Format(Size is: %drn,pop3.GetSizeMsg();info+=s; /显示信件的大小break;case S_ENDRETR: /we have received all of messagess.Format(Received %d messagesrn,pop3.GetRetrMsgNum();info+=s; /显示接收了所有信件的信息if(pop3.GetRetrMsgNum()0)GetDlgItem(IDC_VIEW)-EnableWindow(TRUE);/启用查看邮件按钮break;UpdateData(FALSE); /更新用户界面/当用户点击“连接”按钮时,执行此函数void CPop3Dlg:OnConn() pop3.Set(this); /设定pop3类的变量,使之指向本对话框,以便传递信息UpdateData(TRUE); /取来用户在对话框中输入的数据info=; /列表框清空pop3.SetProp(user,pass); /设定用户名和密码pop3.DelAfterRead(del); /设定邮件删除标志pop3.Create(); /创建pop3.Connect(LPCSTR)server,110); /连接服务器UpdateData(FALSE);void CPop3Dlg:OnDisc() pop3.Close();void CPop3Dlg:OnView() CChooseDlg dlg;dlg.DoModal();/ Gniazdo.h : header file/this is base class for CPop3/mostly, you dont use it directly (exceptions are Set, and OnConnect)/CPop3 will find this class useful#if !defined(AFX_GNIAZDO_H_INCLUDED_)#define AFX_GNIAZDO_H_INCLUDED_#if _MSC_VER = 1000#pragma once#endif / _MSC_VER = 1000#define DLG CPop3Dlg* /change it to your CDialog-based class/ CBase4Pop3 command target/ CBase4Pop3 socket messages:#define S_ACCEPT 0 #define S_CLOSE 1#define S_CONNECT 2#define S_RECEIVE 3#define S_SEND 4class CBase4Pop3 : public CAsyncSocketpublic:CBase4Pop3();virtual CBase4Pop3();/ Overridespublic:/设定接收消息窗体void Set(CDialog * pWnd);CDialog * m_pWnd; /指向父窗体/ Implementationprotected:/几个socket处理函数virtual void OnReceive(int);virtual void OnClose(int);virtual void OnSend(int);virtual void OnConnect(int);virtual void OnAccept(int);private:;/AFX_INSERT_LOCATION/ Microsoft Developer Studio will insert additional declarations immediately before the previous line.#endif / !defined(AFX_GNIAZDO_H_03185426_9BCB_420B_ADA4_4561790828A8_INCLUDED_)/ Gniazdo.cpp : implementation file#include stdafx.h#include pop3Dlg.h#include Gniazdo.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CBase4Pop3CBase4Pop3:CBase4Pop3()CBase4Pop3:CBase4Pop3()/ Do not edit the following lines, which are needed by ClassWizard.#if 0BEGIN_MESSAGE_MAP(CBase4Pop3, CAsyncSocket)/AFX_MSG_MAP(CBase4Pop3)/AFX_MSG_MAPEND_MESSAGE_MAP()#endif / 0/ CBase4Pop3 member functionsvoid CBase4Pop3:Set(CDialog * pWnd) /set the pointerm_pWnd=pWnd;void CBase4Pop3:OnAccept(int err)/free of any error, dispatch messageif(err=0) (DLG)m_pWnd)-Dispatch(S_ACCEPT);void CBase4Pop3:OnConnect(int err)if(err=0) (DLG)m_pWnd)-Dispatch(S_CONNECT);void CBase4Pop3:OnSend(int err)void CBase4Pop3:OnClose(int err)if(err=0) (DLG)m_pWnd)-Dispatch(S_CLOSE);void CBase4Pop3:OnReceive(int err)if(err=0) (DLG)m_pWnd)-Dispatch(S_RECEIVE);/ Pop31.h: interface for the CPop3 class./mailto: zmoraabox43.pl/ feel free to e-mail me!/#if !defined(AFX_POP31_H_INCLUDED_)#define AFX_POP31_H_INCLUDED_#include Gniazdo.h#include #include #include using namespace std;#if _MSC_VER 1000#pragma once#endif / _MSC_VER 1000/enum type describing actual pop3 statetypedef enum FIRST=0,USER,PASS,STAT,LIST,RETR,ENDRETR,DELE,GOON STATE;#define S_GETNUMMSGS 5 /send when user can obtain number of messages#define S_GETSIZEMSGS 6 /as above, but size of messages#define S_ENDRETR 7 /send when done retrieving/simple struct that keeps message data, size, and retrieving sizetypedef struct CString text;int msgSize, retrSize; MESSAGEPROP;class CPop3 : public CBase4Pop3public:void WriteMsg(int i,CString name); /保存邮件到文件/获得基本的邮件信头字段: From, To, Date, Subject, BodyCString GetMsgStuff(int i); CString GetMsgBody(int i); /获得信件体void DelAfterRead(BOOL del=FALSE); /是否收完信后从服务器删除邮件CString GetMsgSubject(int i); /获得信件标题CString GetMsg(int i); /获得整封信的内容int GetRetrMsgNum(); /获得邮件的数量int GetSizeMsg(); /获得所有邮件的大小int GetNumMsg(); /获得服务器上邮件的数量CString GetError(); /获得错误void Close(); /退出服务器void SetProp(CString u, CString p); /设定用户名和密码void GetLastMsg(CString &); /获得从服务器传来的最后一条信息void OnReceive(int err); /套接字对象处理OnReceive事件的函数CPop3();virtual CPop3();private:void ReadLn(int index,CString src, CString &dst); /读入一行数据void ParseMsg(); / 分析服务器的应答,作不同的处理CString lastMsg, error;STATE state;CString user, pass;int numMsg, sizeMsg, retrMsg;vector msgs; /vector for message stuffBOOL delAfterRead;int delMsg;#endif / !defined(AFX_POP31_H_INCLUDED_)/ Pop31.cpp: implementation of the CPop3 c
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 医考资格考试题目及答案
- 徐州市宪法考试题目及答案
- 广电会计招聘考试题库及答案
- 武汉道法考试题库及答案
- 护士报考试题库及答案解析
- 寿司店营销策划方案
- 《阿Q正传(节选)》教学设计 2023-2024学年统编版高中语文选择性必修下册
- (浙江新高考专用版)2024-2025学年高中物理 第十一章 机械振动 4 单摆说课稿 新人教版选修3-4
- Unit 3 Toys Lesson 1(教学设计)-2024-2025学年人教精通版(2024)英语三年级上册
- 华西医院建筑方案设计
- 2025贵州贵安城市置业开发投资有限公司招聘32人考试参考题库及答案解析
- 露天煤业安全生产培训课件
- 2025全国科普日科普知识竞赛题库及答案
- 2025年全国医学基础知识试题(附答案)
- 食堂安全培训课件
- 【课件】角的概念+课件+2025-2026学年人教版(2024)七年+数学级上册+
- 2025企业劳动合同范本新版
- 2025年防雷检测专业技术人员能力认定考试题库及答案
- 《房屋市政工程生产安全重大事故隐患判定标准(2024版)》解读
- 美发裁剪理论知识培训课件
- 舞蹈老师自我介绍课件
评论
0/150
提交评论