我现在做图形打印,设置如下:在ScrollBox上放一PaintBox,当PaintBox的Align属性设置为alClient时,在PaintBox上的图形能全部打印出来。但把PanitBox的属性Align设置为alNone时,同时把PaintBox的大小调整比ScrollBox大,打印的结果是只是Scrollbox所能显示的图形,还带有边框。如何打印整个PaintBox上的图形?因为有很多图形,一个屏幕肯定是放不下的。先给这些分,如能回答再加分。以下是输出图形的代码:
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;type
  TMainForm = class(TForm)
    Button1: TButton;
    Image1: TImage;
    ScrollBox1: TScrollBox;
    PaintBox1: TPaintBox;
    procedure PaintBox1Paint(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  MainForm: TMainForm;implementation{$R *.dfm}const
  CStep = 40;
  CStartColor = clBlue;
  CEndColor = clBlack;procedure TMainForm.PaintBox1Paint(Sender: TObject);
var
  I: Integer;
  iRedInc: BYTE;
  iGreenInc: Byte;
  iBlueInc: Byte;
  R: TRect;
  cl: TColor;
  iIncrement: Integer;
begin
  iRedInc := (GetRvalue(CEndColor) - GetRvalue(CStartColor)) div CStep;
  iGreenInc := (GetGvalue(CEndColor) - GetGvalue(CStartColor)) div CStep;
  iBlueInc := (GetBvalue(CEndColor) - GetBvalue(CStartColor)) div CStep;
  iIncrement := PaintBox1.ClientHeight div CStep;  I := 0;
  cl := CStartColor;
  while I <= PaintBox1.ClientHeight do
  begin
    R := Rect(0, I, PaintBox1.Width, I + iIncrement);
    PaintBox1.Canvas.Brush.Color := cl;
    PaintBox1.Canvas.FillRect(R);
    Inc(I, iIncrement);
    cl := RGB(GetRvalue(cl) + iRedInc, GetGvalue(cl) + iGreenInc, GetBvalue(cl) + iBlueInc);
  end;
end;procedure TMainForm.Button1Click(Sender: TObject);
var
  bmp: TBitmap;
begin
  bmp :=tbitmap.create;
  bmp.width :=paintbox1.width;
  bmp.height :=paintbox1.height;
  bitblt(bmp.canvas.handle,0,0,bmp.width,bmp.height,paintbox1.canvas.handle,0,0,srccopy);
  image1.autosize :=true;
  image1.picture.bitmap.assign(bmp);
  image1.picture.savetofile('c:\a.bmp');
  bmp.free;
end;end.

解决方案 »

  1.   

    次问题我已经解决,既然来了,看一下这个问题:如何给PaintBox加滚动条,我的做法是把PaintBox放在ScrollBox上,在程序初始化时,指定一个很大的PaintBox,比如PaintBox.Height:=3000,PaintBox.Width:=3000.结果出现了上面的问题。而且有时并不能画满整个PaintBox,这样打印起来很浪费。有没有好的方法使图形一旦超出PaintBox则ScrollBox自动加上滚动条?
      

  2.   

    一般的做法是:在ScrollBox中加入Panel,在Panel中加入PaintBox,当PaintBox尺寸改变时使Panel的大小=PaintBox,这样就可使ScrollBox自动加上滚动条。但我不这么做。我的做法是:设二个全局变量BMP:TBitMap,RECT:TRect;窗体中PaintBox的大小固定。程序开始时BMP:=TBitMap.Create;结束时 BMP.Free;然后在程序中在BMP中绘出所需的图形,窗体刷新时根据RECT绘出图形,RECT根据MOUSE的位置不断变化。
      

  3.   

    打印时直接在打印机的Canvas中绘出BMP即可。
      

  4.   

    procedure TMainFm.PrintClick(Sender: TObject);
    var
      bmp :TBitmap;
      ScaleX,ScaleY :Integer;
      R :TRect;
    begin
      bmp :=TBitmap.Create;
      bmp.Width :=TMdiFm(ActiveMDIChild).PaintBox.Width;
      bmp.Height :=TMdiFm(ActiveMDIChild).PaintBox.Height;
      TMdiFm(ActiveMDIChild).nmg_tvgVectorGraph.Paint(bmp.Canvas);
       //在bmp上重新绘出PaintBox图形
      Image.AutoSize :=True;
      Image.Picture.Bitmap.Assign(bmp);
      //Image.Picture.SaveToFile('c:\a.bmp');
      bmp.Free;  if Printer.Printers.Count=0 then
      begin
        ShowMessage('请首先安装打印机');
        Exit;
      end;
      if  not PrintDialog.Execute then  Exit
      else
      begin
        with Printer do
        begin
          BeginDoc;
          ScaleX :=GetDeviceCaps(Handle,LogPixelsX) div PixelsPerInch;
          ScaleY :=GetDeviceCaps(Handle,LogPixelsY) div PixelsPerInch;
          R :=Rect(0,0,Image.Width*ScaleX,Image.Height*ScaleY);
          Canvas.StretchDraw(R,Image.Picture.Graphic);
          EndDoc;
        end;
      end;
    end;
    以上是我的打印程序。
    现在问题是:如果PaintBox的大小固定,要画很多图形,肯定会超出PaintBox的大小,这部分图形怎么来画?所以我想要动态的改变PaintBox的大小,根据图形自动使PaintBox增大,但我不会。
      

  5.   

    to  xiaoxiao197821(你的笑对我很重要) 
    你有什么好的方法?