当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
最简单的MFC程序
发布时间:2010/5/14 11:13:44 来源:城市学习网 编辑:ziteng
  建立一个Win32空项目后,然后手工打造的最简单的MFC程序:
  1: //stdafx.h
  2: #pragma once
  3: #include <sdkddkver.h>
  4: #include <afxwin.h>
  5: #include <afxwinappex.h>
  6: #include <afxframewndex.h>  1: //stdafx.cpp
  2: #include "stdafx.h"  1: //MainApp.h
  2: #pragma once
  3: class CMainApp : public CWinAppEx
  4: {
  5: public:
  6: virtual BOOL InitInstance();
  7: };  1: //MainApp.cpp
  2: #include "stdafx.h"
  3: #include "MainApp.h"
  4: #include "MainWindow.h"
  5:
  6: CMainApp mianApp;
  7:
  8: BOOL CMainApp::InitInstance()
  9: {
  10: CWinAppEx::InitInstance();
  11:
  12: SetRegistryKey(TEXT("Hello MFC app.")); [NextPage]   13:
  14: m_pMainWnd = new CMainWindow();
  15: m_pMainWnd->ShowWindow(SW_SHOW);
  16: m_pMainWnd->UpdateWindow();
  17: return TRUE;
  18: }  1: //MainWindow.h
  2: #pragma once
  3: class CMainWindow : public CFrameWndEx
  4: {
  5: public:
  6: CMainWindow(void);
  7:
  8: DECLARE_MESSAGE_MAP()
  9:
  10: afx_msg void OnPaint();
  11: afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
  12: };  1: //MainWindow.cpp
  2: #include "stdafx.h"
  3: #include "MainWindow.h"
  4:
  5: CMainWindow::CMainWindow(void)
  6: {
  7: Create(NULL,TEXT("Main window"));
 [NextPage]   8: }
  9:
  10: BEGIN_MESSAGE_MAP(CMainWindow, CFrameWndEx)
  11: ON_WM_PAINT()
  12: ON_WM_LBUTTONDOWN()
  13: END_MESSAGE_MAP()
  14:
  15: void CMainWindow::OnPaint()
  16: {
  17: CPaintDC dc(this); // device context for painting
  18: // TODO: Add your message handler code here
  19: CRect rc;
  20: GetClientRect(&rc);
  21: dc.DrawText(TEXT("It is a simple MFC application!"),&rc,DT_VCENTER|DT_SINGLELINE|DT_CENTER);
  22: }
  23:
  24: void CMainWindow::OnLButtonDown(UINT nFlags, CPoint point)
  25: {
  26: // TODO: Add your message handler code here and/or call default
  27: AfxMessageBox(TEXT("Left mouse button clicked!"));
  28: CFrameWndEx::OnLButtonDown(nFlags, point);
  29: }
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved