[Author Press] This article is mainly written for beginners to write some issues when using dialog boxes. If you are very skilled in the use of dialogs, please skip this article. [For the phenomenon] Many beginners, often written such a program to modify the title of a button in the dialog: CMYDIALOG DLG; DLG.M_BTN.SetWindowText ("MyButton); where M_BTN is defined as cbutton m_btn; this program After running, you will be wrong, Debug, the error will point to an assertion statement assert (:: iswindow (m_hwnd)); the report window pointer is empty. Look at M_HWnd, equal to 0x00000000. In fact, the handle of the cbutton object of M_BTN is empty. [Cause Analysis] The reason for this error, obviously M_BTN's window object has not been created yet. Let's take a look at the CMYDIALOG DLG this constructor to see the description of the MSDN to the CDIALOG constructor. In fact, it is just a simple creation of a dialog instance. Here I understand this, that is, the dialog is a container, and each control on the dialog is the object contained in the container. The constructor of the dialog is only constructed with a container and does not create each object in the container. I think everyone should understand why M_BTN's handle is empty. When is the control in the dialog box? I personally didn't care carefully, but from the experience of personal use, I should be created in the create function of the Domodal function and dialog. Because when using a mode dialog, m_btn.setwindowtext ("MyButton" statement does not have problems in the OnInitDialog function. So you should be created in Domodal. As for how Domodal is created, you are welcome to give explanations with you. For non-mode dialogs, the following programs are run without problems: CMYDIALOG DLG; DLG.CREATE (IDD_MYDIALOG, THIS); DLG.M_BTN.SetWindowText ("MyButton"); the creation of the visible control should be in the CREATE function. [Solution] What should I do if I don't have such a function? The author's solution is to increase member variables in the dialog box, such as m_sbtntitle. The delivery of data has the following three methods: 1. Increase the set function, such as setbTntitle (CString str); cmydialog :: setbtitle (cstring str) {m_sbtntitle = str;} The calling program is as follows: CMYDIALOG DLG; DLG.SetBTiti ("MyButton);
2. Define m_sbtntitle into a public type.
Then dlg.m_sbtntitle = "mybutton";
3. Modify or overload a new constructor, such as: CMYDIALOG :: CMYDIALOG (CSTRING STR) {m_sbtntitle = str;} Finally, the initialization function of the dialog box is increased by m_btn.setWindowText (m_sbtntitle); There are also some netizens to provide a global variable. Of course, first, affirmation of global variables can be done. However, the author does not agree with the method of global variables. For object-oriented programming and design, global variables should be used as little as possible because global variables will increase the coupling between the modules, which is contrary to object-oriented ideas. As the goto statement, it is easy to use. Here, the author is not against the global variable, but to see the situation. The author has developed a Japanese project, roughly calculated, the global variable is not 1000, such a program's maintenance difficulty can be known. I hope this article can help beginners will take less skeletons on the VC road, and integrate into the VC development army as soon as possible.