如何打印PANEL上的内容(50分)

  • 主题发起人 主题发起人 fxb
  • 开始时间 开始时间
F

fxb

Unregistered / Unconfirmed
GUEST, unregistred user!
方法一:这是以前搜索到的,可是我怎么打印出来什么也没有呢?
procedure TForm1.Button1Click(Sender: TObject);
var
DC: HDC;
Canvas: TCanvas;
begin
Canvas := TCanvas.Create;
DC := GetDC(0);
Canvas.Handle := DC;

printer.begin
doc;//准备打印
//下面是把 panel 在屏幕上的图象拷贝下来
printer.Canvas.CopyRect(rect(0,0,panel1.Width,panel1.Height),
canvas,
rect(self.Left+panel1.Left,
self.Top+panel1.Top,
self.Left+panel1.Left+panel1.Width,
self.Top+panel1.Top+panel1.Height));
ReleaseDC(0, DC);
canvas.Free;
printer.enddoc;//开始打印
end;

CopyRect 方法
第一个参数是目的矩形的大小
第二个参数源的canvas,这里也就是屏幕的
第三个参数是源的矩形,可能有差距,你修改一下
方法二:panel1.paintto()方法,可以打印但如何改变打印比例?
 
可以这样试试,把panel放在form上面,平铺,然后就form1.print
 
我一直是使用DevExpress中的Print组件的:)
 
printer.enddoc;//开始打印
canvas.Free;
 
to :zhlfdm
换了位置也没有用,打印机动作的,但出来的是白纸一张,为什么?
 
先打印预览一下啊
 
这里有一些代码,你试一试
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls ,Clipbrd, Grids, jpeg, ComCtrls,Printers;
type
TForm1 = class(TForm)
Panel1: TPanel;
Image1: TImage;
Label1: TLabel;
Image2: TImage;
Image3: TImage;
Image4: TImage;
CheckBox1: TCheckBox;
RadioButton1: TRadioButton;
StatusBar1: TStatusBar;
DisplayButton: TButton;
ExitButton: TButton;
PrintButton: TButton;
procedure DisplayButtonClick(Sender: TObject);
procedure ExitButtonClick(Sender: TObject);
procedure PrintButtonClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
function PanelImage(aPanel:TPanel): TBitmap;
var
Ofs: Integer;
begin
Result := TBitmap.Create;
try
Result.Width := aPanel.ClientWidth;
Result.Height := aPanel.ClientHeight;
Result.Canvas.Brush :=aPanel.Brush;
Result.Canvas.FillRect(aPanel.ClientRect);
Result.Canvas.Lock;
try
if GetWindowLong(aPanel.Handle, GWL_STYLE) and WS_BORDER <> 0 then
Ofs := -1 //do
n't Panel form border
else
Ofs := 0;
// There is no border
aPanel.PaintTo(Result.Canvas.Handle, Ofs, Ofs);
finally
Result.Canvas.Unlock;
end;
except
Result.Free;
raise;
end;
end;
procedure TForm1.DisplayButtonClick(Sender: TObject);
var
FormImage: TBitmap;
begin
FormImage := PanelImage(Panel1);
try
//Clipboard.Assign(FormImage);
Image2.Picture.Assign(FormImage);
finally
FormImage.Free;
end;

end;

procedure TForm1.ExitButtonClick(Sender: TObject);
begin
Close;
end;

procedure TForm1.PrintButtonClick(Sender: TObject);
var
FormImage: TBitmap;
begin
FormImage := PanelImage(Panel1);
Printer.begin
Doc;
Printer.Canvas.Draw(20,20,FormImage);
Printer.EndDoc;
end;

end.
 
后退
顶部