Question 1:
Why does RichEditCtrl cause data loss when using DDX for data exchange? Why!
When we drag a control to a program, we usually press Ctrl + W to associate a variable with the control using the Class Wizard, and then rely on DDX/DDV for data exchange, if we use the same method to associate RICHEDIT with a CString type variable, there will be a problem, that is, if our data is larger than 64 KB, the data will be lost.
According to MSDN, WM_GETTEXT messages are not designed to be processed when the RICHEDIT data is larger than 64 KB. The code generated by the Class Wizard is to use DDX_Text to exchange data between controls and CString variables. The DDX_Text function calls the GetWindowText function, which sends the WM_GETTEXT message to the control to return the data in the control. The WM_GETTEXT message cannot accept more than 64 KB of data, resulting in RICHEDIT loss during data exchange.
To solve this problem, we need to use the DDX_RichText function. Add the following two functions to the project:
Dword callback ES2MemCallBack (DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG * pcb)
{
LPTSTR & lpszStrFill = * (LPTSTR *) dwCookie;
Memcpy (lpszStrFill, pbBuff, * pcb = cb );
LpszStrFill + = cb;
* Lpszstrfill = tchar ('/0 ');
Return 0;
}
Void afxapi ddx_richtext (cdataexchange * PDX, int NIDC, cstring & value)
{
Extern void afxapi afxsetwindowtext (hwnd hwndctrl, lpctstr lpsznew );
Hwnd hwndctrl = PDX-> prepareeditctrl (NIDC );
If (PDX-> m_bsaveandvalidate)
{
Int nlen =: getwindowtextlength (hwndctrl );
Lptstr lpszstrfill = value. getbuffersetlength (nlen );
Editstream es = {(dword_ptr) & lpszstrfill, 0, es2memcallback };
: Sendmessage (hwndctrl, em_streamout, sf_text, (lparam) & ES );
Value. releasebuffer ();
}
Else
{
Afxsetwindowtext (hwndctrl, value );
}
}
Then we need to modify the. CLW file of the project and open the. CLW file in text mode. Add the following two lines of code to the class format:
Extraddxcount = 1
Extraddx1 = 7; textover64kb; cstring; richtext; retrieves text in excess of 64kb from RichEdit controls
If the preceding steps are not used, we need to manually modify the code and change all DDX_Text to DDX_RichText. At the same time, move them out of the Class Wizard control code. That is, remove:
// {AFX_DATA_INIT (...)
//} AFX_DATA_INIT
// {AFX_DATA_MAP (...)
//} AFX_DATA_MAP
Reference:
Q280447 BUG: Text from a Rich Edit Control Is Truncated During Dialog Data Exchange (DDX)
Question 2:
When we use the Class Wizard to add EN_SETFOCUS to richedit, the function of EN_KILLFOCUS cannot respond. I found that this response function was not called at all. Even a MessageBox () function is not called.
It turns out that the default message ing is incorrect.
The correct message shot and response should be:
ON_EN_SETFOCUS (IDC_RICHEDIT1, OnSetfocusRichedit1)
ON_EN_KILLFOCUS (IDC_RICHEDIT1, OnKillfocusRichedit1)
The response function is in the following format:
Afx_msg void OnSetfocusRichedit1 ();
Afx_msg void OnKillfocusRichedit1 ();
However, if we use the Class Wizard to add directly, the generated code is:
ON_NOTIFY (EN_SETFOCUS, IDC_RICHEDIT1, OnSetfocusRichedit1)
On_policy (EN_KILLFOCUS, IDC_RICHEDIT1, OnKillfocusRichedit1)
We need to manually change it to the above format.
Another problem is that RichEditCtrl sometimes does not appear in the Control ID list of the Class Wizard. Therefore, we need to add the DDX/DDV function. Do it yourself! Pai_^
-- Sampledlg. h --
Class CSampleDlg: public CDialog
{
Public:
CSampleDlg (CWnd * pParent = NULL );
// Dialog Data
// {AFX_DATA (CSampleDlg)
Enum {IDD = IDD_SAMPLE_DIALOG };
CString m_edit; // variable generated by the Class Wizard for the EDIT Space
//} AFX_DATA
// Add one for RICHEDIT.
CRichEditCtrl m_richEditCtrl;
.......
Sampledlg. cpp --
......
Void CSampleDlg: DoDataExchange (CDataExchange * pDX)
{
CDialog: DoDataExchange (pDX );
// {AFX_DATA_MAP (CSampleDlg)
DDX_Text (pDX, IDC_EDIT, m_edit );
DDV_MaxChars (pDX, m_edit, 10 );
//} AFX_DATA_MAP
// Manually add the DDX_Control, DDX_Text and DDV_MaxChars functions for RICHEDIT
DDX_Control (pDX, IDC_RICHEDIT1, m_richEditCtrl );
DDX_Text (pDX, IDC_RICHEDIT1, m_richedit );
DDV_MaxChars (pDX, m_richedit, 10 );
}
Reference:
Q181664