MFC windows programming (version 2) essence notes

Source: Internet
Author: User
MFC windows programming (version 2) is a classic book about 32-bit Windows Application Design Using MFC instead of Win32 APIs. The author Jeff prosise is known as Petzold In the MFC field. The author wrote in the preface: "My goal is to write a book for C ++ programmers that is comparable to that of the everlasting, untitled windows programming for C programmers."
This book is different because it does not start with the Appwizard Wizard to create a program, but directly uses the MFC class library, using various types of coupling between self-built programs, compared to other (especially the same domestic) books, it can give people a fresh feeling.
I found this book in the school library. The feeling at that time was: Mom, the ups and downs of life were so exciting. I didn't expect such a new classic in that pile of old antiques.
I hope I can use it for a summer vacation. After completing each chapter, I will write a document on Reading Notes and some of my own ideas, hoping to help anyone who is lucky enough.
Because "every day you have the opportunity to pass by with others, you may not know anything about him, but maybe one day he will become your friend or confidant .", We hope you can support it.

Chapter 1 Hello MFC

Essence concentration:
We all know that every Windows program starts with a winmain (). Of course, MFC is no exception. But in MFC, it is hidden. MFC defines a global application object (theapp), and each global object is constructed by the startup code before the main program entry point (entrypoint. In other words, before the program enters winmain (), the memory will have a theapp object. In this way, theapp's member functions can be used by us. That is, winmain () came out...
Winmain () first calls theapp (that is, the application class) member function initinstance () virtual function (to overwrite), constructs the main form, displays the update, and then calls run () (normally do not overwrite), it enters the message loop.
In SDK programming, messages always indicate their own existence by calling the Window Process (wndproc () of a window. At the same time, this call is usually accompanied by four parameters: the window handle referred to by the message, a message ID, and two 32-bit parameters named wparam and lparam. The values of the last two parameters vary with messages. In MFC, message ing replaces the window process as the communication interface between messages and their processing.
Any class derived from csf-target can contain message ing. Message ing is a table that associates a message with its processing function (as a member function of the class. It is a way for MFC to avoid lengthy virtual function tables (vtable) (imagine, if every class in the memory has a virtual function table, every class object instance requires a pointer vpointer (4 bytes) pointing to that table, which is incalculable and redundant ). MFC solves the problem with three macros.
Therefore, adding a message ing to a class requires three tasks:
1) Add the declare_message_map statement to the class declaration to declare message ing;
2) execute message ing by placing a macro that indicates the start and end of message ing. The corresponding class will call the message and its processing function between the call to begin_message_map and end_message_map;
3) add member function definitions to process messages.
For details about the message ing mechanism, refer to the in-depth introduction to MFC.
This book also provides methods for self-built Windows programs. Select Win32 Application in New-project, and then select a empty project in the next window. After entering the main environment, use project-add to project to add files. Before compiling, remember to change the compilation option to use MFC in a shared DLL.

Some knowledge points:
1) Hungarian markup: one or more lower-case letters are used to identify the variable type before each variable name. The member variable starts with M. For example, m_strwndclass, M _ indicates that this is a class data member, and STR indicates that this is a variable of the cstring type. Common Type Prefix: B-bool, H-handle, N-int, L-long int, etc.
2) Document/View Structure: separates the data from the data presentation to simplify a program structure for programming, which will be detailed in later sections.
3) cobject: Most MFC classes are derived directly or indirectly from cobject. It provides three important features for Its Derived classes: serialization support, runtime type information, and diagnosis and debugging support.
4) afx function: afx is short for application framework. All functions starting with afx are global functions and can be called in any class implementation. Typical examples include afxmessagebox () and afxgetapp.
5) _ t macro: used to set the Character String constant to "neutral ". In this way, both the ANSI character set and Unicode character string will be irrelevant to the system. For more information about _ t, I suggest you read Jeffrey Richter's advanced windows or msdn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.