阅读量:0
以下是一个简单的使用C++编写的MFC应用程序示例,可以在其中添加一些控件。请使用Visual Studio等C++开发工具打开并编译运行。
#include <afxwin.h> class CMyApp : public CWinApp { public: virtual BOOL InitInstance(); }; class CMainWindow : public CFrameWnd { public: CMainWindow(); afx_msg void OnButtonClicked(); DECLARE_MESSAGE_MAP() }; BOOL CMyApp::InitInstance() { CMainWindow* pMainWnd = new CMainWindow(); m_pMainWnd = pMainWnd; pMainWnd->Create(NULL, _T("My Application")); pMainWnd->ShowWindow(m_nCmdShow); pMainWnd->UpdateWindow(); return TRUE; } CMainWindow::CMainWindow() { Create(NULL, _T("My Application"), WS_OVERLAPPEDWINDOW, CRect(100, 100, 500, 300), NULL); CButton* pButton = new CButton(); pButton->Create(_T("Click Me"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, CRect(10, 10, 100, 30), this, 1); CEdit* pEdit = new CEdit(); pEdit->Create(WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_AUTOVSCROLL, CRect(10, 50, 200, 200), this, 2); } void CMainWindow::OnButtonClicked() { MessageBox(_T("Button clicked!"), _T("Message"), MB_OK); } BEGIN_MESSAGE_MAP(CMainWindow, CFrameWnd) ON_COMMAND(1, OnButtonClicked) END_MESSAGE_MAP() CMyApp myApp;