A customer asks to customize a dialog box to display a few pieces of data that could be done in just a few minutes, but is suddenly stunned by a request: the customer asks one of the edit boxes to display the percentage, and the decimal point must be two digits. That is to say, if the percentage is 0, it will display 0.00. Although it is urgent to use CString to join the edit box, and add the Format member function and the scanf function, it will be done quickly. However, I was shocked by the cold sweat and found that the simplest thing was still not simple.
After carefully reading the information, and looking at the MFC source code, we found that there is no simple way to achieve it. Using CString as a variable, defining a double variable, and then converting it is also quite feasible. However, you need to add and determine whether the string is a double-type number, that is, simple verification data, A little troublesome.
Check the source code. We can find that the DDX_Text (CDataExchange * pDX, int nIDC, double & value) of MFC is different from the internal implementation of several other DDX_Text, and even the implemented files are different, in dlgfloat. cpp and dlgdata. cpp
Void AFXAPI DDX_Text (CDataExchange * pDX, int nIDC, double & value)
{
AfxTextFloatFormat (pDX, nIDC, & value, value, DBL_DIG );
}
Void AFXAPI DDX_Text (CDataExchange * pDX, int nIDC, DWORD & value)
{
If (pDX-> m_bSaveAndValidate)
_ Afx_DDX_TextWithFormat (pDX, nIDC, _ T ("% lu"), AFX_IDP_PARSE_UINT, & value );
Else
_ Afx_DDX_TextWithFormat (pDX, nIDC, _ T ("% lu"), AFX_IDP_PARSE_UINT, value );
}
The implementation of tracking AfxTextFloatFormat is as follows:
Void AFXAPI AfxTextFloatFormat (CDataExchange * pDX, int nIDC,
Void * pData, double value, int nSizeGcvt)
{
ASSERT (pData! = NULL );
HWND hWndCtrl = pDX-> PrepareEditCtrl (nIDC );
TCHAR szBuffer [32];
If (pDX-> m_bSaveAndValidate)
{
: GetWindowText (hWndCtrl, szBuffer, _ countof (szBuffer ));
Double d;
If (! _ AfxSimpleFloatParse (szBuffer, d ))
{
AfxMessageBox (AFX_IDP_PARSE_REAL );
PDX-> Fail (); // throws exception
}
If (nSizeGcvt = FLT_DIG)
* (Float *) pData) = (float) d;
Else
* (Double *) pData) = d;
}
Else
{
_ Stprintf (szBuffer, _ T ("%. * g"), nSizeGcvt, value );
AfxSetWindowText (hWndCtrl, szBuffer );
}
}
We can find that we only need to change
_ Stprintf (szBuffer, _ T ("%. * g"), nSizeGcvt, value );
AfxSetWindowText (hWndCtrl, szBuffer );
This part of code can achieve our goal.
1. Create a test project tddx
2. Add an edit box in the dialog box template to associate the double m_fPercent double variable.
3. Add Custom Data Exchange Functions
Extern void AFXAPI AfxTextFloatFormat (CDataExchange * pDX, int nIDC,
Void * pData, double value, int nSizeGcvt );
Void CTddxDlg: DDXEx_Text (CDataExchange * pDX, int nIDC, double & value, LPCTSTR lpszFormat)
{
If (pDX-> m_bSaveAndValidate ){
AfxTextFloatFormat (pDX, nIDC, & value, value, DBL_DIG );
}
Else {
HWND hWndCtrl = pDX-> PrepareEditCtrl (nIDC );
CString str;
Str. Format (lpszFormat, value );
// _ Stprintf (szBuffer, _ T ("%. * g"), nSizeGcvt, value );
: SetWindowText (hWndCtrl, str );
}
}
AfxTextFloatFormat is defined as a global function, so we only need to import it.
4. Modify DoDataExchange as follows:
Void CTddxDlg: DoDataExchange (CDataExchange * pDX)
{
CDialog: DoDataExchange (pDX );
// {AFX_DATA_MAP (CTddxDlg)
// DDX_Text (pDX, IDC_EDIT_PERCENT, m_fPercent );
//} AFX_DATA_MAP
DDXEx_Text (pDX, IDC_EDIT_PERCENT, m_fPercent, "% 0.2f ");
}
If you enter a non-number in the edit box, an error message is displayed.
I almost forgot. Because DBL_DIG exists, # include <float. h> is required.