04. oktober 2000 - 13:03
#2
Jeg gætter på at du kan bruge PaintTo. Nedenstående er fra Delphi\'s hjælp:
<SNIP>
Draws the windowed control to a device context.
procedure PaintTo(DC: HDC; X, Y: Integer);
Description
Call PaintTo to draw the control on a device context. Specify the device context as the value of the DC parameter and specify the X and Y coordinates on the device context where the top-left corner of the windowed control is to be drawn. PaintTo first erases the background of the device context and then paints the control.
PaintTo is useful for drawing an image of the control into a bitmap DC.
</SNIP>
Ligeledes er der et eksempel:
<SNIP>
procedure TForm1.Button1Click(Sender:TObject);
var
I, Start, Stop: Integer;
begin
PrintDialog1.Options := [poPageNums, poSelection];
PrintDialog1.FromPage := 1;
PrintDialog1.MinPage := 1;
PrintDialog1.ToPage := PageControl1.PageCount;
PrintDialog1.MaxPage := PageControl1.PageCount;
if PrintDialog1.Execute then
begin
{ determine the range the user wants to print }
with PrintDialog1 do
begin
if PrintRange = prAllPages then
begin
Start := MinPage - 1;
Stop := MaxPage - 1;
end
else if PrintRange = prSelection then
begin
Start := PageControl1.ActivePage.PageIndex;
Stop := Start;
end
else { PrintRange = prPageNums }
begin
Start := FromPage - 1;
Stop := ToPage - 1;
end;
end;
{ now, print the pages }
with Printer do
begin
BeginDoc;
for I := Start to Stop do
begin
PageControl1.Pages[I].PaintTo(Handle, 10, 10);
if I <> Stop then
NewPage;
end;
EndDoc;
end;
end;
end;
</SNIP>
04. oktober 2000 - 13:14
#3
Jeg vidste det heller ikke, så jeg checkkede lige Form.Print metoden. Dette inspirerede mig til følgende unit.
Bemærk at WinControlPrint rutinen er istand til at printe en hvilken som helst component der er nedarvet fra TWinControl. I uniten her printer jeg således et Panel og en button.
Håber det kan bruges, det var en sjov måde at bruge en middags pause på.
Delphi
Unit Unit1;
Interface
Uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, jpeg, StdCtrls, Buttons;
Type
TForm1 = Class(TForm)
Panel1 : TPanel;
Button1 : TButton;
Image1 : TImage;
Procedure Button1Click(Sender: TObject);
Private
{ Private declarations }
Procedure WinControlPrint(Source : TWinControl);
Public
{ Public declarations }
End;
Var
Form1: TForm1;
Implementation
{$R *.DFM}
Uses
Printers;
Procedure TForm1.Button1Click(Sender: TObject);
Begin
WinControlPrint(Panel1);
WinControlPrint(Button1);
End;
Procedure TForm1.WinControlPrint(Source : TWinControl);
// Prints any Window control, eg. a button or Panel, to the default printer.
// Created 2000-10-04, Niels Peter Gibe.
// (With heavy inspiration from Borlands TCustomForm.Print method).
Function GetWinControlImage(Source : TWinControl) : TBitmap;
Begin
Result := TBitmap.Create;
Try
Result.Width := Source.Width;
Result.Height := Source.Height;
Result.Canvas.Brush := Source.Brush;
Result.Canvas.FillRect(Source.ClientRect);
Result.Canvas.Lock;
Try
// Overfør Wincontrol, via Formens canvas, til bitmap.
PaintTo(Result.Canvas.Handle, 0-Source.Left, 0-Source.Top);
Finally
Result.Canvas.Unlock;
End;
Except
Result.Free;
Raise;
End;
End;
Const
XDest = 0;
YDest = 0;
Var
SourceImage : TBitMap;
Info : PBitmapInfo;
InfoSize : DWORD;
Image : Pointer;
ImageSize : DWORD;
DIBWidth : LongInt;
DIBHeight : Longint;
PrintWidth : LongInt;
PrintHeight : Longint;
Begin
Printer.BeginDoc;
Try
SourceImage := GetWinControlImage(Source);
Canvas.Lock;
Try
With Printer Do
Begin
GetDIBSizes(SourceImage.Handle, InfoSize, ImageSize);
Info := AllocMem(InfoSize);
Try
Image := AllocMem(ImageSize);
Try
GetDIB(SourceImage.Handle, 0, Info^, Image^);
DIBWidth := Info^.bmiHeader.biWidth;
DIBHeight := Info^.bmiHeader.biHeight;
// Kald Printerdialog før print for at ændre PrintScale mv.
Case PrintScale Of
poProportional:
Begin
PrintWidth := MulDiv(DIBWidth, GetDeviceCaps(Handle,
LOGPIXELSX), PixelsPerInch);
PrintHeight := MulDiv(DIBHeight, GetDeviceCaps(Handle,
LOGPIXELSY), PixelsPerInch);
End;
poPrintToFit:
Begin
PrintWidth := MulDiv(DIBWidth, PageHeight, DIBHeight);
if PrintWidth < PageWidth Then
PrintHeight := PageHeight
Else
Begin
PrintWidth := PageWidth;
PrintHeight := MulDiv(DIBHeight, PageWidth, DIBWidth);
End;
End;
Else
PrintWidth := DIBWidth;
PrintHeight := DIBHeight;
End;
// Her kan der fifles med Destination konstanterne, hvis printet
// ikke skal placeres i koordinat 0,0 på printeren.
StretchDIBits(Canvas.Handle, XDest, YDest, PrintWidth, PrintHeight,
0, 0, DIBWidth, DIBHeight,
Image, Info^, DIB_RGB_COLORS, SRCCOPY);
Finally
FreeMem(Image, ImageSize);
End;
Finally
FreeMem(Info, InfoSize);
End;
End;
Finally
Canvas.Unlock;
SourceImage.Free;
End;
Finally
Printer.EndDoc;
End;
End;
End.
04. oktober 2000 - 13:41
#8
Jeg har lige testet det - jeg har ikke så meget tid lige i øjeblikket, da jeg sidder på arbejdet, men jeg får udskrevet en tom side..
Jeg har oprettet en form med et panel og på det panel, er der en stringgrid, editfelt og label.
09. oktober 2000 - 08:01
#10
1000 tak til Delphi, det var lige det jeg skulle bruge.
Det virker upåklageligt hjemme hos mig, men på arbejdet bliver der kun udskrevet en blank side.
Det er ligegyldigt, det er hjemme jeg skal bruge det.
Også tak til jer andre, som svarede.
Undskyld den lange svartid. :-))