vs2008最简单的MFC对话框程序
vs2008新建一个空的win32项目,工程设置使用MFC静态库
新建stdafx.h键入如下代码(这里我是从vs2008新建的mfc中拷贝过来的)
// stdafx.h : 标准系统包含文件的包含文件, // 或是经常使用但不常更改的 // 特定于项目的包含文件 #pragma once #ifndef _SECURE_ATL #define _SECURE_ATL 1 #endif #ifndef VC_EXTRALEAN #define VC_EXTRALEAN // 从 Windows 头中排除极少使用的资料 #endif #pragma once // 以下宏定义要求的最低平台。要求的最低平台 // 是具有运行应用程序所需功能的 Windows、Internet Explorer 等产品的 // 最早版本。通过在指定版本及更低版本的平台上启用所有可用的功能,宏可以 // 正常工作。 // 如果必须要针对低于以下指定版本的平台,请修改下列定义。 // 有关不同平台对应值的最新信息,请参考 MSDN。 #ifndef WINVER // 指定要求的最低平台是 Windows Vista。 #define WINVER 0x0600 // 将此值更改为相应的值,以适用于 Windows 的其他版本。 #endif #ifndef _WIN32_WINNT // 指定要求的最低平台是 Windows Vista。 #define _WIN32_WINNT 0x0600 // 将此值更改为相应的值,以适用于 Windows 的其他版本。 #endif #ifndef _WIN32_WINDOWS // 指定要求的最低平台是 Windows 98。 #define _WIN32_WINDOWS 0x0410 // 将此值更改为适当的值,以适用于 Windows Me 或更高版本。 #endif #ifndef _WIN32_IE // 指定要求的最低平台是 Internet Explorer 7.0。 #define _WIN32_IE 0x0700 // 将此值更改为相应的值,以适用于 IE 的其他版本。 #endif #define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // 某些 CString 构造函数将是显式的 // 关闭 MFC 对某些常见但经常可放心忽略的警告消息的隐藏 #define _AFX_ALL_WARNINGS #include <afxwin.h> // MFC 核心组件和标准组件 #include <afxext.h> // MFC 扩展 #include <afxdisp.h> // MFC 自动化类 #ifndef _AFX_NO_OLE_SUPPORT #include <afxdtctl.h> // MFC 对 Internet Explorer 4 公共控件的支持 #endif #ifndef _AFX_NO_AFXCMN_SUPPORT #include <afxcmn.h> // MFC 对 Windows 公共控件的支持 #endif // _AFX_NO_AFXCMN_SUPPORT #ifdef _UNICODE #if defined _M_IX86 #pragma comment(linker,"/manifestdependency:"type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="x86" publicKeyToken="6595b64144ccf1df" language="*""") #elif defined _M_IA64 #pragma comment(linker,"/manifestdependency:"type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="ia64" publicKeyToken="6595b64144ccf1df" language="*""") #elif defined _M_X64 #pragma comment(linker,"/manifestdependency:"type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="amd64" publicKeyToken="6595b64144ccf1df" language="*""") #else #pragma comment(linker,"/manifestdependency:"type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*""") #endif #endif
新建MFCTestDlg.rc的资源文件,插入一个对话框命名ID为IDD_DIALOG1(默认是这个不用修改,自动产生resource.h)
MFCTestDlg.h
#ifndef MFCTEST_HEAD #define MFCTEST_HEAD #pragma once #include "stdafx.h" #include "resource.h" // CMFC123Dlg 对话框 class CMFCTestDlg : public CDialog { // 构造 public: CMFCTestDlg(CWnd* pParent = NULL); // 标准构造函数 // 对话框数据 enum { IDD = IDD_DIALOG1 }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 // 实现 protected: HICON m_hIcon; // 生成的消息映射函数 virtual BOOL OnInitDialog(); afx_msg void OnPaint(); DECLARE_MESSAGE_MAP() }; #endif
MFCTestDlg.cpp
#include "stdafx.h" #include "MFCTestDlg.h" CMFCTestDlg::CMFCTestDlg(CWnd* pParent /*=NULL*/): CDialog(CMFCTestDlg::IDD, pParent) { m_hIcon =::LoadIcon(NULL,MAKEINTRESOURCE(IDI_ERROR)); } void CMFCTestDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CMFCTestDlg, CDialog) ON_WM_PAINT() END_MESSAGE_MAP() BOOL CMFCTestDlg::OnInitDialog() { CDialog::OnInitDialog(); // TODO: 在此添加额外的初始化代码 SetIcon(m_hIcon, TRUE); // 设置大图标 SetIcon(m_hIcon, FALSE); // 设置小图标 return TRUE; // 除非将焦点设置到控件,否则返回 TRUE } void CMFCTestDlg::OnPaint() { CDialog::OnPaint(); }
WinApp.h
#pragma once #include "stdafx.h" class CMFCTestApp : public CWinApp { public: CMFCTestApp(); // 重写 public: virtual BOOL InitInstance(); // 实现 DECLARE_MESSAGE_MAP() }; extern CMFCTestApp theApp;
WinApp.cpp
#include "stdafx.h" #include "WinApp.h" #include "MFCTestDlg.h" CMFCTestApp::CMFCTestApp() { } BEGIN_MESSAGE_MAP(CMFCTestApp, CWinApp) END_MESSAGE_MAP() // 唯一的一个 CMFCTestApp 对象 CMFCTestApp theApp; BOOL CMFCTestApp::InitInstance() { INITCOMMONCONTROLSEX InitCtrls; InitCtrls.dwSize = sizeof(InitCtrls); InitCtrls.dwICC = ICC_WIN95_CLASSES; InitCommonControlsEx(&InitCtrls); CWinApp::InitInstance(); AfxEnableControlContainer(); SetRegistryKey(_T("应用程序向导生成的本地应用程序")); CMFCTestDlg dlg; m_pMainWnd = &dlg; INT_PTR nResponse = dlg.DoModal(); if (nResponse == IDOK) { // TODO: 在此放置处理何时用 // “确定”来关闭对话框的代码 } else if (nResponse == IDCANCEL) { // TODO: 在此放置处理何时用 // “取消”来关闭对话框的代码 } // 由于对话框已关闭,所以将返回 FALSE 以便退出应用程序, // 而不是启动应用程序的消息泵。 return FALSE; }
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。