Problem 1:richeditctrl data loss occurs when using DDX for data exchange? why!
When we drag a control into the program, usually the hair is ctrl+w, with the Class wizard to the control associated with a variable, and then rely on DDX/DDV for data exchange, if we use the same method to give RichEdit a CString type of variable will have a problem, Is that if our data is greater than 64K, the data will be lost.
By checking MSDN, the Wm_gettext message was not designed to be handled when the RichEdit data was greater than 64K. The Class Wizard generates code that uses DDX_Text to exchange data for controls and CString variables. Exactly, the DDX_Text function calls the GetWindowText function, which emits a wm_gettext message to the control to return the data in the control. Wm_gettext messages cannot accept more than 64K of data, which results in the loss of RichEdit in the event of a 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 (' ");
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);
}
}