CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2008
    Posts
    2

    Rich Edit "Ignoring" Messages?

    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!

  2. #2
    Join Date
    Jun 2005
    Posts
    1,255

    Smile Re: Rich Edit "Ignoring" Messages?

    Here is my solution in C.
    Code:
       // 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().

  3. #3
    Join Date
    Feb 2008
    Posts
    2

    Re: Rich Edit "Ignoring" Messages?

    I tried using the Edit_GetTextLength macro like you said

    DWORD dwBufferSize = Edit_GetTextLength(hWnd)

    but that is still 0...

    Any other suggestions?

  4. #4
    Join Date
    Feb 2008
    Location
    Seattle, WA - USA
    Posts
    39

    Re: Rich Edit "Ignoring" Messages?

    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?

  5. #5
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: Rich Edit "Ignoring" Messages?

    jlanza,
    Could you post a snippet of the code that you use to create edit control?
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured