Rich Edit

This is a discussion on Rich Edit within the Windows Programming forums, part of the Platform Specific Boards category; hi. when i GetWindowtext(), is there anyway to use this function with a rich edit control and get the text ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    102

    Rich Edit

    hi.
    when i GetWindowtext(), is there anyway to use this function with a rich edit control and get the text formatting aswell?
    because when i use it, it just gets the text, but all the formatting of the text is lost.
    or is there some other function that will get the formatting aswell?

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Look up EM_STREAMIN and EM_STREAMOUT.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    thanks for the reply but...
    i just looked on msdn and searched this forum and i really don't understand what's going on.
    is this the only way to achieve what i want?

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    please, is this the only way to do what i'm wanting?

  5. #5
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    im not to sure how with formating but if u where to use WM_GETTEXT that would get the text

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    yes i can get the text, i use GetWindowText(), but it doesn't get me the formatting.

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    From http://www.pocketpcdn.com/articles/word_embed.html:

    Code:
    #include <windows.h>
    #include <richedit.h>
    
    // LoadCallback
    //
    // Reader callback
    //
    static DWORD CALLBACK LoadCallback(DWORD_PTR dwCookie, 
           LPBYTE pbBuff,
           LONG cb,
           LONG* pcb )
    {
    	HANDLE hFile = (HANDLE) dwCookie;
    
    	ReadFile(hFile, pbBuff, cb, (DWORD*)pcb, NULL);
    
    	return 0;
    }
    
    // SaveCallback
    //
    // Writer callback
    //
    static DWORD CALLBACK SaveCallback(DWORD_PTR dwCookie, 
          LPBYTE pbBuff,
          LONG cb,
          LONG* pcb)
    {
    	HANDLE hFile = (HANDLE) dwCookie;
    
    	WriteFile(hFile, pbBuff, cb, (DWORD*)pcb, NULL);
    
    	return 0;
    }
    
    
    /* 
     * Load a RTF file with the given file name into a rich edit control.
     */
    BOOL LoadRichEdit(HWND hwndRich, LPCTSTR szFileName)
    {
    	HANDLE hFile = CreateFile(
    			szFileName, 
    			GENERIC_READ, 
    			FILE_SHARE_READ, 
    			NULL, 
    			OPEN_EXISTING, 
    			FILE_ATTRIBUTE_NORMAL, 
    			NULL);
    
    	if(hFile)
    	{
    		EDITSTREAM	eds;
    
    		//
    		// Prepare the structure
    		//
    		eds.dwCookie = (DWORD_PTR) hFile;
    		eds.dwError = 0;
    		eds.pfnCallback = LoadCallback;
    
    		//
    		// Read in the file
    		//
    		SendMessage(hwndRich, EM_STREAMIN, (WPARAM) SF_RTF, (LPARAM) &eds);
    
    		CloseHandle(hFile);
    
    		return (eds.dwError == 0);
    	}
    
    	return FALSE;
    }
    
    
    /*
     * Save the contents of a rich edit control to a RTF file with the given file name.
     */
    BOOL SaveRichEdit(HWND hwndRich, LPCTSTR szFileName)
    {
    	HANDLE hFile = CreateFile(
    			szFileName, 
    			GENERIC_WRITE, 
    			FILE_SHARE_READ, 
    			NULL, 
    			CREATE_ALWAYS, 
    			FILE_ATTRIBUTE_NORMAL, 
    			NULL);
    
    	if(hFile)
    	{
    		EDITSTREAM	eds;
    
    		//
    		// Prepare the structure
    		//
    		eds.dwCookie = (DWORD_PTR) hFile;
    		eds.dwError = 0;
    		eds.pfnCallback = SaveCallback;
    
    		//
    		// Save the file
    		//
    		SendMessage(hwndRich, EM_STREAMOUT, (WPARAM) SF_RTF, (LPARAM) &eds);
    
    		CloseHandle(hFile);
    
    		return (eds.dwError == 0);
    	}
    
    	return FALSE;
    }
    Please let me know if this works. Thanks.
    Last edited by anonytmouse; 07-13-2004 at 07:43 PM.

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    i get 16 errors.
    i think this is the line i am looking for
    Code:
    SendMessage(hwndRich, EM_STREAMOUT, (WPARAM) SF_RTF, (LPARAM) &eds);
    i was hoping for something like this:

    Code:
    char buffer[500];
    SendMessage(richedit1, EM_STREAMOUT, (WPARAM) SF_RTF, (LPARAM) buffer);
    SetWindowText(richedit2, buffer);
    turns out it's a lot harder..

  9. #9
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    I think you could have corrected the two incorrect lines. :-)

    The original post has been edited with corrections.

    You didn't indicate that you wanted to copy text from one rich edit to another. The easiest way to do this is to send WM_COPY to the source control and WM_PASTE to the destination. Of course, this will delete the current contents of the clipboard, so the user may curse you.

    Or you could just save and load using a temp file. Or you could figure out how to alter the code I posted to use EM_STREAMOUT/EM_STREAMIN to do it the best way.

  10. #10
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    nope, i still get 16 errors.

    You didn't indicate that you wanted to copy text from one rich edit to another.
    well, i do, but i also need to store it into a char array so i can send the text & formatting using winsock, at the moment i'm thinking about writing my own set of tags, for example {*B*} turns text bold. is this a good idea? or a waste of time?

  11. #11
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >> nope, i still get 16 errors. <<

    It compiles for me, so you'll have to post the errors you get.

    >> well, i do, but i also need to store it into a char array so i can send the text & formatting using winsock <<

    You could replace the ReadFile/WriteFile with send/recv. Or you could use a std::string.

    >> at the moment i'm thinking about writing my own set of tags, for example {*B*} turns text bold. is this a good idea? or a waste of time? <<

    Possibly, it woud certainly be more portable. Depends on your needs.

  12. #12
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    Or you could use a std::string
    what's the difference between using a char array or an std::string? will using a std::string make it easier for me?

  13. #13
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    this code now works for reading rtf files:
    (it's just test code, doesn't do much)
    Code:
    #if !defined RichEditControlsH
    #define      RichEditControlsH 
    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    #include <string>
    #include <richedit.h> 
    using std::string;
    
    HWND SEND, richedit1, richedit2;
    static CHARFORMAT cf = { sizeof(CHARFORMAT), CFM_COLOR, 0, 0, 0, RGB(0,0,0), 0, 0 };
    EDITSTREAM ES;
    
    // LoadCallback
    //
    // Reader callback
    //
    static DWORD CALLBACK LoadCallback(unsigned long dwCookie, 
    								   LPBYTE pbBuff,
    								   LONG cb,
    								   LONG* pcb )
    {
    	HANDLE hFile = (HANDLE) dwCookie;
    	
    	ReadFile(hFile, pbBuff, cb, (DWORD*)pcb, NULL);
    	
    	return 0;
    }
    
    // SaveCallback
    //
    // Writer callback
    //
    static DWORD CALLBACK SaveCallback(unsigned long dwCookie, 
    								   LPBYTE pbBuff,
    								   LONG cb,
    								   LONG* pcb)
    {
    	HANDLE hFile = (HANDLE) dwCookie;
    	
    	WriteFile(hFile, pbBuff, cb, (DWORD*)pcb, NULL);
    	
    	return 0;
    }
    
    
    /* 
    * Load a RTF file with the given file name into a rich edit control.
    */
    BOOL LoadRichEdit(HWND hwndRich, LPCTSTR szFileName)
    {
    	HANDLE hFile = CreateFile(
    		szFileName, 
    		GENERIC_READ, 
    		FILE_SHARE_READ, 
    		NULL, 
    		OPEN_EXISTING, 
    		FILE_ATTRIBUTE_NORMAL, 
    		NULL);
    	
    	if(hFile)
    	{
    		EDITSTREAM	eds;
    		
    		//
    		// Prepare the structure
    		//
    		eds.dwCookie = (unsigned long) hFile;
    		eds.dwError = 0;
    		eds.pfnCallback = LoadCallback;
    		
    		//
    		// Read in the file
    		//
    		SendMessage(hwndRich, EM_STREAMIN, (WPARAM) SF_RTF, (LPARAM) &eds);
    		
    		CloseHandle(hFile);
    		
    		return (eds.dwError == 0);
    	}
    	
    	return FALSE;
    }
    
    
    /*
    * Save the contents of a rich edit control to a RTF file with the given file name.
    */
    BOOL SaveRichEdit(HWND hwndRich, LPCTSTR szFileName)
    {
    	HANDLE hFile = CreateFile(
    		szFileName, 
    		GENERIC_WRITE, 
    		FILE_SHARE_READ, 
    		NULL, 
    		CREATE_ALWAYS, 
    		FILE_ATTRIBUTE_NORMAL, 
    		NULL);
    	
    	if(hFile)
    	{
    		EDITSTREAM	eds;
    		
    		//
    		// Prepare the structure
    		//
    		eds.dwCookie = (unsigned long) hFile;
    		eds.dwError = 0;
    		eds.pfnCallback = SaveCallback;
    		
    		//
    		// Save the file
    		//
    		SendMessage(hwndRich, EM_STREAMOUT, (WPARAM) SF_RTF, (LPARAM) &eds);
    		
    		CloseHandle(hFile);
    		
    		return (eds.dwError == 0);
    	}
    	
    	return FALSE;
    }
    
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) 
    { 
    	
    	HINSTANCE hInstance = (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE);
    	
        switch(msg) 
        { 	
    	case WM_COMMAND:
    		if( HIWORD(wParam) == BN_CLICKED ){ 
    			if((HWND) lParam == SEND){
    				LoadRichEdit(richedit1,"test.rtf");
    			}
    			return 0;
    		}
    		return 0;
    	case WM_CLOSE: 	
    		PostQuitMessage(0); 
    		return 0;
    	case WM_DESTROY: 
    		PostQuitMessage(0); 
    		return 0;
    	default: 
    		return DefWindowProc(hwnd, msg, wParam, lParam); 
    	} 
    	return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    				   LPSTR lpCmdLine, int nCmdShow)
    {
    	HINSTANCE hLib;
    	static TCHAR     chCntrlName[32];
    	WNDCLASSEX wc;
    	HWND hwnd;
    	MSG Msg;	
    	wc.cbSize        = sizeof(WNDCLASSEX);
    	wc.style         = 0;
    	wc.lpfnWndProc   = WndProc;
    	wc.cbClsExtra    = 0;
    	wc.cbWndExtra    = 0;
    	wc.hInstance     = hInstance;
    	wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    	wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    	wc.hbrBackground = (HBRUSH)(GetStockObject(BLACK_BRUSH));
    	wc.lpszMenuName  = NULL;
    	wc.lpszClassName = "AWindow";
    	wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    	
    	if(!RegisterClassEx(&wc))
    	{
    		MessageBox(NULL, "Window Registration Failed!", "Error!",
    			MB_ICONEXCLAMATION | MB_OK);
    		return 0;
    	}
    	
    	hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"aWindow","",
    		WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX | WS_CAPTION ,
    		CW_USEDEFAULT, CW_USEDEFAULT, 381, 422,
    		NULL, 0, hInstance, NULL);
    	
    	if(hwnd == NULL)
    	{
    		MessageBox(NULL, "Window Creation Failed!", "Error!",
    			MB_ICONEXCLAMATION | MB_OK);
    		return 0;
    	}
    	
    	ShowWindow(hwnd, nCmdShow);
    	UpdateWindow(hwnd);
    	
    	hLib=LoadLibrary(TEXT("RICHED20.DLL"));
    	if (!hLib)
    	{
    		hLib=LoadLibrary(TEXT("RICHED32.DLL"));
    		if (!hLib)
    		{
    			MessageBox( NULL,
    				TEXT("Failed to load rich edit library"),
    				TEXT("ERROR"),
    				MB_OK|MB_ICONERROR);
    			return 0;
    		}
    		else
    		{
    			lstrcpy(chCntrlName,TEXT("RICHEDIT"));
    		}
    	}
    	else
    	{
    		lstrcpy(chCntrlName,RICHEDIT_CLASS);
    	}
    	
    	richedit1 = CreateWindowEx(WS_EX_CLIENTEDGE,chCntrlName,TEXT(""),
    		WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_BORDER|ES_MULTILINE,5,30,
    		360,210,hwnd,NULL,hInstance,NULL);
    	
    	richedit2 = CreateWindowEx(WS_EX_CLIENTEDGE,TEXT("EDIT"),TEXT(""),
    		WS_CHILD | WS_VISIBLE | WS_VSCROLL |ES_MULTILINE,5,260,
    		305,100,hwnd,NULL,hInstance,NULL);
    	
    	if(richedit1 == NULL || richedit2 == NULL)
    	{
    		MessageBox(NULL, "Text box Creation Failed!", "Error!",
    			MB_ICONEXCLAMATION | MB_OK);
    		return 0;
    	}
    	
    	SEND = CreateWindowEx(0,TEXT("BUTTON"),"Go",
    		WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,315,260,50,100,hwnd,NULL,hInstance,NULL); 
    	
    				while(GetMessage(&Msg, NULL, 0, 0) > 0)
    				{
    					TranslateMessage(&Msg);
    					DispatchMessage(&Msg);
    				}
    				
    				return 0;
    }
    
    #endif
    it's also supposed to be able to save to rtf files, but it doesn't seem to work?

    so what i want to know is, can i replace the hFile handle with a std::string or char array, so i can send the data?
    i've tried both ways and i keep getting errors.
    i don't want to have to write to a file and then send the file using winsock then read the file at the other end...
    please help me.

  14. #14
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >> it's also supposed to be able to save to rtf files, but it doesn't seem to work? <<

    You don't seem to be calling the SaveRichEdit function.

    Your new save callback should look something like:
    Code:
    static DWORD CALLBACK SaveCallback(unsigned long dwCookie, 
    								   LPBYTE pbBuff,
    								   LONG cb,
    								   LONG* pcb)
    {
    	SOCKET s = (SOCKET) dwCookie;
    	
    	*pcb = send(s, (const char *) pbBuff, cb, 0);
    	
    	return 0;
    }
    It is your responsibility to make sure dwCookie contains a valid, connected socket. Based on this code you should also be able to write the load callback.

  15. #15
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    this is driving me absolutly crazy, i just can't get it working.

    Code:
    static DWORD CALLBACK SaveCallback(unsigned long dwCookie, 
    								   LPBYTE pbBuff,
    								   LONG cb,
    								   LONG* pcb)
    {
    	ClientSocket = (SOCKET)dwCookie;
    	
    	*pcb = send(ClientSocket, (const char *) pbBuff, cb, 0);
    	if(*pcb == SOCKET_ERROR){
    		MessageBox(0,"ERROR","",0);
    	}
    	
    	return 0;
    }
    
    BOOL SaveRichEdit(HWND hwndRich)
    {
    	
    	EDITSTREAM	eds;
    	eds.dwCookie = (unsigned long) ClientSocket;
    	eds.dwError = 0;
    	eds.pfnCallback = SaveCallback;
    	SendMessage(hwndRich, EM_STREAMOUT, (WPARAM) SF_RTF, (LPARAM) &eds);
    	return FALSE;
    }
    i don't get errors with this, but nothing ever happens. I just can't believe there's not an easier way to do this. Msn Messenger, AIM, ICQ. all these programs do what i want, so it can't be impossible?
    and i find it hard to believe i'm the first person on these forums to want to do this, but i can't find any posts about it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. line number on a rich edit control
    By rakan in forum Windows Programming
    Replies: 1
    Last Post: 02-18-2008, 06:58 AM
  2. rich edit 4.1 is giving me a head-ache
    By master5001 in forum Windows Programming
    Replies: 3
    Last Post: 07-01-2005, 01:21 PM
  3. How the rich get rich [Long]
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 05-05-2005, 10:36 PM
  4. newbie to rich edit, cant get it to work
    By hanhao in forum Windows Programming
    Replies: 1
    Last Post: 03-24-2004, 09:54 PM
  5. Rich edit
    By Garfield in forum Windows Programming
    Replies: 10
    Last Post: 07-10-2002, 04:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21