// MFC.cpp -- Windows MFC编程示例
// 编译:cl /MD /D "_AFXDLL" MFC.cpp /link /subsystem:windows
#include <afxwin.h>
#define IDC_BUTTON1 100
//声明应用类
class MyApp:public CWinApp
{
public:
virtual BOOL InitInstance();
};
//声明主窗口类
class MyWnd:public CFrameWnd
{
CStatic * cs;
CButton * cb;
public:
MyWnd();
afx_msg void HandleButton();
DECLARE_MESSAGE_MAP() //声明消息映射宏
};
//消息处理函数
void MyWnd::HandleButton()
{
AfxMessageBox("Hello World!");
}
//消息映射
BEGIN_MESSAGE_MAP(MyWnd, CFrameWnd)
ON_BN_CLICKED(IDC_BUTTON1, HandleButton)
END_MESSAGE_MAP()
//应用类初始化函数
BOOL MyApp::InitInstance()
{
m_pMainWnd=new MyWnd();
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}
//主窗口构造函数
MyWnd::MyWnd()
{
Create(NULL, "Windows MFC Program", WS_OVERLAPPEDWINDOW, rectDefault);
cs=new CStatic();
cs->Create("Hello Windows 98!", WS_CHILD|WS_VISIBLE|SS_CENTER, CRect(180,100,430,120), this);
cb=new CButton();
cb->Create("Push me", WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, CRect(250,150,350,180), this, IDC_BUTTON1);
}
//创建应用类实例
MyApp HelloApp;
最最简单的MFC程序
// 编译:cl /MD /D "_AFXDLL" MFC2.cpp /link /subsystem:windows
#include <afxwin.h>
class MyApp:public CWinApp
{
public:
BOOL InitInstance();
};
BOOL MyApp::InitInstance()
{
CFrameWnd* pMainWnd=new CFrameWnd;
m_pMainWnd=pMainWnd;
pMainWnd->Create(NULL, "Hello World!");
pMainWnd->ShowWindow(m_nCmdShow);
return TRUE;
}
MyApp theApp;