MFC technology insider

zhaozj2021-02-16  44

Programming model

When he learned new knowledge, I personally prefer to think about problems with Lenovo, comparison and summary methods, solve the problem, so that all unknown relationships, make all known similar comparison, thus summing up them The commonality, sorting and taking a mess in the brain, thereby achieving promotion. Learning programming is no exception, in the process of learning programming, I found that there is a very common technology in programming technology: model! Message mechanism, document view structure, dynamic generation, and COM use a similar model method to solve the problem. Please listen to the extraordinary summary of the younger and one by one by one by one: Message mechanism:

The WINDOWS program is based on an external event to drive. There is a USER module in the operating system to capture events that occur in peripherals; The system interface determines the window that responds to this message, populates the message structure MSG, and put this mouse message into the system message queue, the getMessage function gets a message from the message queue, and send the message to a specific window according to the content of the message. The function is handled by the window function. Each message corresponds to a set of code segments, and the program completes our task by executing code segments; in the MFC, the data of the class is encapsulated with the data we have to operate, and the code segment to be executed is encapsulated with the function member. A message is usually corresponding to a function. So, how do messages know that you should be implemented by the function member in that class? First, the MFC designs a message mapping table Message Map for each class that can handle the message, and the message mapping table is responsible for "tied" the message that can be processed and its handle function is "tied" in one. Second, let's link the message mapping table of the class with inheritance relationship to form a message network. By comparing the contents of the message in each class of inheritance relationships (MessageID), if you find Call its handler. If you can't find equal MessageID in this message network (there is no function in the network in the network), that is, two cases: if it is a command message or the message sent by other controls, Jump to another message network to see if there is a choice, after the jump of the experts, after the jump network is set, still can't find a message, then call the system's default processing message, find it, of course, call it. The handler is handled; if it is a Windows message, you don't have to jump, and the system defaults to handle the function will be done. It can be seen that during the process of message delivery, we need to know some information: what type is this message, and if you want to "jump", you should first jump to the net, jump to the net, still can't find it. After giving up. Therefore, in a function of processing message capabilities, a type named onWndMSG function resolved message is defined, defining a route named oncmdmsg function to determine the jumper (this route is set by the experts. There is no need to work.).

Just now, put the message with its handle function "tied" on a piece, connect the message mapping of the class with inheritance relationship, what do we do in MFC? Several macros are set in the MFC to be responsible for work: declare_message_map / begin_message_map / on_command / end_message_map. The macro in the base, which mainly includes a message mapping table structure, a function of obtaining a message mapping table structure address and a MessageEntris member. Begin_MESSAGE_MAP / ON_COMMAND / End_MAP / ON_COMMAND / END_MESSAGE_MAP three macros responsible for populating the contents of the message mapping table and establish a message network inheritance. So, when we join a new message to the class, we don't have to join anything in the classes, because there is a function of obtaining a message mapping table structure address in DECLARE_MESSAGE_MAP, we only need to add new messages to this address. Items and their handlers. To define a new message for a class, it becomes a fill. I understand the structure between this message and function into a model; the message and the function should be directly corresponding, now we separate them to the convenience of management, people are separated, join the intermediate layer (message mapping table, special management The correspondence between the message and the processing function is to achieve more powerful functions. This is a model, indirect model, is also a method, that is, the A-C model becomes a A-B-C model. I also regard this kind of fill in a template, just like the painting, the dragon is already painted, we only need to touch your eyes, the dragon will fly. We will find that in computer technology, this is a commonly used method of solving problems. Dynamic generation and type identification:

Whether it is the type of implementation, dynamic generation or the reading and writing of the file, is the same data structure CruntimeClass as the same as a network of category. The category record network is made by the CRUNTIMECLASS. The declaration in the class is made by the declare_dynamic macro package. The link work is completed by the import_dynamic macro seal outside the class. Of course, according to different applications, CRUntimclass has different contents, the name of the macro used It is also slightly different. His working principle and message mechanism are a bit similar, all of which use the "pointer" to complete the bridge work, and use the pointer to be integrated with an exterior network, centralized management information. What is the big use of this network? This is the case, when we generate a class object during the execution period (such as when reading the file, the data we can generate according to the actual situation can be read), the information we need: the name of the class, the size of the class, Class construct functions, classes and other classes of relationships, etc., information about classes, will be recorded on the category network; then when we get a class name (with the string) class name, we can This online identity is identified (by the class name of the member of the Net) and then calls its constructive function (there is a pointer pointing to its constructive function) to generate an object. Of course, the corresponding class of objects generated during the execution period will need to travel in the category network, and this network is built by you in one hand, there is something in it, and you are clear. As for how to build a network, it is more troublesome, anyway, there are two macros to automatically do these work, we only need to make a fill problem, and determine those classes to hang online. Everyone thinks about it, it is very simple to generate an object, it is very simple, it can be produced directly, but in order to actively generate the program, in order to dynamically generate during the execution period (then we can't make it like it before. Add a statement to generate an object, because we can't add the language sentence to the package that has been distributed), so we add an intermediate layer, we have generated a network to centrally manage type information, this solution method Is it very high! ? I understand the dynamic generation process into a model, indirect model, and a method, that is, the A-C model becomes a A-B-C model. I also regard this kind of fill in a template, just like the painting, the dragon is already painted, we only need to touch your eyes, the dragon will fly. All this is to provide convenience for programmers. Document / View Structure:

We see documents as a piece of data, and the view is to show us the window we look at the way we want. If a data is displayed in one way, if we only need to display data in a file type, then the data and views don't need to play what tricks can meet our requirements. However, when we need to display the data in different ways (such as data about statistics, we hope to show us a variety of ways, column chart, text, etc., we are more hopeful The program can handle data in several different file types and provide different display interfaces for it, then there should be like it is not possible! How to do? Oh, we plus a middle layer! This middle layer is called Document Template in the MFC, which is responsible for centralizing the conversion between the management document view. Well, it should be said to manage Document / View / Frame, frame is a window that is outside the view, he can be VIEW / Document provides a dedicated menu. Why can data display in a lot of form? That is because, the data can be displayed in a form, it will be displayed in a variety of forms! Oh, it seems to be a bit strong, actually, because there is a middle layer to separate the document view, then as long as you design a few display forms in the original data, you need to display, just tell Document Template, it Will be considered to help you arrange (connect the view you want, and this view-specific frame) must do. Similarly, provide dedicated View and Frames for data in different file types in the same main framework also help us arrange by Document Template. It is naturally, in the Document / View / Frame Trinity Structure, there is a linked list that connects the file type (a Document Template corresponding to the type of Document, there is also a lookup process when the data is displayed, that is, the file to display first. Type, find the way to display the View mode, then dynamically generate a dedicated View / Frame. Hey, it is really complicated inside, think about dizziness, no matter what it is, anyway, it is the genius. Oh, but I also understand the document / diagram structure, which is also a method, that is, the A-C model becomes a A-B-C model. C object model:

According to my understanding, the C object model is to implement the relevant type of data-oriented data-oriented in the C , how to arrange the function of the function (in order to achieve object-oriented properties more efficiently) Model. In order to achieve polymorphism, the object model has to design a Virtual Table table, and this table should consider the problem brought about by inheritance, and consider pointing to the pointer address of this table in an object assignment. What is the location of the derived memory (in binary form order), derived, especially if there are multiple base classes, how to "inherit" this virtual table, but also consider how to "inherit" base class Other members (many times, the derived class must copy all the members of all base classes, so there is a waste of memory, but it doesn't help why, so if it is best to raise the category as little as possible, and must be from the most "base" The class is derived. Because of the nature of polymorphism, inheritance, packaging, etc., the structure of the class, the destructor, and it has become more complicated. It is necessary to consider a lot of questions, because the class is no longer independent, between them One but there is a connection, things are more complicated. Class of function members, data members, in this case, the days are not good, their arrangements in memory require higher skill. Basically, you can know from the C object model, know why C will have a multi-state, inherit these features, and know why C syntax is like this, what is restrictions, etc. When I learned "Deep Exploration C Object Model", I have this feeling: all chapters of all books are basically uniform, and all chapters require you understand the reason, you have to return to memory allocation. On this topic; so I suggest, draw two detailed classes of four or five classes, including a variety of relationships, then when we look at this picture to learn this book, it will become Easy, although there is also a picture inside, but a little mess, I painted two classes to seek streamlined. COM with registry:

What is the COM interface? According to my current understanding, as in the model I have previously said, the A-C model becomes the B-B-C model, A and C are relatively independent executable, it is one A rule of a protocol that is used to connect A and C to provide for them to communicate. The COM interface defines a set of virtual functions. These functions will be implemented in A (or B); the interface is also a class structure and is also a parent class of A (or B), according to C polymorphism, we know, pass through The parent class can call the function of the party class (as long as the address of the derived object) is. Now the problem is how to get the object address of the derived class? I am not clear. Because C has more inherited this great nature, we can define a plurality of COM interfaces for A, using nested technologies to insert the COM interface to implement the connection; more complicated.

So far, I still can't understand what the registry is Dongdong. How does the computer manage memory; I have seen many books that inside technologies can know the book, but talk to these two aspects Gently brought it, so I can only guess. On the surface, the registry is a collection of some symbol strings, and these symbols have heard that some of the configuration information in the application, what kind of information is required to record some information? Let me think about it. When we use the program, it seems that the newly launched program interface and the last last time uses the closing interface seems to be the same (size, location, color, etc.), why do you like this? Is it possible to record poor difference in the registry, get this information from the registration table each time you start the program? I suspect that I haven't confirmed yet. When I learned COM, I seem to say that the path to executable program files is also recorded in the registry. I am very curious about memory management, but I've learned to be learned in school textbooks, so far can't meet my curiosity, and I must learn again. Application Framework:

When I first started learning VC, I often heard that MFC was a class library, and the API was simple package; huh, really? Is it simple package? Do not! At least the most important and most critical class is not! Several most important classes in MFC, such as CWINAPP, CView, CframeWnd, CDocument, CDOCTemplate, etc., not just "simple packaging"! They cooperate with each other, communicate through the flow of messages, and call each other's functions, etc., to call each other? It seems that it is quite good, but do it, if you want to call each other, you can still have a chaotic situation, and completely complete the basic framework of all the procedures we have developed, the problem to consider Why is it! ? Application Framework ---- The basic framework of the program? Yes, my understanding is true, Application Framework core thinking is based on the idea of ​​facing the development program, we make a perfect basic program model for us, we The developed program is based on it. Under the interim pair language C , we only need to send its own classes from the original class in the model, change some virtual functions, define some new functions, and make it easy to complete the features we want, many troublesome For things (such as processing Document / View / Frame, what we discussed before, program initialization, etc.) are resolved by the basic program model.

Application and user interaction:

Stupid I have always wanted to understand the problem between the application and the user's interaction from the C / C language; in my mind, I think there is a main () in the program, the program code inside is executed sequentially; I often have a headache when the application can run the code in the program in the program at the same time. Now, after I have read a lot of source, I realized that the procedure we have developed, in addition to the code to execute in the initialization of the launcher, there are many dead code segments, they have not implemented Get up, and the death of the dead things, I feel very stupid. However, death is relative, the operating system provides us to activate these code segments to provide a line: send a message to the program. Message With the help of the operating system, send to the application, the application performs a series of dead code, including the function corresponding to the execution message, and the order execution is meaningful. The news we send must have a corresponding handler so that the news will make sense; between the processing functions of each message, the programmer should make them independent as much as possible, it is good, the function is implemented. It's over, don't pull too much relationship with other messaging functions, this is the modular thinking. The program function module and the function module are not significant. Now I also know that the essence of order execution has not changed, just because there is a while loop to capture the message will become a bit incredible. I often deliberately pursue what it happened, and it is often important to violate it. Too much time is in these theories, and Ignore the practice, it turns into the skin; the most hateful thing is that the harvest made in the past, not only did it in practice, but also forget; It is found that the API itself is a big black box, tears, come to the end, or check the manual, or clearly understand the API function parameters and functions can be used; use the API and use the controls, I don't know what ghost it is. The dark box operation, we can't return to day, we can do it, and only understand the interface and parameters of the interface as much as possible. July 10, 2002

转载请注明原文地址:https://www.9cbs.com/read-25691.html

New Post(0)