打印和打印预蓝

var
  M_RateX,M_RateY:Double;
begin
  // 水平每毫米象素数, float
  M_RateX:=GetDeviceCaps(printer.Handle,HORZRES)/GetDeviceCaps(printer.Handle,HORZSIZE);
  //铅直每毫米象素数, float
  M_RateY:=GetDeviceCaps(printer.Handle,VERTRES)/GetDeviceCaps(printer.Handle,VERTSIZE);
 
  //在以毫米为单位的位置(30,40)开始打印一行字
  Printer.Canvas.Font.Style:=[]; 
  Printer.Canvas.Font.Size :=10; 
  Printer.Canvas.Font.Name :='宋体';
  Printer.Canvas.TextOut(30*M_Ratex,40*M_RateY'测试打印内容!');
  //画以毫米为单位的线,从(10,10)到(110,10)
  Printer.Canvas.Pen.Width :=1;
  Printer.Canvas.MoveTo(round(10*M_Ratex),round(10*M_Ratey));
  Printer.Canvas.LineTo(round(110*M_Ratex),round(10*M_Ratey)) ;
end; 


function GetAveCharSize(Canvas: TCanvas): TPoint;
var
  I: Integer;
  Buffer: array[0..51] of Char;
begin
  for I := 0 to 25 do
    Buffer[I] := Chr(I + Ord('A'));
  for I := 0 to 25 do
    Buffer[I + 26] := Chr(I + Ord('a'));
  GetTextExtentPoint(Canvas.Handle, Buffer, 52, TSize(Result));
  Result.X := Result.X div 52;
end;

procedure SetDefaultPaperSize(PaperSize, PaperLength,
  PaperWidth, copys: Integer);
var
  Device: array[0..cchDeviceName - 1] of Char;
  Driver: array[0..(MAX_PATH - 1)] of Char;
  Port: array[0..32] of Char;
  hDMode: THandle;
  pDMode: PDevMode;
begin
  Printer.GetPrinter(Device, Driver, Port, hDMode);
  if hDMode <> 0 then
  begin
    pDMode := GlobalLock(hDMode);
    if pDMode <> nil then
    begin
      pDMode^.dmPaperSize := PaperSize;
      pDMode^.dmPaperLength := PaperLength;
      pDMode^.dmPaperWidth := PaperWidth;
      pDMode^.dmCopies := copys;

      pDMode^.dmFields := pDMode^.dmFields or
        DM_PAPERSIZE;
      pDMode^.dmFields :=
        pDMode^.dmFields or DM_PAPERLENGTH;
      pDMode^.dmFields :=
        pDMode^.dmFields or DM_PAPERWIDTH;

      pDMode^.dmFields :=
        pDMode^.dmFields or DM_Copies;

      ResetDC(Printer.Handle, pDMode^);
      GlobalUnlock(hDMode);
    end;
  end;
end;

procedure TForm1.PrintPaint(Sender: TObject);
var
  i, j: integer;
begin
  Fcanvas.TextOut(0, 0, '0厘米');
  i := 0;
  j := 0;
  repeat
    i := i + round(round(1 / 2.54 * Screen.PixelsPerInch * 10000) / 10000);
    j := j + 1;

    FCanvas.Pen.Width := 1;
    Fcanvas.Pen.Style := psDot;
    FCanvas.MoveTo(i, 0);
    FCanvas.LineTo(i, Screen.WorkAreaHeight);

    Fcanvas.TextOut(i, 0, inttostr(j));
  until i > Screen.WorkAreaWidth;

  i := 0;
  j := 0;
  repeat
    i := i + round(round(1 / 2.54 * Screen.PixelsPerInch * 10000) / 10000);
    j := j + 1;
    FCanvas.Pen.Width := 0;
    Fcanvas.Pen.Style := psDot;

    FCanvas.MoveTo(0, i);
    FCanvas.LineTo(Screen.WorkAreaWidth, i);

    Fcanvas.TextOut(0, i, inttostr(j));

  until i > Screen.WorkAreaHeight;

  PrintTxt;
end;


procedure TForm1.Button8Click(Sender: TObject);

begin
  if messagebox(self.Handle, '打印预览,选择 是,实际打印,选择否?', '提示',
    MB_YESNO or MB_ICONQUESTION) = ID_YES then
    IsPreView := true
  else
    ispreview := false;

  if IsPreView then
  try
    FPrintForm := Tform.Create(Application);
    with FPrintForm do
    begin
      BoundsRect := Screen.WorkAreaRect;
      Color := clWhite;
      BorderStyle := bsSingle;
      BorderIcons := [biSystemMenu];

      PaintBox := TPaintBox.Create(FPrintForm);
      with PaintBox do
      begin
        BoundsRect := FPrintForm.ClientRect;
        OnPaint := form1.PrintPaint;
        FCanvas := Canvas;
        Parent := FPrintForm;
      end;
      ShowModal;
    end;

  finally
    FreeAndNil(Fprintform);
  end
  else

    with printer do
    begin
      FCanvas := Canvas;
      BeginDoc;
      try
        PrintTxt;
      finally
        EndDoc;
      end;

    end;
end;


function XcmtoPoint(cmint: integer): integer;
var
  Mx: integer;
begin
  if IsPreView then
    result := round(round(cmint / 2.54 * Screen.PixelsPerInch * 10000) / 10000)
  else
  begin
    mx := GetDeviceCaps(printer.Handle, HORZSIZE);
    result := round(round(cmint / 2.54 * mx * 10000) / 10000);
  end;
end;

function YcmtoPoint(cmint: integer): integer;var  My: integer;begin  if IsPreView then    result := round(round(cmint / 2.54 * Screen.PixelsPerInch * 10000) / 10000)  else  begin    my := GetDeviceCaps(printer.Handle, VERTSIZE);    result := round(round(cmint / 2.54 * my * 10000) / 10000);  end;end;

相关文章
相关标签/搜索