|
I use the code below to resize text to print
correctly onto a printer canvas. The reason for this code is that if the rect
area specified by r is too large or a section is outside of the print margin of
the printer then the rich edit is resized to fit, this way the richtext isn’t
cropped. The problem I have is that it doesn’t print on
a Win9x machine, it prints in NT 4.0, 2K, XP and 2003. Can someone look over this and let me know if I am
doing something wrong? Thanks in advance Jason
GetVersionEx(osv);
if osv.dwPlatformId = VER_PLATFORM_WIN32_NT then
hdcPrinter := CreateDc(pDriver, pDevice, pPort, nil)
else
hdcPrinter := CreateDc(nil, pDevice, nil, nil);
hdcCompatible := CreateCompatibleDC(hdcPrinter);
hbmPrinter := CreateCompatibleBitmap(hdcPrinter, w, h);
SelectObject(hdcCompatible, hbmPrinter);
//Clear the canvas
brush := CreateSolidBrush(clWhite);
SelectObject(hdcCompatible, brush);
FillRect(hdcCompatible, rect(0,0,w,h), Brush);
DeleteObject(brush); //this line works on nt , 2k and nt but not on 9x
machines, so I changed to use the code above //
bitblt(hdcCompatible, 0,0,w,h,hdcPrinter,0,0,Whiteness);
PrintRTFToBitmap(hdcCompatible, 0,0,w, h, RTs[j], 0, 0, CheckRTFDataFields);
AdjustRect(r, Pmargin, pxo, pyo, xo, yo);
bmp := TBitmap.Create;
bmp.Handle := hbmPrinter;
PrintBitmap(Printer.Canvas, r, bmp);
bmp.Free;
DeleteDC(hdcPrinter);
DeleteDC(hdcCompatible); procedure PrintRTFToBitmap(cnv: THandle; Side, Top,
Width, Height: Integer; Editor: TRichEdit; ScaleX, ScaleY:
Single; CheckFields: Boolean); var range: TFormatRange; MaxLen: Integer; LogX, LogY: Single; MyStream: TStringStream; begin try FillChar(Range, SizeOf(TFormatRange),
0); if ScaleX <> 0 then begin LogX := ScaleX; LogY := ScaleY; end else begin // Rendering to the
same DC we are measuring. LogX := GetDeviceCaps(cnv,
LOGPIXELSX); LogY := GetDeviceCaps(cnv,
LOGPIXELSY); end; Range.hdc := cnv; Range.hdcTarget := cnv;//Printer.Canvas.handle;//
Range.hdc; // Set up the page. Range.rc.left :=
Trunc((Side * 1440) / LogX); Range.rc.top
:= Trunc((Top * 1440) / LogX); Range.rc.right := Trunc((Width
* 1440) / LogX); Range.rc.Bottom := Trunc((Height
* 1440) / LogY); Range.rcPage := Range.rc; if CheckFields then begin CheckDataFields(MyStream,
Editor); Editor.Lines.LoadFromStream(MyStream); MyStream.Free; end; // Default the range of text to
print as the entire document. MaxLen := Editor.GetTextLen; Range.chrg.cpMax := MaxLen; Range.chrg.cpMin := 0; SetBkMode(cnv, TRANSPARENT); // format the text SendMessage(Editor.Handle, EM_FORMATRANGE,
1, Longint(@Range)); // Free cached information SendMessage(Editor.Handle, EM_FORMATRANGE,
0,0); except end; procedure PrintBitmap(Canvas: TCanvas; DestRect: TRect;
Bitmap: TBitmap); var BitmapHeader: POINTER; BitmapImage: POINTER; HeaderSize: DWord; ImageSize:
DWord; begin GetDIBSizes(Bitmap.Handle,HeaderSize,ImageSize); BitmapHeader := AllocMem(HeaderSize); BitmapImage := AllocMem(ImageSize); try GetDIB(Bitmap.Handle, Bitmap.Palette,
BitmapHeader^, BitmapImage^); StretchDIBits(Canvas.Handle,
DestRect.Left, DestRect.Top, {Destination Origin}
DestRect.Right - DestRect.Left, {Destination Width}
DestRect.Bottom - DestRect.Top, {Destination Height} 0,0,
{Source Origin}
Bitmap.Width, Bitmap.Height, {Source Width & Height}
BitmapImage,
TBitmapInfo(BitmapHeader^),
DIB_RGB_COLORS,
SRCAND);//COPY); finally FreeMem(BitmapHeader); FreeMem(BitmapImage) end; end;{PrintBitmap} |
