如题

解决方案 »

  1. fastreport中有直接打印DBGR的控件!
      

  2. procedure TForm1.Button1Click(Sender: TObject);
    var
      pl,pr,pt,pb:integer;//页边距
      px,py:integer;//打印机分辨率
      ps:integer;//打印机与显示器分辨率之比
      lh:integer;//行高
      i:integer;
      x,y:integer;//打印坐标
      rowb,colb:integer; //行距、行距
    begin
      //初始化页面
      pl:=SpinEdit1.Value;
      pr:=SpinEdit2.Value;
      pt:=SpinEdit3.Value;
      pb:=SpinEdit4.Value;
      rowb:=SpinEdit6.Value;//行距
      colb:=SpinEdit7.Value;//列距
      //将获得的打印机分辨率由厘米转换为英寸
      px:=Trunc(GetDeviceCaps(Printer.Handle,LOGPIXELSX)/2.54);
      py:=Trunc(GetDeviceCaps(Printer.Handle,LOGPIXELSY)/2.54);
      //将屏幕分辨率转换为打印机的比例 
      ps:=Trunc(GetDeviceCaps(Printer.Handle,LOGPIXELSX)
      /Screen.PixelsPerInch);
      //设置打印方向
      if ComboBox1.Text='横向打印' then
        Printer.Orientation:=poPortrait;
      else
        Printer.Orientation:=poLandscape;
      //获得打印的字体和大小
      Printer.Canvas.Font.Name:=Combobox2.Text;
      Printer.Canvas.Font.Size:=Spinedit5.Value;
      Printer.canvas.Font.Color:=clred;
      Printer.Canvas.Pen.Color:=clblue;
      //设置行高
      lh:=Printer.Canvas.TextHeight(DBgrid1.Font.Name);
      //打印的起始位置(打印机画布上的位置)
      x:=PX*pl;
      y:=PY*pt;
      //打印DBGrid1中的内容
      if (Table1.Active=true) and (Table1.RecordCount>0) then
        begin
          //设置一打印机任务标题
          Printer.Title:='DBGrid内容打印';
          with Printer do
          begin
            BeginDoc;
            Table1.First;
            while not Table1.Eof do
              begin
                for i:=0 to DBGrid1.FieldCount-1 do
                  begin
                  //设定打印的宽度
                    if (x+DBGrid1.Columns.Items[i].Width*ps)
                       <=(PageWidth-ps*pr) then
                      begin
                        //画出表格线
                        Printer.Canvas.Rectangle(x,y,x+dbgrid1.Columns.Items[i].Width*ps,y+lh);
                        //如果为第一行时,打印DBGrid1确的标题
                        if y=PY*pt then
                          Canvas.TextOut(x+rowb,y+colb,
                          DBGrid1.Columns[i].Title.Caption)
                        else
                        //其它行,打印每行的内容
                          Printer.Canvas.TextOut(x+rowb,y+colb,
                          DBGrid1.Fields[i].asString);
                      end;
                       //下一列的横坐标,下一列的纵坐标不变
                     x:=x+dbgrid1.Columns.Items[i].Width*ps;
                    end;
                    //打印下一记录
                    if not (y=py*pt) then
                     Table1.next;
                      x:=px*pl; //打印下一行时,横坐标回到行首
                      y:=y+lh;
                   //打印满一页时,新建一页
                   if (y+lh)>(PageHeight-py*pb) then
                     begin
                       NewPage;
                       x:=px*pl;
                       y:=py*pt;
                     end;
                  end;
            EndDoc;
          end;
          Table1.First;//回到DBGrid1首行
          Application.MessageBox('已打印完成','打印',32);
       end;
    end;procedure TForm1.FormActivate(Sender: TObject);
    begin
      //获得打印可选字体,并设置初始字体为DBGrid1的字体,字号
      ComboBox2.Items:=Printer.Fonts;
      ComboBox2.Text:=DBGrid1.Font.Name;
      Spinedit5.value:=DBGrid1.Font.Size;
      Table1.DatabaseName:=ExtractFilePath(Application.ExeName);
      Table1.Open;
    end;
    end.
      

  3. 用ehlib吧,当中有一个DBGrideh,非常好用,我用过的。
      

  4. printatonce,相当于抓图的效果,我只在delphi4下面用过,很爽
      

类似问题 »