delphi如何打印到指定的打印机
1个回答
展开全部
一、设置默认打印机,并在相关代码中,将指定打印机设置为-1;
procedure SetPaperHeight(Value:integer); //设置纸张高度-单位:mmvar Device : array[0..255] of char; Driver : array[0..255] of char; Port : array[0..255] of char; hDMode : THandle; PDMode : PDEVMODE;begin if Value < 90 then Value := 90; //自定义纸张最小高度127mm if Value > 432 then Value := 432; //自定义纸张最大高度432mm Printer.PrinterIndex := Printer.PrinterIndex; Printer.GetPrinter(Device, Driver, Port, hDMode); if hDMode <> 0 then begin pDMode := GlobalLock(hDMode); if pDMode <> nil then begin pDMode^.dmFields := pDMode^.dmFields or DM_PAPERSIZE or DM_PAPERLENGTH; pDMode^.dmPaperSize := DMPAPER_USER; pDMode^.dmPaperLength := Value * 10; pDMode^.dmFields := pDMode^.dmFields or DMBIN_MANUAL; pDMode^.dmDefaultSource := DMBIN_MANUAL; GlobalUnlock(hDMode); end; end; Printer.PrinterIndex := Printer.PrinterIndex;end;
procedure SetPaperWidth(Value:integer); //设置纸张宽度:单位--mmvar Device : array[0..255] of char; Driver : array[0..255] of char; Port : array[0..255] of char; hDMode : THandle; PDMode : PDEVMODE;begin if Value < 100 then Value := 100; //自定义纸张最小宽度76mm if Value > 216 then Value := 216; //自定义纸张最大宽度216mm Printer.PrinterIndex := Printer.PrinterIndex; Printer.GetPrinter(Device, Driver, Port, hDMode); if hDMode <> 0 then begin pDMode := GlobalLock(hDMode); if pDMode <> nil then begin pDMode^.dmFields := pDMode^.dmFields or DM_PAPERSIZE or DM_PAPERWIDTH; pDMode^.dmPaperSize := DMPAPER_USER; pDMode^.dmPaperWidth := Value * 10; //将毫米单位转换为0.1mm单位 pDMode^.dmFields := pDMode^.dmFields or DMBIN_MANUAL; pDMode^.dmDefaultSource := DMBIN_MANUAL; GlobalUnlock(hDMode); end; end; Printer.PrinterIndex := Printer.PrinterIndex;
Printer.PrinterIndex :=-1;如果设置为为-1,则为系统默认打印机;
end;
二、或者使用打印对话框,代码通过读取用户选择的打印机,进行指定打印机操作。
procedure Tfr_mai1.Button1Click(Sender: TObject);begin PrintDialog1.PrintToFile:=true; PrintDialog1.PrintRange:= prPageNums;
PrintDialog1.Execute;
end;
单击本按钮将会出现一个打印机选择对话框,可供用户选择指定的打印机.
procedure SetPaperHeight(Value:integer); //设置纸张高度-单位:mmvar Device : array[0..255] of char; Driver : array[0..255] of char; Port : array[0..255] of char; hDMode : THandle; PDMode : PDEVMODE;begin if Value < 90 then Value := 90; //自定义纸张最小高度127mm if Value > 432 then Value := 432; //自定义纸张最大高度432mm Printer.PrinterIndex := Printer.PrinterIndex; Printer.GetPrinter(Device, Driver, Port, hDMode); if hDMode <> 0 then begin pDMode := GlobalLock(hDMode); if pDMode <> nil then begin pDMode^.dmFields := pDMode^.dmFields or DM_PAPERSIZE or DM_PAPERLENGTH; pDMode^.dmPaperSize := DMPAPER_USER; pDMode^.dmPaperLength := Value * 10; pDMode^.dmFields := pDMode^.dmFields or DMBIN_MANUAL; pDMode^.dmDefaultSource := DMBIN_MANUAL; GlobalUnlock(hDMode); end; end; Printer.PrinterIndex := Printer.PrinterIndex;end;
procedure SetPaperWidth(Value:integer); //设置纸张宽度:单位--mmvar Device : array[0..255] of char; Driver : array[0..255] of char; Port : array[0..255] of char; hDMode : THandle; PDMode : PDEVMODE;begin if Value < 100 then Value := 100; //自定义纸张最小宽度76mm if Value > 216 then Value := 216; //自定义纸张最大宽度216mm Printer.PrinterIndex := Printer.PrinterIndex; Printer.GetPrinter(Device, Driver, Port, hDMode); if hDMode <> 0 then begin pDMode := GlobalLock(hDMode); if pDMode <> nil then begin pDMode^.dmFields := pDMode^.dmFields or DM_PAPERSIZE or DM_PAPERWIDTH; pDMode^.dmPaperSize := DMPAPER_USER; pDMode^.dmPaperWidth := Value * 10; //将毫米单位转换为0.1mm单位 pDMode^.dmFields := pDMode^.dmFields or DMBIN_MANUAL; pDMode^.dmDefaultSource := DMBIN_MANUAL; GlobalUnlock(hDMode); end; end; Printer.PrinterIndex := Printer.PrinterIndex;
Printer.PrinterIndex :=-1;如果设置为为-1,则为系统默认打印机;
end;
二、或者使用打印对话框,代码通过读取用户选择的打印机,进行指定打印机操作。
procedure Tfr_mai1.Button1Click(Sender: TObject);begin PrintDialog1.PrintToFile:=true; PrintDialog1.PrintRange:= prPageNums;
PrintDialog1.Execute;
end;
单击本按钮将会出现一个打印机选择对话框,可供用户选择指定的打印机.
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |