In general, a class is corresponding to a window template, but in many cases, the window we need is basically similar, but it is not exactly the same, with the same class, but some flags, such as window name, menu, The number of controls is different, defined multiple classes, and it is repeated, and the efficiency is low.
How can I make the same class to meet the needs of multiple different windows! Just started to consider using a common base class, including the function functions required by each window, and then each window inherits it, and do some necessary modifications in your own class. However, in the practice process, I found this idea that this idea is not wonderful. If you do a virtual base class, each inherited, for this most operation, the meaning of the button, mouse, keyboard, menu, is not big. Then I did a window that inherited from cdialog, and let each of the required windows inherit it. Then after the program runs, dynamically change the window title, dynamically load menu items, hide, and add certain controls, but those message mappings cannot be inherited (because they are a series of macros, do not belong to the program, naturally unable to inherit), and Dynamically hidden controls are better, but the dynamic increase control is not too easy. The first one is that the size of each window may not be the same. On the layout of the layout, it will be tired! The second is, many controls use the program Create, the annoying parameters, but also to familiarize our headache!
Later, I am in experiment, the same class, and map multiple windows separately.
This method, I didn't see someone used it, until the end, I realized that this skill, although I can save code, but the maintenance efficiency is too high, and it is not easy to transplant, and I haven't intended, then Perfect, in addition to C , VB, C # can not support this skill.
For non-Mode dialogs, the same class, mapping multiple windows, it is easy, as long as its constructor is defined as follows:
Test :: test (cruanjian2dlg * parent, int i) {m_hicon = AFXGETAPP () -> Loadicon (IDi_icon1 i); DLGID = i;
m_parent = pent;
Create (test :: IDD i, m_parent); setWindow (i);
MEMSET (INTBUFSEND0, 0, 4); // {{AFX_DATA_INIT (TEST) M_LUJING = _T ("); m_sock = null; file1 = null; InitOver = false; fivoltage0 = 0.0; fOutValue = 0.0; lDevNum813 = 0; lDevNum1730 = 0 ;; lDevNum727 = 0 ;; jieshouflag = false; udpjieshou = false; udpwangluo = false; fasongflag = false; m_edit1 = _T ( ""); m_edit2 = _T ( ""); M_EDit3 = _t (""); m_edit4 = _t ("); bexit = false;
//}} AFX_DATA_INIT} You can initialize the different windows with Test :: IDD i as long as those resource ID numbers of the window template. Each window title, icon, size, menu can be different, but the control position can also be different, but the number of controls must be the same. If a window, missing the control, when running, it will report the memory error, but Other windows, you may not need some controls at all, the solution is used in advance, one is on that window template, remove the display attribute of unwanted controls, second, after the window is initialized, use code dynamically hidden They are displayed.
Speaking of this, almost, you can complete the same class, map multiple windows, so you can save the same code to the same code, many cases as long as the Switch statement, according to DLGID, different window calls The content of the function is distinguished.
For the mode dialog box, the situation is slightly complex! Because it does not call the Create function, you cannot pass different parameters to create according to a parameter, to initialize the purpose of each of the different windows! We can only initialize different windows by establishing N different constructor, according to different constructor. In the header file, modify the value of the default window template: Enum {IDD = IDD_RUANJian2_Dialog, IDD1 = IDD_DIALOG4}; (This will not be able to use class wizard, because class wizard, only one "enum {IDD = IDD_Ruanjian2_dialog } ")
The constructor is as follows: cruanjian2dlg :: cruanjian2dlg (cwnd * pparent / * = null * /): cdialog (cruanjian2dlg :: IDd, pparent) {
......}
Cruanjian2dlg :: cruanjian2dlg (int CF, CWND * PParent): cdialog (cruanjian2dlg :: idd1, pparent) {...
}
When calling them, the way is as follows:
Switch () {casse 1: cruanjian2dlg dlg; m_pmainwnd = & dlg; dlg.domodal (); break;
Case 2: cruanjian2dlg DLG (1); (Note Different from the above construction function) m_pmainwnd = & dlg; dlg.domodal (); BREAK;
Case 3: ......
}
Finally, it is still necessary to repeat that this method does not seem to be particularly high, if there is not much window, the functions and layout are not highly consistent, this skill is not meaningful, but when the window has 7 or 8 The basic interface is also quite similar, and the functional requirements are similar, that is, when you need to change, use this kind of class, mapping multiple windows, can save you Ctrl C, Ctrl V the amount.