1

I saw a discussion here about displaying a RTF file in a rich text edit control. Maybe what I am trying to do it too much.

In my dialog class I define a static method:

static DWORD CALLBACK MyStreamInCallback(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG* pcb)
{
    std::ifstream* pFile = (std::ifstream*)dwCookie;
    pFile->read((char*)pbBuff, cb);
    return 0;
}

In my dialog OnInitDialog class I try to display the data:

std::ifstream  File("d:\\RevisionHistoryTest.rtf");
EDITSTREAM es = { 0 };
es.dwCookie = (DWORD)&File;
es.pfnCallback = MyStreamInCallback;
::SendMessage(m_rtfEdit, EM_STREAMIN, SF_RTF, (LPARAM)&es);

Now, here is a link to a sample project. I don't know where I can put this project in the long term, but my DropBox will do for now. The project does not include the RTF file. This is how I created it:

  1. I went to the following URL in a browser.
  2. I selected all the Revision History content and pasted it into a Microsoft Word file.
  3. I copied the Microsoft Word content and pasted it into a WordPad session and saved it.

Example:

enter image description here

Interestingly, when I subsequently open my RTF file in WordPad I get a popup message:

enter image description here

If I select Unblock then it still opens in the editor. I assume that is because all of the images must still be linked to those on my website. And I think this is related to the issue with my test project, because this is what I see:

enter image description here

I get no errors or anything. It just reads the first line and stops.

I am trying to find the easiest way to display my HTML history in a RTF window.


My original intention was to use a CHtmlView control instead (makes sense to do that) and directly read my Revision History file from the internet. But my help system is designed to permanently show the contents pane on the left. This is why I thought that RTF might be a suitable alternative. But struggling with it.


Update

Based on the comments made about 64 bit builds I located this tutorial which works for both 32 bit and 64 bit. They both display "Revision History" only.

BOOL FillRichEditFromFile(HWND hwnd, LPCTSTR pszFile)
{
    BOOL fSuccess = FALSE;

    HANDLE hFile = CreateFile(pszFile, GENERIC_READ,
        FILE_SHARE_READ, 0, OPEN_EXISTING,
        FILE_FLAG_SEQUENTIAL_SCAN, NULL);

    if (hFile != INVALID_HANDLE_VALUE)
    {
        EDITSTREAM es = { 0 };

        es.pfnCallback = MyStreamInCallback;
        es.dwCookie = (DWORD_PTR)hFile;

        if (SendMessage(hwnd, EM_STREAMIN, SF_RTF, (LPARAM)&es) && es.dwError == 0)
        {
            fSuccess = TRUE;
        }

        CloseHandle(hFile);
    }

    return fSuccess;

}

And in OnInitDialog:

FillRichEditFromFile(m_rtfEdit.GetSafeHwnd(), _T("d:\\RevisionHistoryTest.rtf"));

But my initial issue still remains.


Update

I had forgotten to set the control to multiline! That was part of the issue:

enter image description here

At least all of the text is visible now. Just not the images. And I don't like the way some of the links are displayed.

I have been able to edit the file and bring the indents over to the left to make it look better. But images still won't show.


Update

As a workaround to this I realised that I could simply duplicate my HTML Revision History as a standalone page. Then I can use CHtmlView:

enter image description here

The benefit is that the display is consistent wit what the user sees in the help system.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • 1
    Probably unrelated, but `(std::ifstream*)dwCookie` isn't going to work for 64-bit architectures, if `dwCookie` is of type `DWORD`. Likewise, this is probably unrelated, but that callback has the wrong signature, too. [EDITSTREAMCALLBACK](https://learn.microsoft.com/en-us/windows/win32/api/richedit/nc-richedit-editstreamcallback)'s first parameter is of type `DWORD_PTR`, not `DWORD`. – IInspectable Jan 14 '21 at 14:10
  • @IInspectable When I change the parameter to `DWORD_PTR` in both locations whilst it compiles for both bit versions only the x66 shows Revision History. x64 is blank. – Andrew Truckle Jan 14 '21 at 14:31
  • 1
    `(DWORD)&File` is *always* wrong for 64-bit architectures. It truncates the pointer. The application not crashing is an unfortunate change in behavior for 64-bit applications, where the OS will silently swallow access violation exceptions raised inside a window procedure. – IInspectable Jan 14 '21 at 14:40
  • @IInspectable As mentioned, I did change it to `es.dwCookie = (DWORD_PTR)&File;`. And whilst it now compiles for 64 too, 64 bit does not show anything at all. – Andrew Truckle Jan 14 '21 at 14:59
  • @IInspectable I updated the question. I use new code now. I don't even think these are external links. Eitherway, it stops. – Andrew Truckle Jan 14 '21 at 15:17
  • I found this https://stackoverflow.com/questions/22097956/visual-c-load-rtf-document-contains-images-and-text-in-richedit-box but the articles it links to do not compile in current VS. – Andrew Truckle Jan 14 '21 at 15:32

0 Answers0