Dialog Box Data Exchange and Verification
FMD (http://www.fmdstudio.net)
Data Exchange and Verification Mechanisms (DDX, DDV) of dialog boxes can be coordinated between controls on dialogs and object data members.
Including the connection between the control window and the control object, the connection between the control window and the dialog data member, and the legality verification of the data member.
These relationships are recorded in Virtual Void DodataExchange (CDataExchange * PDX);
E.g:
DDX_Control (PDX, IDC_Button1, M_BTN); // CButton object to the association of the control window IDC_Button1
DDX_Text (PDX, IDC_EDit1, m_INT); // Integer data to the association of the editing window
DDV_MINMAXINT (PDX, M_INT, 0, 40); // Range Verification of Integer Data
If necessary, you can write a verification function to implement a specific content check.
Related member functions:
CDIALOG :: OnInitdialog ();
CWnd :: DODATAEXChange ();
CDIALOG :: DODATAEXCHANGE ();
1. Control association
DDX_Control implements the association of the control window and the C control object.
Since the dialog is established in a template, the control window is established in advance.
The C control object is attached to the control window with SubclassWindow method, and the behavior of the management window is managed.
Internal process analysis:
1onInitDialog calls the base class cDialog :: OnInitdialog ();
Bool cDialog :: OnInitDialog ()
{
.....
/ / Perform Updatedata (), parameter is false, indicating initialization
IF (! Updatedata (false))
{
....
}
.....
}
2UPDATEDATA ()
Bool CWnd :: Updatedata (Bool BsaveandValidate)
{
....
// Call the virtual function DODATAEXCHANGE
CDataExchange DX (this, bsaveAndvalidate);
....
Try
{
DODATAEXCHANGE (& DX);
Bok = true; // it worked
}
.....
}
3DodataExchange
Void C ???? DLG :: DODATAEXCHANGE (CDataExchange * PDX)
{
....
// Object to the association of the window.
DDX_Control (PDX, IDC_Button1, M_BTN);
....
}
4DDX_Control
Void AFXAPI DDX_Control (CDataExchange * PDX, INT NIDC, CWND & RCONTROL)
{
IF (rcontrol.m_hwnd == null) // If it is not closed
{
// Window handle
HWND HWNDCTRL = PDX-> PrepareCtrl (NIDC);
// SubclassWindow implementation association.
IF (! rcontrol.subclasswindow (hwndctrl))
{
....
}
....
}
}
At this point, the control object is associated with the sub-window on the dialog, and the sub-window can be managed by the operation of the control object.
Data association
The approximate process is similar to the front. Update (TRUE), the parameter TRUE is called read and verify data when dialog Onok ().
You can call Update (TRUE) to complete the check and conversion of the UPDATE (TRUE) to the member data at any time as needed.
Process analysis (with the association of the edit box to the integer as an example)
4DodataExchange
Void C ??? DLG :: DODATAEXCHANGE (CDataExchange * PDX)
{
....
DDX_Text (PDX, IDC_EDit1, M_INT);
....
}
2DDX_Text
Void AFXAPI DDX_Text (CDataExchange * PDX, INT NIDC, INT & VALUE)
{
IF (PDX-> M_BsaveAndValidate) // Read and verify
_Afx_ddx_textwithformat (PDX, NIDC, _T ("% D"), AFX_IDP_PARSE_INT, & VALUE
ELSE // Initialization
_Afx_ddx_textwithformat (PDX, NIDC, _T ("% D"), AFX_IDP_PARSE_INT, VALUE);
}
3_AFX_DDX_TEXTWITHFORMAT
When the dialog is initialized, the M_BsaveAndValidate parameter is false;
When performing data reading, the M_BsaveAndValidate parameter is TRUE;
AFX_STATIC VOID AFX_CDECL _AFX_DDX_TEXTWITHFORMAT (CDataExchange * PDX, INT NIDC,
LPCTSTR LPSZFORMAT, UINT NIDPROMPT, ...)
// ONLY Supports Windows Output Formats - No floating point
{
....
// Window handle
HWND HWNDCTRL = PDX-> prepareEditctrl (NIDC);
TCHAR SZT [32];
IF (PDX-> M_BsaveAndValidate)
{
// read
// the folloading works for% d,% u,% ld,% lu
:: getWindowText (HWndctrl, SZT, _COUNTOF (SZT));
IF (! _afxsimplescanf (SZT, LPSZFORMAT, PDATA)
{
AfxMessageBox (NidPrompt);
PDX-> fail (); // throws exception
}
}
When the ELSE // dialog is initialized, the contents of the member data content initial window content.
{
// Initialize the window content.
WVSPrintf (SZT, LPSZFORMAT, PDATA);
AfxSetWindowText (HWndCtrl, SZT);
}
....
}
3. Data verification
Process analysis (as an example of an integer numerical range
Void C ???? DLG :: DODATAEXCHANGE (CDataExchange * PDX)
{
....
DDV_MINMAXINT (PDX, M_INT, 0, 40);
....
}
Void AFXAPI DDV_MINMAXINT (CDataExchange * PDX, INT Value, Int Minval, int maxval)
{
Assert (minval <= maxval);
//verification
Value
// Remove the error
_AFXFAILMINMAXWITHFORMAT (PDX, (long) minval, (long) maxval, _t ("% ld"),
AFX_IDP_PARSE_INT_RANGE);
}