MFC category
1 root: cobject
2 application architecture classes: cwinapp/cframewnd /...
3 window, dialog, and control classes: cwnd/cdialog /...
4 Drawing and printing classes: cgdiobject/cprintinfo /...
5 simple data type classes: cstring/crect /...
6 array, list, and map classes: ctypedptrarray /...
7 file and database classes: cstdiofile/cdatabase /...
8 Internet and networking classes: csocket /...
9 Ole classes...
10 debugging and exception classes: cmemorystate/cexception /...
Oncreate initdialog differences:
The window has not been created during wm_create. You can assign values to the member variables or modify the basic content of the window, such as the style, but cannot operate on the window control, because the window has not been created yet.
The window has been created when initdialog. You can move the window, modify the style, and so on. You can also assign values to the member variables,
Generally, you need to create a form (oncreate) first, and then initialize it (oninitdialog ).
Wm_initdialog
The wm_initdialog message is sent to the dialog box procedure immediately before a dialog box is displayed.
Wm_create
The wm_create message is sent when an application requests that a window be created by calling the createmediawex or createwindow function.
Differences between create and oncreate:
Oncreate is a message response function that responds to the wm_create message, while the wm_create message is called by the create function (# Add occurs ?) .
In the View class, create is a virtual function called by the framework. It is used to "generate a subwindow of a window ". The oncreate function is used to "indicate that a window is being generated ".
After a window is created (create), The wm_create message is sent to the operating system. The oncreate () function is mainly used to respond to this message. In MFC, a message ing mechanism is used to respond to messages, that is, functions can be used to respond to corresponding messages. Take the cmainframe class as an example. After a window is created, the wm_create message is generated. In the oncreate function, we can add things in the window, such as the button, Status Bar, and toolbar. These subwindows are generally defined as a member variable in the class, because the lifecycle must be guaranteed. It generally starts with M _ to represent a member (member ).
Oncreate () does not generate a window, but sets window attributes such as style and position before the window is displayed. Create () registers and generates a window.
Create () does not correspond to the Message wm_create, and oncreate () is. Create () is only used to generate a window, just like create () in the dynamic creation control.
Msdn:
The cwnd object names es this call after the window is created but before it becomes visible. oncreate is called before the create or createex member function returns.
Override this member function to perform any needed initialization of a derived class.
Ondraw and onpaint:
Onpaint is the message processing function of the wm_paint message. ondraw is called in onpaint. Generally, your own drawing code should be placed in ondraw.
Onpaint () is a member of the cwnd class and is responsible for responding to the wm_paint message. Ondraw () is a member function of cview and does not respond to messages. When the view becomes invalid (including changing the size, moving, overwriting, etc.), Windows sends the wm_paint message. The onpaint handler function of this view responds to the message by creating a DC object of the cpaintdc class and calls the ondraw member function of the view. ondraw must also be called at the end of onpaint, so it is generally drawn in the ondraw function.
The wm_paint message is sent when the updatewindow or redrawwindow member function is called.
In onpaint, beginpaint is called to obtain the display device environment of the customer zone, and the GDI function is called to perform the Drawing operation. After the Drawing operation is complete, endpaint is called to release the display device environment. Ondraw is called between beginpaint and endpaint.
1) In the MFC structure, onpaint is a member function of cwnd. ondraw is a member function of cview. (# add, of course, cview is a subclass of cwnd)
2) onpaint () calls ondraw () and onprint will also call ondraw (). Therefore, ondraw () is a common operation for displaying and printing.
Onpaint is the re-painting message processing function triggered by the wm_paint message. onpaint calls ondraw for plotting. Onpaint first constructs a cpaintdc class instance, and then calls the virtual function onpreparedc as a parameter for some processing before the painting, such as setting the ing mode, and finally calls ondraw. Ondraw and onpreparedc are not message processing functions. Therefore, when ondraw is called not because of the onpaint caused by the re-painting message, for example, when drawing in onlbuttondown and other message processing functions, you must call onpreparedc by yourself.
As for cpaintdc and cclientdc, cpaintdc is a device environment class. In onpaint, it is passed as a parameter to onpreparedc for setting the device environment. Cwindowdc is comparable to cclientdc. One is to describe the customer region and the other is to describe the entire screen.
Ondraw should be used for drawing windows derived from the cview or cview class.
What is the difference between ondraw () and onpaint?
First, we need to clarify that the cview class is derived from the cwnd class. Onpaint () is a member of the cwnd class and is responsible for responding to the wm_paint message. Ondraw () is a member function of cview and has no message response function. This is why only ondraw does not have onpaint in the View class when you use VC program code. In a dialog box-based program, only onpaint (# add, because there is no view class) is available ).
Secondly, we have already said in the beginning of article 3, "Learning MFC every day. To draw or display a graph on the screen, you must first establish a device environment DC. In fact, DC is a data structure that contains a description of the drawing properties of an output device (not just a 17-inch pure screen display, but an output device such as a printer. MFC provides the cpaintdc class and cwindwodc class for real-time response, while cpaintdc supports repainting. When the view becomes invalid (including size change, movement, overwriting, etc.), Windows sends the wm_paint message to it. The onpaint handler function of this view responds to the message by creating a DC object of the cpaintdc class and calls the ondraw member function of the view. Generally, we do not need to write the override onpaint to process the member function.
/// The default standard redraw function of cview
Void cview: onpaint () // see viewcore. cpp. This is the source code.
{
Cpaintdc DC (this );
Onpreparedc (& DC );
Ondraw (& DC); // ondraw called
}
/// Default standard onprint function of cview
Void cview: onprint (CDC * PDC, cprintinfo *)
{
Assert_valid (PDC );
Ondraw (PDC); // call draw
}
Since ondraw is also called at the end of onpaint, we generally draw it in the ondraw function. The following is a typical program.
/// The drawing code in the view first retrieves the pointer to the document, and then calls the drawing through DC.
Void cmyview: ondraw (CDC * PDC)
{
Cmydoc * pdoc = getdocument ();
Cstring S = pdoc-> getdata ();
Getclientrect (& rect); // returns a cstring crect rect;
PDC-> settextalign (ta_baseline | ta_center );
PDC-> textout (rect. Right/2, rect. Bottom/2, S, S. getlength ());
}
Finally: now we understand the relationship between them. Therefore, we generally use onpaint to maintain the customer area of the window (for example, adding a background image to the customer area of our window ), use ondraw to maintain the customer area of the view (for example, drawing in the view with the mouse ). Of course, you may not follow the above rules, as long as you achieve the purpose and there is no problem, how to do it all. Supplement: we can also use the invalidate (), validatergn (), validaterect () function to force the re-draw window. For details, see msdn.
You can draw user areas in ondraw. Onpaint only redraws when the window is invalid and does not retain the content drawn by cclientdc.
The two functions are different and related:
1. Difference: ondraw is a pure virtual function, defined as virtual void ondraw (CDC * PDC) = 0; while onpaint is a message response function that responds to wm_panit messages, it is also a window re-painting message.
2. Contact: We generally reload the ondraw pure virtual function instead of directly responding to the wm_panit message when constructing the class, this is because the ondraw function is called in the wm_panit message response function of the cview class. If the wm_paint message is returned in the cmyview class and the ondraw function is not explicitly called, the ondraw function is not called during window re-painting.
Almost all plotting in an application occurs in the ondraw member function of the view. This member function must be rewritten in the View class. (Mouse drawing is a special case, which is discussed in the interpretation of user input through views .)
Ondraw rewrite:
You can call the document member function provided by you to obtain data.
Call the member function of the device context object passed to ondraw to display data.
When the document data is changed in some way, the view must be repainted to reflect the change. The default onupdate implementation invalidates the entire Workspace of the view. When the view becomes invalid, Windows sends the wm_paint message to it. The onpaint processing function of this view responds to the message by creating a device context object of the cpaintdc class and calls the ondraw member function of the view.
When the wm_paint message is not added and the window is re-painted, ondraw will respond to the message... when a wm_paint message is added for processing and the window is re-painted, the wm_paint message is shipped and onpaint is used to respond to the message. in this case, ondraw cannot be called implicitly. must be explicitly called (CDC * PDC = getdc (); ondraw (PDC );).. (# Add, which is added to the onpaint function)
Implicit call: When onpaint calls a message, the system automatically calls cview: ondraw (& PDC ).
Imagine that the content displayed in the window is similar to the printed content. Therefore, in general, ondraw is used for painting. When the window foreground needs to be refreshed, the system will call onpaint, while onpaint generally calls ondraw () after initialization of DC ().
Onerasebkgnd () is called by the system when the window background needs to be refreshed. An obvious example is to set the background color of the window (you can put this in onpaint, but it will cause flickering ).
As for how to define the background and prospects, we need to analyze specific issues. In general, it is easy for you to differentiate.
Indeed, onpaint () is used to respond to the wm_paint message. The ondraw () virtual function is called with different parameters based on whether the onpaint () class is printed or on the screen. So in ondraw (), you can differentiate printing from screen painting.
In fact, MFC has done a lot of work before and after printing, calling a lot of virtual functions, such as onprepareprint.
For ondraw ()
This method is called by the Framework to render an image of the document. the framework callthis method to perform screen display, printing, and print preview, And it passes a different device context in each case. there is no default implementation.
Differences between ondrawitem and drawitem:
1. ondrawitem: Message Processing Function of the wm_drawitem message
The parent window calls this function when the child control has the self-painting property and the control needs to be re-painted. When the control with the owner draw property needs to be re-painted, the ondrawitem
This function is called when the visible part of the Custom button control, Combo control, list box control, or menu needs to be drawn.
Ondrawitem ()-à drawitem ();
2. drawitem: virtual function, which must be reloaded
If drawitem is used for a widget, you need to add the owner draw style to the widget and then reload the drawitem function of the widget class, if the parent window of the control provides the on_wm_drawitem message ing macro and the ondrawitem function is reloaded, the re-painting message is processed by the parent window, the parent window calls the ondrawitem of the base class to call the drawitem function of the derived child control.
(# Add seems to have an incorrect expression, such as the sentence "The ondrawitem function has been reloaded". What the fuck Sb said is not completely understood yet. It is to be solved)
Differences between onctlcolor and ctlcolor:
Onctlcolor is the message Response Function in the parent window, which is used to process the drawing of child controls,
Ctlcolor is the response function in the Child control. This control has the self-painting function, which is drawn by itself and belongs to the Message reflection mechanism.
Onctlcolor is the wm_ctlcolor message sent from the subcontrol,
Ctlcolor is the wm_ctlcolor message sent by the reflection control.
Difference between clienttoscreen and screentoclient:
Clienttoscreen () is used to convert the window coordinate to the screen coordinate.
Screentoclient () is used to convert screen coordinates into window coordinates.
The screen coordinates are relative to the upper left corner of the screen, while the window coordinates are relative to the upper left corner of the window user area.
In VC, some functions use window coordinates and some use screen coordinates to distinguish between them.
A form is divided into two parts: System zone and customer zone.
For example, the title and menu are in the system zone, which is controlled by the system. The customer zone is your region !!!
Width and height refer to the whole. clientwidth and clientheight refer to the customer zone.
System zone !!!
Clienttoscreen converts coordinates from the current form to full screen !!!
Screentoclient converts screen coordinates to coordinates relative to the current form !!!!
Differences between wparam and lparam:
Wparam and lparam are the products left behind by the Win16 system. In win16api, wndproc has two parameters:
One is a 16-bit integer variable of the Word type, and the other is a 32-bit integer variable of the long type. Therefore, according to the Hungarian naming method, the 16-bit variable is named wparam, and the 32-bit variable is named lparam.
In WIN32API, the original 16-bit variable is also extended to 32-bit, so the size of wparam and lparam is the same.
In the early days of WIN32API, two macros wparam and lparam were defined to ensure code portability with win16api.
At that time, the W prefix was retained because the wparam Macro started with W and the programmer was reminded to notice portability. Of course, Win16 has already left the stage of history, this prefix is also customary.
The differences between invalidate, invalidate (false), and invalidate (true:
1. Void invalidate (bool berase = true );
This function is used to invalidate the entire window client area. If the customer area of the window is invalid, it indicates that re-painting is required. For example, if a window that is covered by other windows is changed to a front-end window, the originally covered window is invalid and needs to be re-painted. In this case, Windows will place the wm_paint message in the message queue of the application. MFC provides the message processing function onpaint of wm_paint for the window class. onpaint is responsible for repainting the window. There are some exceptions to the View class. The ondraw function is called in the onpaint function of the View class, and the actual re-painting is completed by ondraw. If the berase parameter is true, the background in the repainting area is erased. Otherwise, the background remains unchanged.
It differs from updatewindow () in the following ways:
Updatewindow () is used to re-paint the window immediately. After the invalidate and other functions are called, the window will not be re-painted immediately. This is because the priority of the wm_paint message is very low. It must be processed after other messages in the message queue are sent. You can call the updatewindow function to send wm_paint directly to the target window, causing the window to be re-painted immediately.
2. Differences between invalidate (false) and invalidate (true)
(1) invalidate (false) normal (2) invalidate (true) screen flushing (3) if not used, the file cannot be automatically displayed each time it is opened. You can manually change the window size, then it can be displayed normally.
Postmessage, sendmessage, peekmessage, and getmessage are different:
Postmessage returns the execution program immediately after the message is sent. The message is placed in the message queue. After sendmessage is sent, it can be returned to continue executing the program after the message is processed. messages are not placed in the queue. |
More specifically, postmessage sends a message to a window.
Sendmessage directly calls the window processing function of this window to process a message and wait for its return,
A. Getmessage is similar to sendmessage. A message is sent back, otherwise it is blocked... and the original queue message is also retrieved (Deleted.
B. Peekmessage is similar to postmessage. If no message is returned, you can also choose whether to delete the original message...
1. getmessage () transfers control to your program only after receiving the message, and peekmessage () transfers control to your program no matter whether there is any message: if there is a message, the returned result is true, no message returns false.
2. The main function of getmessage () is to "retrieve" A message from the message queue. After a message is taken out, it is no longer used in the message queue. peekmessage () the main function is to "peek" A message. If a message exists, the message returns true without returning false messages. However, peekmessage () allows you to "retrieve" A message from the message queue. This is the purpose of peekmessage ()'s fifth parameter: If pm_remove is selected, the message is removed from the queue, for example, if pm_noremove is selected, peekmessage () is the "text publisher", only "peek", while retaining the message.
3. Getmessage () is "waiting for message processing" each time, while peekmessage () is "checking whether there is any message ".
Code related to peekmessage:
While (true)
{
If (peekmessage (& MSG, null, 0, 0, pm_remove ))
{
If (msg. Message = wm_quit)
Break;
Translatemessage (& MSG );
Dispatchmessage (& MSG );
}
Else
{
// Other program lines to do some work
}
}
Difference between declare_dyncreate and declare_dynamic:
Implement_dynamic is the macro that implements "runtime type recognition", which corresponds to declare_dynamic (declaring the "runtime type recognition" macro ). That is to say, if you see implement_dynamic in the. cpp file, you must have the declare_dynamic declaration in the. h file.
Therefore, declare_dynamic/declare_dynamic is a macro defined to determine which class the runtime object belongs. You can use the runtime_class macro and cobject: iskindof function at runtime to determine the class to which the object belongs.
Declare_dyncreate/implement_dyncreate is a macro defined for "dynamic creation" class instances. New can be used to create objects, but not dynamic. For example, if you want to create a class instance in a program based on the class name that supports input, the following method is not acceptable:
Char szclassname [60];
Cin> szclassname;
Cobject * POB = new szclassname; // cannot pass
Therefore, the function defined by declare_dyncreate/implement_dyncreate is required to implement the dynamic creation class.
Differences between memmove and memcpy:
1. when the SRC and DEST memory areas overlap, memmove can ensure that the first n Bytes of the SRC memory area can be correctly copied to the memory in the Dest;
2. When the SRC address is lower than the Dest address, the results are the same. In other words, the difference between memmove and memcpy is only reflected in the case that the head of DeST overlaps with the tail of SRC;
3. memcpy is implemented by assembly, which is more efficient.
Differences between pretranslatemessage and wndproc:
In MFC, pretranslatemessage is a virtual function. We can reload it to process keyboard and mouse messages.
In the SDK, this is different. We must process the message in the callback function lresult callback wndproc (hwnd, uint message, wparam, lparam: it is similar to pretranslatemessage. Only the MFC encapsulation is better.
This function can be used to filter window messages before being distributed to the trnaslatemessage and dispatchmessae () functions. the default implementation is to complete the translation of the acceleration key. because you must call the cwinapp: pretranslatemessage () function in your overloaded version. obviously, this function is implemented before the translatemassage () function in the SDK.
In MFC, pretranslatemessage is the next operation of the getmessage (...) function. That is, after getmessage (...) acquires a message from the message queue, it is processed by pretranslatemessage,
If the return value is false, it will be handed over to translatemessage and dispatchmessage for processing (that is, entering windowproc );
Pretranslatemessage is just something like hook callback function. It gives you a chance to process messages first before translatemessage. Pseudocode:
MSG;
While (getmessage (& MSG, null, 0, 0 ))
{
Pretranslatemessage (& MSG );
Translatemessage (& MSG );
Dispatchmessage (& MSG );
}
If sendmessage is used, the message is directly delivered to windowproc for processing. Therefore, getmessage does not obtain the sendmessage message. Of course, pretranslatemessage will not be called.
If postmessage is used, the message enters the message queue and is obtained by getmessage. pretranslatemessage has the opportunity to process the message.
Invalidate () -- redrawwindow () -- updatewindow:
Invalidate () is to force the system to re-draw, but not necessarily immediately re-draw. Because invalidate () is only a notification system, the window has become invalid. Force the system to call wm_paint, and the message is only post (sent), that is, put the message into the message queue. The exposure is repainted only when the message wm_paint is executed.
Updatewindow only sends the wm_paint message to the form. before sending the message, determine whether the getupdaterect (hwnd, null, true) can be drawn in the customer region. If not, wm_paint is not sent. Sending is directly sent to the corresponding window without passing through the message queue. Therefore, this function can update the window immediately.
Redrawwindow () is a double feature with invalidate () and updatewindow. The status of the Declaration window is invalid. Update the window immediately and call wm_paint to process the message immediately.
A section of spam is provided. You can only check it. Do not take it seriously:
In the View class, create is a virtual function called by the framework. It is used to "generate a subwindow of a window ".
The oncreate message response function is used to "indicate that a window is being generated ".
The create function of a cwnd is called by the owner of the current cwnd. In cwnd: Create, The oncreate function is called, but the create function has not exited yet, some parts of cwnd have not been created yet. Therefore, commandtoindex cannot be called in toolbar: oncreate, because commandtoindex can be called only after ctoolbar is created. Otherwise, the returned value is always-1.
Oncreate () does not generate a window, but sets window attributes such as style and position before the window is displayed,
Create () registers and generates a window
Create () does not correspond to the Message wm_create, and oncreate () is. Create () is only used to generate a window, just like create () in the dynamic creation control.
Differences among setactivewindow, setfocus, setforegroundwindow, and bringwindowtotop:
Setforegroundwindow () is used to bring a program to the foreground. It is not necessarily activated.
Setactivewindow () is to activate a program.
Setfocus () is a place that can be input, such as an input box, to get the cursor.
Bringwindowtotop
From: http://blog.csdn.net/weiwangchao_/article/details/6923976