您的位置:首页 > 编程语言 > C语言/C++

(VC++6.0 )基于单文档对话框的自动生成代码注释

2015-04-04 16:07 465 查看
1、创建的对话框名字是“2”,会有三个类,CAoutDlg类,CMy2App类,CMy2Dlg类;

2、CAoutDlg类是关于对话框,不用多管;

3、CMy2App类

(1)头文件

// 2.h : main header file for the 2 application

//

//这是一中常见的防止头文件被重复包含的格式

#if !defined(AFX_2_H__54191561_7B45_4E73_9CC7_672DCCBFDD4A__INCLUDED_)

#define AFX_2_H__54191561_7B45_4E73_9CC7_672DCCBFDD4A__INCLUDED_

//MSC代表微软c编译器,VER代表版本

//是指如果vc编译器的版本大于1000则这个语句被编译!VC++6.0就是1200

//#pragma once则由编译器提供保证:同一个文件不会被包含多次

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

//如果没有定义__AFXWIN_H__,编译时就报错“error :include 'stdafx.h' before including this file for PCH

#ifndef __AFXWIN_H__

#error include 'stdafx.h' before including this file for PCH

#endif

#include "resource.h" // main symbols

/////////////////////////////////////////////////////////////////////////////

// CMy2App:

// See 2.cpp for the implementation of this class

//

class CMy2App : public CWinApp

{

public:

CMy2App();

// Overrides

// ClassWizard generated virtual function overrides

//{{AFX_VIRTUAL(CMy2App)

public:

virtual BOOL InitInstance();

//}}AFX_VIRTUAL

// Implementation

//{{AFX_MSG(CMy2App)

// NOTE - the ClassWizard will add and remove member functions here.

// DO NOT EDIT what you see in these blocks of generated code !

//}}AFX_MSG

DECLARE_MESSAGE_MAP()

};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}

// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_2_H__54191561_7B45_4E73_9CC7_672DCCBFDD4A__INCLUDED_)

(2)源文件

// 2.cpp : Defines the class behaviors for the application.

//

#include "stdafx.h"

#include "2.h"

#include "2Dlg.h"

//当在debug模式下时,我们分配内存时的new被替换成DEBUG_NEW,

//而这个DEBUG_NEW不仅要传入内存块的大小,还要传入源文件名和行号,这就有个好处,即当发生内存泄漏时,

//我们可以在调试模式下定位到该问题代码处。

//若删掉该句,就不能进行定位了。而在release版本下的new就是简单的new,并不会传入文件名和行号。

//因此,我们在开发代码阶段,保留上述代码是值得的。

//当碰到__FILE__时,编译器会把__FILE__替换成一个字符串,这个字符串就是当前在编译的文件的路径名。在DEBUG_NEW的定义中没有直接使用__FILE__,而是用了THIS_FILE,其目的是为了减小目标文件的大小

#ifdef _DEBUG //表示在调试状态下编译,因此相应修改了两个符号的定义

#define new DEBUG_NEW //在MFC中,可以使用 DEBUG_NEW 宏代替 new 运算符来帮助定位内存泄漏

#undef THIS_FILE //取消THIS_FILE的定义

static char THIS_FILE[] = __FILE__; //定义THIS_FILE指向文件名

#endif

/////////////////////////////////////////////////////////////////////////////

// CMy2App

BEGIN_MESSAGE_MAP(CMy2App, CWinApp)

//{{AFX_MSG_MAP(CMy2App)

// NOTE - the ClassWizard will add and remove mapping macros here.

// DO NOT EDIT what you see in these blocks of generated code!

//}}AFX_MSG

ON_COMMAND(ID_HELP, CWinApp::OnHelp)

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////

// CMy2App construction

CMy2App::CMy2App()

{

// TODO: add construction code here,

// Place all significant initialization in InitInstance

}

/////////////////////////////////////////////////////////////////////////////

// The one and only CMy2App object

CMy2App theApp;

/////////////////////////////////////////////////////////////////////////////

// CMy2App initialization

BOOL CMy2App::InitInstance()

{

AfxEnableControlContainer();//允许应用程序作为控件容器来使用

// 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 _AFXDLL

Enable3dControls();
// 如果成功地载入了CTL3D32.DLL,则为TRUE;否则为FALSE。在与MFC DLL连接时,必须使用Enable3dControls

#else

Enable3dControlsStatic();
// Call this when linking to MFC statically,当与MFC库进行静态连接时,必须使用Enable3dControlsStatic

#endif

CMy2Dlg dlg;//创建一个对话框对象

m_pMainWnd = &dlg;//将本线程(即程序主线程)的主窗口设置为这个对话框

int nResponse = dlg.DoModal();//有模式地显示这个对话框,直到对话框关闭

if (nResponse == IDOK)

{

// TODO: Place code here to handle when the dialog is

// dismissed with OK

}

else 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 application's message pump.

return FALSE;//InitInstance()函数的结束返回值是false,应用程序将会立即退出。

//也就是只显示对话框,当对话框关闭后,程序就会结束了。这时候的InitInstance函数就有点主函数的味道了。

}

4、CMy2Dlg类

(1)头文件

// 2Dlg.h : header file

//

#if !defined(AFX_2DLG_H__2DF50C16_32A5_4358_970B_131779B517C8__INCLUDED_)

#define AFX_2DLG_H__2DF50C16_32A5_4358_970B_131779B517C8__INCLUDED_

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

/////////////////////////////////////////////////////////////////////////////

// CMy2Dlg dialog

class CMy2Dlg : public CDialog

{

// Construction

public:

CMy2Dlg(CWnd* pParent = NULL);
//自定义的构造函数

// Dialog Data

//{{AFX_DATA(CMy2Dlg)

enum { IDD = IDD_MY2_DIALOG };//它表明这个CDialogDlg类使用的对话框模板是IDD_MY2_DIALOG

// NOTE: the ClassWizard will add data members here

//}}AFX_DATA

// ClassWizard generated virtual function overrides

//{{AFX_VIRTUAL(CMy2Dlg)

protected:

virtual void DoDataExchange(CDataExchange* pDX);
// DDX/DDV support

//}}AFX_VIRTUAL

// Implementation

protected:

HICON m_hIcon;

// Generated message map functions

//{{AFX_MSG(CMy2Dlg)

virtual BOOL OnInitDialog();//对话框初始化消息操作函数

afx_msg void OnSysCommand(UINT nID, LPARAM lParam);//系统菜单消息响应函数

afx_msg void OnPaint();//对话框重绘响应函数

afx_msg HCURSOR OnQueryDragIcon();//最小化图标询问响应函数

//}}AFX_MSG

DECLARE_MESSAGE_MAP()

};

//{{AFX_INSERT_LOCATION}}

// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_2DLG_H__2DF50C16_32A5_4358_970B_131779B517C8__INCLUDED_)

(2)源文件

// 2Dlg.cpp : implementation file

//

#include "stdafx.h"

#include "2.h"

#include "2Dlg.h"

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

/////////////////////////////////////////////////////////////////////////////

// CAboutDlg dialog used for App About

class 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

// Implementation

protected:

//{{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_MAP

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////

// CMy2Dlg dialog

CMy2Dlg::CMy2Dlg(CWnd* pParent /*=NULL*/)

: CDialog(CMy2Dlg::IDD, pParent)

{

//{{AFX_DATA_INIT(CMy2Dlg)

// NOTE: the ClassWizard will add member initialization here

//}}AFX_DATA_INIT

// Note that LoadIcon does not require a subsequent DestroyIcon in Win32

m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

//在这个函数中首先,调用父类CDialog的构造函数来完成默认构造操作。

//其次,它使用AfxGetApp函数取得全局CWinApp对象theApp的指针,

//并使用它的LoadIcon函数来取得程序中IDR_MAINFRAME图标资源,并赋给成员变量m_hIcon。

//这个图标可以在资源视图的ICON中可以的查到和设定

}

void CMy2Dlg::DoDataExchange(CDataExchange* pDX)

{

CDialog::DoDataExchange(pDX);

//{{AFX_DATA_MAP(CMy2Dlg)

// NOTE: the ClassWizard will add DDX and DDV calls here

//}}AFX_DATA_MAP

}

BEGIN_MESSAGE_MAP(CMy2Dlg, CDialog)

//{{AFX_MSG_MAP(CMy2Dlg)

ON_WM_SYSCOMMAND()

ON_WM_PAINT()

ON_WM_QUERYDRAGICON()

//}}AFX_MSG_MAP

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////

// CMy2Dlg message handlers

BOOL CMy2Dlg::OnInitDialog()

{

CDialog::OnInitDialog();

// // 将\“关于...\”菜单项添加到系统菜单中。

// 取得此对话框系统菜单的CMenu对象指针

ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);//ASSERT是宏定义,用来判断IDM_ABOUTBOX的ID值是否发生变化

ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);///取得此对话框系统菜单的CMenu对象指针,并赋给pSysMenu;

if (pSysMenu != NULL)//获取的系统菜单非空

{

CString strAboutMenu;//声明一个字符串对象

strAboutMenu.LoadString(IDS_ABOUTBOX);//取得资源IDS_ABOUTBOX预定义字符串,可以在资源视图中的String Table查到和设定这个预定义字符串

if (!strAboutMenu.IsEmpty())//如果不为空,则

{

pSysMenu->AppendMenu(MF_SEPARATOR);//向菜单添加一个分隔符

pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);//向菜单添加这个字符串,并将消息ID设为IDM_ABOUTBOX

}

}

//设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动执行此操作

// 

SetIcon(m_hIcon, TRUE);
// Set big icon

SetIcon(m_hIcon, FALSE);
// Set small icon

// TODO: Add extra initialization here

return TRUE; // return TRUE unless you set the focus to a control 除非设置了控件的焦点,否则应该返回 TRUE

}

//

void CMy2Dlg::OnSysCommand(UINT nID, LPARAM lParam) //按下了系统对话框

{

if ((nID & 0xFFF0) == IDM_ABOUTBOX) //如果按下了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.

//如果CWnd最小化了,那么就先获取设备上下文,然后用图表擦出背景,然后获得图表的宽和高,

//再获得最小化时的客户区矩形大小,

//通过int x = (rect.Width() - cxIcon + 1) / 2;int y = (rect.Height() - cyIcon + 1) / 2;计算,

//然后在x,y出画图标m_hIcon.(由// Center icon in client rectangle注释可以知道是在最小化时的客户区中间画图标)

//否则直接调用父类的OnPaint重画对话框。

void CMy2Dlg::OnPaint()

{

if (IsIconic())//当前窗口最小化时返回True,否则返回false

{

CPaintDC dc(this); //获取设备上下文 device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);//这个函数,是发送WM_ICONERASEBKGND到本窗口消息队列,(WPARAM) dc.GetSafeHdc(), 是响应这个消息的函数的参数。

// 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);//否则直接调用父类的OnPaint重画对话框

}

else

{

CDialog::OnPaint();

}

}

// The system calls this to obtain the cursor to display while the user drags

// the minimized window.

//框架为不具有为类图标的最小化(图标化)窗口调用这个成员函数。系统调用这个函数以在用户拖拉最小化窗口的时候显示光标。

HCURSOR CMy2Dlg::OnQueryDragIcon()

{

return (HCURSOR) m_hIcon;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: