Click to See Complete Forum and Search --> : Rich Edit "Ignoring" Messages?


jlanza
February 26th, 2008, 08:22 AM
Hi I was beginning to work with a Rich Edit control in VS 2008. I am able to create the rich edit via CreateWindowEx, but something isn't working when I try to send it a message. The first test piece of code I was trying to write was a way to save everything that had been typed into the rich text.

void SaveBuffer(HWND hWnd)
{
GETTEXTLENGTHEX gtlxstruct;
gtlxstruct.codepage = 1200;
gtlxstruct.flags = GTL_NUMBYTES;

DWORD dwBufferSize = ::SendMessage(hWnd, EM_GETTEXTLENGTHEX, (WPARAM) &gtlxstruct, 0);

GETTEXTEX gettextexstruct;
gettextexstruct.cb = dwBufferSize;
gettextexstruct.codepage = 1200;
gettextexstruct.flags = GT_DEFAULT;
gettextexstruct.lpDefaultChar = NULL;
gettextexstruct.lpUsedDefChar = NULL;

char* pszSaveBuffer = new char[dwBufferSize + 1];
LRESULT lResult = ::SendMessage(hWnd, EM_GETTEXTEX, (WPARAM) &gettextexstruct, (LPARAM) pszSaveBuffer);

ofstream outfile(_T("new.txt"), ofstream::binary);
outfile.write(pszSaveBuffer, dwBufferSize + 1);

outfile.close();

delete [] pszSaveBuffer;
}

Both the dwBuffSize and lResult are 0. Even when I say assign 100 to dwBuffSize lResult is still 0. Could someone please help?

Thanks!

olivthill
February 26th, 2008, 09:31 AM
Here is my solution in C.
// Write file
if ((fp = fopen(rtf_filename, "wb")) == NULL) {
char info_msg[256];
sprintf(info_msg, "Cannot open %s.\n", rtf_filename);
MessageBox(hwnd, info_msg, "Error", 0);
return(-4);
}

i9 = Edit_GetTextLength(hRTF);
for (i = 0; i < i9; i++) {
Edit_SetSel(hRTF, i, i+1);
if (SendMessage(hRTF, (UINT) EM_GETSELTEXT, (WPARAM) 0, (LPARAM) s) > 0) {
SendMessage(hRTF, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM) &cf);
if (cf.dwEffects & CFM_BOLD)
fwrite("<b>", 3, 1, fp);
if (cf.dwEffects & CFM_ITALIC)
fwrite("<i>", 3, 1, fp);
if (cf.dwEffects & CFM_UNDERLINE)
fwrite("<u>", 3, 1, fp);
if (fputc((int)(s[0]), fp) <= 0) {
char info_msg[256];
sprintf(info_msg, "Cannot write in %s.\n", rtf_filename);
MessageBox(hwnd, info_msg, "Error", 0);
fclose(fp);
return(-5);
}
if (s[0] == 0x0d) fputc((int)(0x0a), fp);
if (cf.dwEffects & CFM_UNDERLINE)
fwrite("</u>", 4, 1, fp);
if (cf.dwEffects & CFM_ITALIC)
fwrite("</i>", 4, 1, fp);
if (cf.dwEffects & CFM_BOLD)
fwrite("</b>", 4, 1, fp);
}
}
Edit_SetSel(hRTF, 0, 0);

fclose(fp);
N.B. Sometimes, there are functions named Edit_xxxx() which can be used instead of SendMessage().

jlanza
February 26th, 2008, 10:07 AM
I tried using the Edit_GetTextLength macro like you said

DWORD dwBufferSize = Edit_GetTextLength(hWnd)

but that is still 0...

Any other suggestions?

heteroy
February 28th, 2008, 09:01 AM
You are using the codepage for Unicode yet you use char instead of WCHAR in your code snippet. Are you sure you have the right code page?

JohnCz
March 1st, 2008, 07:09 PM
jlanza,
Could you post a snippet of the code that you use to create edit control?