Hey guys,

I have been looking around for a while but I can't seem to find any tutorial on how to print the contents of a RichEdit to a printer. Any help would be appreciated and if anyone has any links to a good tutorial please post them.

Thanks!
TheNewKid

Thanks ANcient Dragon, however my program does not currently use MFC and both of those examples do. (Sorry I should have mentioned that before) Do you know of any ways to do it with just Win32 and not MFC?

Thanks!
TheNewKid

Refer to this MSDN article.

Thanks nullptr thats perfect!

Hey guys,

I have been working on my program some more and currently I am using this function from the msdn docs to print from my richedit:

BOOL PrintRTF(HWND hwnd, HDC hdc)
{

    HDC hDC = hdc;

    DOCINFO di = { sizeof(di) };

    if (!StartDoc(hdc, &di))
    {
        return FALSE;
    }

    int cxPhysOffset = GetDeviceCaps(hdc, PHYSICALOFFSETX);
    int cyPhysOffset = GetDeviceCaps(hdc, PHYSICALOFFSETY);

    int cxPhys = GetDeviceCaps(hdc, PHYSICALWIDTH);
    int cyPhys = GetDeviceCaps(hdc, PHYSICALHEIGHT);

    SendMessage(hwnd, EM_SETTARGETDEVICE, (WPARAM)hdc, cxPhys/2);

    FORMATRANGE fr;

    fr.hdc       = hdc;
    fr.hdcTarget = hdc;

    // Set page rect to physical page size in twips.
    fr.rcPage.top    = 0;
    fr.rcPage.left   = 0;
    fr.rcPage.right  = 12000;//MulDiv(cxPhys, 1440, GetDeviceCaps(hDC, LOGPIXELSX));
    fr.rcPage.bottom = 15000;//MulDiv(cyPhys, 1440, GetDeviceCaps(hDC, LOGPIXELSY));

    // Set the rendering rectangle to the pintable area of the page.
    fr.rc.left   = cxPhysOffset;
    fr.rc.right  = 12000;//cxPhysOffset + cxPhys;
    fr.rc.top    = cyPhysOffset;
    fr.rc.bottom = 15000;//cyPhysOffset + cyPhys;

    SendMessage(hwnd, EM_SETSEL, 0, (LPARAM)-1);          // Select the entire contents.
    SendMessage(hwnd, EM_EXGETSEL, 0, (LPARAM)&fr.chrg);  // Get the selection into a CHARRANGE.

    BOOL fSuccess = TRUE;

    // Use GDI to print successive pages.
    while (fr.chrg.cpMin < fr.chrg.cpMax && fSuccess)
    {
        fSuccess = StartPage(hdc) > 0;

        if (!fSuccess) break;

        int cpMin = SendMessage(hwnd, EM_FORMATRANGE, TRUE, (LPARAM)&fr);

        if (cpMin <= fr.chrg.cpMin)
        {
            fSuccess = FALSE;
            break;
        }

        fr.chrg.cpMin = cpMin;
        fSuccess = EndPage(hdc) > 0;
    }

    SendMessage(hwnd, EM_FORMATRANGE, FALSE, 0);

    if (fSuccess)
    {
        EndDoc(hdc);
    }

    else

    {
        AbortDoc(hdc);
    }

    return fSuccess;

}

This works great for the first time you print, but after it runs once the richedit gets a bit messed up and the words in it all get shoved to the left. Any ideas why this is happening and how to fix it?

I took some pictures to show what it looks like before and after I print:

Before: 0d7de499c350193ab7ec1c97e05e20ee

After: 879784159bf90aa93b8a236e6fc3b2c9

Does anyone know wy this is happening? I really need some help!

You need to call SendMessage(hwnd, EM_SETTARGETDEVICE, 0, 0) after you're done printing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.