2008年4月2日 星期三

MFC 簡介

MFC Introduction

Contents:

1. Some Background on Object-Oriented Programming

2. A Little History

3. A Grand Tour of MFC

(i) CObject

(ii) CWnd

(iii) CWinApp / CWinThread

(iv) CString

(v) Remaining

4. A Simple Code about Dialog Style Application


Preface

MFC (Microsoft Foundation Classes) is a framework for Windows programming, and there are other ones like ATL (Active Template Library) and WTL (Windows Template Library) from Microsoft.

Some Background on Object-Oriented Programming

OOP Terminology

1. Abstraction

2. Encapsulation

3. Inheritance

4. Polymorphism

5. Modularity

A Little History

1989 – Microsoft establish the application framework technology development group (AFX group, and you will see many functions with prefix “AFX”) (Legend has it that “AF” didn’t sound so great by itself, so the threw in the letter X to complete the acronym, The X doesn’t really mean anything)

1992 – Microsoft released MFC 1.0

1993/04 – Microsoft released VC++ 1.0 with MFC 2.0

1993 late – VC++ 1.5 with MFC 2.5

1994 late – MFC 3.0

1995 – VC++ 4.0 with MFC 4.0 (the last version is MFC 4.2)

1995 ~ 200x – MFC 5.0, 6.0 Unknow

2002 - Visual Studio .Net 2003 (VC++ 7.0) with MFC 7.0

Future - Visual Studio .Net 2005 (VC++ 8.0) with MFC 8.0

A Grand Tour of MFC

CObject: The Mother of (Almost) All Classes

It is MFC’s strategy, like JAVA Object. Classes derived from CObject inherit some useful capabilities, including run-time type identification for derived class, serialization (that is, persistence), diagnostic functions, and support for dynamic object creation.

CWnd: The Mother of All Windows

CWnd serves as a breeding ground for all of Windows’ visual interface objects like dialog box and controls (button, list box, edit and so on).

Dialogs:

CFileDialog – Selects a file from a directory.

CColorDialog – Selects a specific color.

CFontDialog – Selects a font.

CPrintDialog – Handles printer setup and printing.

CFindReplaceDialog – Selects text to search and replace.

Most important skill for Dialog: (In MFC, I only like this)

Dialog Data Exchange and Validation

CWinApp / CWinThread

CwinThread represents a thread of execution within an MFC program. Though earlier versions of MFC weren’t thread safe, version 3.0 and later are.

CWinApp, which is derived from CWinThread, represents the standard Windows application. CWinApp has overrideable functions you can use to initialize your app and perform termination cleanup to suit your own needs.

CString

Creating strings with the CString class is a snap. MFC’s CString class provides basic operations such as concatenation, comparison, and assignments.

Remaining:

1. GDI Support and Drawing Object – See GDI topic in MSDN

2. Document/View Architecture – like MVC concept.

3. OLE Support: OLE Control

4. ODBC Support / DAO Support

A Simple Code about Dialog Style Application

See HelloWorld project

The Key point for Dialog

1. The disagreeable buttons: OK and Cancel

They are very convenient for dialog box, but not for application. Because of terminating application when user clicks OK or Cancel (presses Enter or ESC), we have to modify these undesired codes.

Solution 1: Modify the message mapping (I don’t like it).

Solution 2: Modify the virtual function and add new message mapping.

2. The Message MAP: the macro instructions for message map.

(i) DECLARE_MESSAGE_MAP(), BEGIN_MESSAGE_MAP / END_MESSAGE_MAP()

(ii) Combine the Windows message with your routine

(iii) Don’t add any extra codes in the Message Map declaration

3. DDX / DDV (Dialog Data Exchange / Validation)

(i) DoDataExchange(CDataExchange* pDX)

(ii) The DDX_ and DDV_ macro instructions

(iii) UpdateData(true) and UpdateData(false)

4. WM_INITDIALOG / OnInitDialog()

When a dialog is created, Windows sends a WM_INITDIALOG message just before the dialog is displayed so that the application can initialize any controls in the dialog before they become visible to the end user.

5. GetDlgItem: To get handle of child window

The HWND is the key to control any windows (including button and etc). If you want access unknown windows, you just find their handle (HWND) and call some windows function (like SetWindowText) with it.

沒有留言: