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

    RichEdit printing problem!

    Hi, I am trying to print content from a Win32 richedit control, but it doesn't work. The text wraps to the half width of the page, and after printing, the text in the richedit is also messed up (word wrap to the half width of the box).

    I use this unmodified code taken directly from MSDN:

    Code:
    bool PrintRTF(HWND Handle, HDC PrinterDC) {	
    	DOCINFO di = { sizeof(di) };
    	if (!StartDoc(PrinterDC, &di)) {
    		return false;
    	}
    	int cxPhysOffset = GetDeviceCaps(PrinterDC, PHYSICALOFFSETX);
    	int cyPhysOffset = GetDeviceCaps(PrinterDC, PHYSICALOFFSETY);
    	int cxPhys = GetDeviceCaps(PrinterDC, PHYSICALWIDTH);
    	int cyPhys = GetDeviceCaps(PrinterDC, PHYSICALHEIGHT);
    	SendMessage(Handle, EM_SETTARGETDEVICE, (WPARAM)PrinterDC, cxPhys / 2);
    	FORMATRANGE fr;
    	fr.hdc = PrinterDC;
    	fr.hdcTarget = PrinterDC;
    	fr.rc.left = cxPhysOffset;
    	fr.rc.right = cxPhysOffset + cxPhys;
    	fr.rc.top = cyPhysOffset;
    	fr.rc.bottom = cyPhysOffset + cyPhys;
    	SendMessage(Handle, EM_SETSEL, 0, (LPARAM)-1);
    	SendMessage(Handle, EM_EXGETSEL, 0, (LPARAM)&fr.chrg);
    	bool fSuccess = true;
    	while (fr.chrg.cpMin < fr.chrg.cpMax && fSuccess) {
    		fSuccess = StartPage(PrinterDC) > 0;
    		if (!fSuccess) break;
    		int cpMin = SendMessage(Handle, EM_FORMATRANGE, true, (LPARAM)&fr);
    		if (cpMin <= fr.chrg.cpMin) {
    			fSuccess = false;
    			break;
    		}
    		fr.chrg.cpMin = cpMin;
    		fSuccess = EndPage(PrinterDC) > 0;
    	}
    	SendMessage(Handle, EM_FORMATRANGE, false, 0);
    	if (fSuccess) {
    		EndDoc(PrinterDC);
    	 } 
    	else {
    		AbortDoc(PrinterDC);
    	}
    	DeleteDC(PrinterDC);
    	return fSuccess;
    }
    Any thoughts?

    Thanks in advance!

  2. #2
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: RichEdit printing problem!

    Oh, man, you went into a real thicket with this one.... The way I was able to even remotely come close to printing is by doing this: create a test MFC project, make it a project with a single document and derive the view class from CRichEditView. All of your answers are in that class, btw. Then in your test project override OnPreparePrinting and put a breakpoint on it. Then compile and start debugging. When the test app opens up go to File and Print. This will trigger the breakpoint. Then step into the CRichEditView and try to copy what they're doing there. (If you don't want to do it this way, look for the viewrich.cpp file in the installation folder for the Visual Studio.)

    Good luck. All that stuff is largely undocumented (like pretty much anything else about rich edits).

  3. #3
    Join Date
    Jan 2009
    Posts
    13

    Re: RichEdit printing problem!

    OK thanks for your reply,
    but unfortunately I have no time to implement MFC into my project. I am looking for a native Win32 solution. I know it's not very well documented, there's almost nothing on Google about that.

  4. #4
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: RichEdit printing problem!

    I know what you mean, I'm going through the same thing. OK, the last thing I'm going to post here is that MFC is simply a wrapper for your Win32 calls so if you dig deep enough you'll get to it.... you'll just have to go through the Microsoft thicket, if you know what I mean.... Good luck.

  5. #5
    Join Date
    Jan 2009
    Posts
    13

    Re: RichEdit printing problem!

    Problem solved!

    Putting...
    Code:
    SendMessage(Handle, EM_SETTARGETDEVICE, NULL, NULL);
    SendMessage(Handle, EM_SETSEL, 0, 0);
    ...restores the richedit text as before printing.

    For 2.5-inches margins, this code works perfectly:

    Code:
    SIZE PPP;
    PPP.cx = GetDeviceCaps(DeviceContext, LOGPIXELSX);
    PPP.cy = GetDeviceCaps(DeviceContext, LOGPIXELSX);
    fr.rc.left = ::MulDiv(GetDeviceCaps(DeviceContext, PHYSICALOFFSETX), 1440, PPP.cx) + 1000;
    fr.rc.right = ::MulDiv(GetDeviceCaps(DeviceContext, PHYSICALWIDTH), 1440, PPP.cx) - fr.rc.left - 200;
    fr.rc.top = ::MulDiv(GetDeviceCaps(DeviceContext, PHYSICALOFFSETY), 1440, PPP.cy) + 1000;
    fr.rc.bottom = ::MulDiv(GetDeviceCaps(DeviceContext, PHYSICALHEIGHT), 1440, PPP.cy) - fr.rc.top - 750;

Tags for this Thread

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