Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: H0CKEY on January 14, 2004, 07:56 AM

Title: Writing To a Richedit
Post by: H0CKEY on January 14, 2004, 07:56 AM
Could I get some help. I want to send text to my richedit window on my dialog. COuld someone show me how thsi would be accomplished. thank you
Title: Re:Writing To a Richedit
Post by: Skywing on January 14, 2004, 08:58 AM
The simple way is to set the selection to the bottom of the window, and then replace it with the text that you want to add.

See EM_EXSETESEL (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/richedit/richeditcontrols/richeditcontrolreference/richeditmessages/em_exsetsel.asp) and EM_REPLACESEL (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/editcontrols/editcontrolreference/editcontrolmessages/em_replacesel.asp).  You can use the EM_SETCHARFORMAT (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/richedit/richeditcontrols/richeditcontrolreference/richeditmessages/em_setcharformat.asp) message to control how the text looks.
Title: Re: Writing To a Richedit
Post by: Okee on December 16, 2004, 10:32 PM
Sorry for bringing back old topics, but it's probably better than creating repeated ones.

I'm using EM_SETSEL, and adding text to my RichEdit. On msdn it says if the wParam is -1, then all selected text will be deselected. Well, I'm setting wParam to -1, and it's not deselecting my text! Anyone know why or have this happen also?


void AddText(HWND hRichEdit, char *szStatic, ...)
{
char szTextToAdd[2048] = "";
va_list vaArg;
va_start(vaArg, szStatic);
vsprintf(szTextToAdd, szStatic, vaArg);
va_end(vaArg);

SendMessage(hRichEdit, EM_SETSEL, -1, -1);
SendMessage(hRichEdit, EM_REPLACESEL, FALSE, (LPARAM)szTextToAdd);
SendMessage(hRichEdit, EM_SETSEL, -1, 0);
}
Title: Re: Writing To a Richedit
Post by: UserLoser. on December 16, 2004, 10:45 PM
Quote from: Okee on December 16, 2004, 10:32 PM
Sorry for bringing back old topics, but it's probably better than creating repeated ones.

I'm using EM_SETSEL, and adding text to my RichEdit. On msdn it says if the wParam is -1, then all selected text will be deselected. Well, I'm setting wParam to -1, and it's not deselecting my text! Anyone know why or have this happen also?

You could use EM_EXSETSEL (RichEdit specific, and perhaps better since you're using a RichEdit window) instead:


    CHARRANGE CharRange = {-1, -1};
    SendMessage(WindowHandle, EM_EXSETSEL, 0, (LPARAM)&CharRange);
    SendMessage(WindowHandle, EM_REPLACESEL, FALSE, (LPARAM)Text);


Or, what happens if you remove the line:

SendMessage(hRichEdit, EM_SETSEL, -1, 0);
Title: Re: Writing To a Richedit
Post by: Okee on December 16, 2004, 11:14 PM


void AddText(HWND hRichEdit, char *szStatic, ...)
{
char szTextToAdd[2048] = "";
va_list vaArg;
va_start(vaArg, szStatic);
vsprintf(szTextToAdd, szStatic, vaArg);
va_end(vaArg);

CHARRANGE CharRange = {-1, -1};
SendMessage(hRichEdit, EM_EXSETSEL, 0, (LPARAM)&CharRange);
SendMessage(hRichEdit, EM_REPLACESEL, FALSE, (LPARAM)szTextToAdd);
}


I was using this method before I switched to the less-specific EM_EXSETSEL. It still results in selected text, even after it has replaced the selected text. There needs to be a EM_DESELECTALLTEXT or something. :-P Anyone know why this is happening?