Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 1 Hello, MFC.

Similar presentations


Presentation on theme: "Chapter 1 Hello, MFC."— Presentation transcript:

1 Chapter 1 Hello, MFC

2 Windows Programming SDK and API MFC Based on C-language
Software Development Kit软件开发工具箱/包 Application Programming Interface应用程序接口 MFC Microsoft Foundation Class Library微软基础类库 Makes Windows programming simpler Microsoft Developer Network微软开发者网络 Windows API:Windows application programming interface(WIndows应用程序接口) Platform SDK:Platform Software Development Kit(平台软件开发工具包) 在概念上,SDK是一系列文件的组合,包括lib、dll、.h、文档、示例等等;API是对程序而言的,提供用户编程时的接口,即一系列模块化的类和函数。可以认为API是包含在SDK中的。

3 Windows Programming Model
Event-driven事件驱动 programming model Procedural过程化 programming model

4 Windows Programming Model
Event - Keystroke - Mouse click Command WM_QUIT message terminates msg loop

5 Common Windows Messages
Sent When WM_CHAR A character is input from the keyboard WM_COMMAND The user selects an item from a menu, or a control sends a notification to its parent WM_CREATE A window is created WM_DESTROY A window is destroyed WM_LBUTTONDOWN The left mouse button is pressed WM_LBUTTONUP The left mouse button is released WM_MOUSEMOVE The mouse pointer is moved WM_PAINT A window needs repainting WM_QUIT The application is about to terminate WM_SIZE A window is resized

6 Four Input Parameters of Windows Message
Handle of the window 窗口句柄 Message ID 消息编号 wParam 消息参数 lParam 消息参数

7 Handle of the window 32-bit value Uniquely identifies a window 窗口的唯一标识
References a data structure in which Windows stores relevant information about the window

8 Message ID A numeric value that identifies the message type
WM_CREATE, WM_PAINT, etc.

9 wParam, lParam wParam lParam Information specific to the message type
Example WM_LBUTTONDOWN wParam Holds a series of bit flags identifying the state of the Ctrl and Shift keys and of the mouse buttons lParam Holds two 16-bit values identifying the location of the mouse pointer when the click occurred

10 Window Program Structure

11 Windows Programming, SDK-Style
int WINAPI WinMain (…) { wc.style = 0; // Class style … wc.lpszClassName = "MyWndClass"; RegisterClass (&wc); hwnd = CreateWindow (…); ShowWindow (hwnd, nCmdShow); UpdateWindow (hwnd); while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg); DispatchMessage (&msg); } return msg.wParam; }

12 Windows Programming, SDK-Style
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; HDC hdc; switch (message) { case WM_PAINT: hdc = BeginPaint (hwnd, &ps); Ellipse (hdc, 0, 0, 200, 100); EndPaint (hwnd, &ps); return 0; case WM_DESTROY: PostQuitMessage (0); } return DefWindowProc (hwnd, message, wParam, lParam);

13 http://nojob.blog.51cto.com/1611772/320218 《Windows程序设计》第三章学习心得(1)|图解消息机制

14 Introducing MFC An object-oriented面向对象 wrapper around the Windows API
Application framework应用程序框架 Encapsulates virtually every aspect of a program's operation Afx

15 Benefits of Using C++ and MFC
Object-oriented面向对象 design methodology Framework uses a lot of tricks to make Windows objects Windows 一般窗口 Dialog boxes 对话框 Controls 控件

16 The MFC Design Philosophy
Object-oriented interface to the Windows Reusability, self-containment, etc. Minimized the overhead

17 Document/View Architecture
CDocument 文档类(data) Cview 视图类(display) To separate processed data and it’s display

18 The MFC Class Hierarchy
CObject Serialization support Run-time class information support Diagnostic and debugging support

19 AFX Functions Global functions Begin with Afx AfxRegisterWndClass()
Registers a custom WNDCLASS for an MFC application AfxMessageBox(…) display a messagebox window

20 First MFC Application How classes are derived from the MFC
How classes are plugged into the application CWinApp class Application CFrameWnd class Main Window CPaintDC class Message mapping

21 Header File (hello.h) class CMyApp : public CWinApp { public: virtual BOOL InitInstance (); }; class CMainWindow : public CFrameWnd CMainWindow (); protected: afx_msg void OnPaint (); DECLARE_MESSAGE_MAP ()

22 Source File (hello.cpp)
#include <afxwin.h> #include "Hello.h" CMyApp myApp; ////////////////////////////////////////////////////// / // CMyApp member functions BOOL CMyApp::InitInstance () { m_pMainWnd = new CMainWindow; m_pMainWnd->ShowWindow (m_nCmdShow); m_pMainWnd->UpdateWindow (); return TRUE; }

23 Source File (hello.cpp)
// CMainWindow message map and member functions BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd) ON_WM_PAINT () END_MESSAGE_MAP () CMainWindow::CMainWindow () { Create (NULL, _T ("The Hello Application")); } void CMainWindow::OnPaint () CPaintDC dc (this); CRect rect; GetClientRect (&rect); dc.DrawText (_T ("Hello, MFC"), -1, &rect, DT_SINGLELINE ¦ DT_CENTER ¦ DT_VCENTER);


Download ppt "Chapter 1 Hello, MFC."

Similar presentations


Ads by Google