According to the following article, oninitialupdate is called only once after the framework and will not be called later. Therefore, you can enable youview-> sendmessagea (wm_initialupdate) when creating other views, so that they can actively call it once, initialize oninitialupdate in your view!
Http://wenku.baidu.com/view/4f36c7d376a20029bd642dbd.html
Oninitialupdate Overview
The oncreate function is written as follows:
Oninitialupdate
Oninitialupdate Overview
The first function called by the framework after the View window is fully created. The Framework calls oninitialupdate before calling ondraw for the first time. Therefore, oninitialupdate is the most suitable place to set the logical size and ing mode of the rolling view. In terms of time, the order of the two is different. The constructor produces cost class objects, but no window is generated. After oncreate, the window is generated, and then the oninitialupdate of the view is generated.
The display of the view is initialized here. Simply put, oncreate only generates the basic structure and variables of the view. In oninitialupdate (), it mainly initializes controls in the view. Initialize each variable.
Example. To add a button and ComboBox control to the view, the oncreate function is written as follows:
Int cformview: oncreate (maid) {If (cview: oncreate (maid) =-1) Return-1; // todo: add your specialized creation code herecrect rect (20,20, 100,50); m_ctrlbutton.create ("button1", ws_child | ws_visible, rect, this, null ); // create a button control cfont * pfont = cfont: fromhandle (hfont): getstockobject (ansi_var_font); crect rect1 (350,100, 20 ); m_combox.create (ws_child | ws_visible | cbs_simple | cbs_nointegralheight | ws_vscroll, rect1, this, null); Return 0 ;}
// Void cformview: oninitialupdate () {cview: oninitialupdate (); // todo: add your specialized code here and/or call the base class // initialize the combox.addstring ("mondy"); m_combobox.addstring ("Tuesday"); m_combobox.addstring ("Wednesday "); m_combox.addstring ("Thursday"); m_combox.addstring ("Saturday"); m_combox.addstring ("Sunday ");}
Oninitialupdate function in the cview base class
The oninitialupdate function in the cview base class does nothing except to call the onupdate function. Therefore, if you overload the function in a derived class, you must call the oninitialupdate function of the base class or the onupdate function of the derived class.
Void cview: oninitialupdate () {onupdate (null, 0, null); // initial update} // onupdate function void cview: onupdate (cview * psender, lparam/* lhint */, cobject */* phint */) {assert (psender! = This); unused (psender); // unused in release builds // invalidate the entire pane, erase background tooinvalidate (true );}
SDI application view multiple calls oninitialupdate ()
The cview object of SDI is created in the view where the document is to be displayed. When the user changes the document, the data in the cdocument will be cleared, use cdocument: deletecontents (), and read the data of the new document to the cdocument object. Then, the system sends the wm_initialupdate message to call the window update view of the oninitialupdate () function. Therefore, the oninitialupdate () function is called every time you change the document view.
Event occurrence order:
Program startup: cmydocument object created-cmyview object created-View window created-cmyview: oncreate function called (if mapped)-cmydocument :: onnewdocument function called-cmyview: oninitialupdate function called-view object initialized-View window invalid-cmyview: ondraw function called-user editing data: functions in the cmyview class update cmydocument data members-> exit program: cmyview object deleted-> cmydocument
Object deleted
In the MFC program design, according to the traditional design, if the wm_paint message is processed, an onpaint function is generally derived and mapped to the wm_paint message for plotting. However, onpaint does not appear in many programs. An ondraw function performs more plotting operations. In the message ing list, the wing from wm_paint to ondraw is not seen. In fact, ondraw is not an onpaint ing, and ondraw occurs to achieve drawing consistency on different devices. First, the reader needs to understand that the wm_paint message appears to draw the screen. Therefore
:
Void mywnd: onpaint ()
{
Cpaintdc DC (this );
// Draw code here
}
Here, the cpaintdc constructor will automatically call beginpaint to obtain a screen DC and attach it to the DC object. When the DC object is destructed, the system automatically calls endpaint and changes invalidated rectangle to the validated state to end the painting. (Note: Repeated creation of cpaintdc instances will also fail.) If we draw in onpaint, We need to write an onprint function to redraw the image on the printer. In this way, the program designer needs to maintain two sets of code. To simplify the operation, the MFC framework places most of the painting operations in
In ondraw, onpaint and onprint only construct the corresponding DC, and then respectively call ondraw. That is to say, ondraw applies to all devices, while onpaint only applies to the screen.
During the design process, you must note that ondraw is actively called by the base class onpaint. If you inherit onpaint, you should either call the base class onpaint (do not create cpaintdc instances before, do not call beginpaint) or create a cpaintdc instance by yourself,
And call ondraw.