如何设置打印机?

steave 2000-01-18 08:13:00
请问如何在程序中将打印机设置为默认打印机,如何设置纸张大小等参数??


...全文
1557 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
skt642 2001-05-31
  • 打赏
  • 举报
回复
24814关注!
limodou 2000-01-20
  • 打赏
  • 举报
回复
上次少给出getprinterinfo5的函数了,下面就是
uses
Windows, WinSpool;

function GetPrinterInfo5(Printer: THandle): PPrinterInfo5;

implementation

function GetPrinterInfo(Printer: THandle; Level: Integer):
Pointer;
var
BytesRequired: DWORD;
begin
{ Find out the required size of the structure }
GetPrinter(Printer, Level, nil, 0, @BytesRequired);
GetMem(Result, BytesRequired);
GetPrinter(Printer, Level, Result, BytesRequired,
@BytesRequired);
end;

function GetPrinterInfo5(Printer: THandle): PPrinterInfo5;
begin
Result := GetPrinterInfo(Printer,5);
end;

steave 2000-01-19
  • 打赏
  • 举报
回复
wy email :e37ty@163.net
steave 2000-01-19
  • 打赏
  • 举报
回复
请问 getprinterinfo5 在那里定义?
光明山人 2000-01-19
  • 打赏
  • 举报
回复
不太理解你的意图,难道是程序指定打印机及打印参数?这样如何适应用户的环境?我倒有个建议。
我做过一个程序,用户可以设置打印机及参数,以后每次打印都用该设置,除非用户又改变了设置。我是这样做的:
设计一个类叫TPrinterSettings,它负责保存打印参数、从注册表中读入设置、写设置到注册表中。每次程序启动时要创建该类对象,并从注册表中读入设置,并设置Printers单元中的默认打印机及属性。当用户点击主窗口的打印机设置时,调用TPrinterSetupDialog对象进行设置,设置完了以后,再更新TPrinterSettings对象并存入注册表中。注册表对象是可以从TPrinterSettings外面赋给,因此存入注册表的根目录既可以是HKEY_LOCAL_MACHINE(所有用户)也可以是HKEY_CURRENT_USER(用户各自不同)。

如果你感兴趣,请给我Email,我可以给你源码及用法说明。
limodou 2000-01-19
  • 打赏
  • 举报
回复
设置默认打印机
uses printers;

procedure SetDefaultPrinter(const printerindex:integer);
Var
FHandle : THandle;
HPrt : THandle;
PrtInfo5: PPrinterInfo5;
FDevice: array[0..79] of char;
FDriver: array[0..79] of char;
FPort: array[0..79] of char;

begin

{printerindex为选中打印机的索引,如果使用打印机名称,则此句可忽略}
Printer.PrinterIndex := printerindex;

Printer.GetPrinter (FDevice, FDriver, FPort, FHandle);
OpenPrinter(FDevice, HPrt, nil);
if HPrt = 0 then
raise(Exception.Create('不能打开打印机'));
try
PrtInfo5 := GetPrinterInfo5(HPrt);
PrtInfo5.Attributes := PrtInfo5.Attributes +
PRINTER_ATTRIBUTE_DEFAULT;
SetPrinter(HPrt,5,PrtInfo5,PRINTER_CONTROL_SET_STATUS);
FreeMem(PrtInfo5);
finally
ClosePrinter(HPrt);
end;

end;

设置打印参数是要修改TDeviceMode结构同yjq说的一样
var
ADevice, ADriver, APort:array [0..255] of Char;
DeviceHandle:THandle;
DevMode:PDeviceMode;//TDeviceMode指针
begin
{首先获取TPrinter的DeviceMode结构的句柄}
PrinterGetPrinter(ADevice, ADriver, APort, DeviceHandle);
{如果句柄是0, 表示打印机没有装载}
if DeviceHandle=0 then
begin
Printer.PrinterIndex := Printer.PrinterIndex;
Printer.GetPritner(ADevice, ADriver, APort, DeviceHandle);
end;
{如果DeviceHandle还是0, 表示有错误发生。否则,就调用}
{GlobalLock来获取TDeviceMode结构的指针}
if DeviceHandle=0 then
Raise Exception.Create('Could Not Initialize TDeviceMode structure')
else
DevMode:=GlobalLock(DeviceHandle);
{下面是设置纸张大小}
with DevMode^ do
begin
dmFields:=dmFields or DM_PAPERSIZE;
dmPaperSize:=DMPADER_LETTER;//LETTER,8-1/2
{如果纸张大小由dmPaperWidth和dmPaperLength设置,则dmPaperSize的值可以设为0}
//dmFields:=dmFields or DM_PAPERLENGTH or DM_PAPERWIDTH;
//dmPaperLength:=somelength;
//dmPaperWidth:=somewidth;
end;
if not DeviceHandle=0 then
GlobalUnlock(DeviceHandle);
end;
关于TDeviceMode的详细情况参阅Win32 Programmer's Reference的DEVMODE主题
barton 2000-01-18
  • 打赏
  • 举报
回复

http://members.aye.net/~bstowers/delphi
是正版。
ken 2000-01-18
  • 打赏
  • 举报
回复
调用PRINTSETUP对话框即可。
steave 2000-01-18
  • 打赏
  • 举报
回复
谢谢 请告送我在哪?
barton 2000-01-18
  • 打赏
  • 举报
回复
有一个专门的控件TPageSetup你要不要?
渤海海峡 2000-01-18
  • 打赏
  • 举报
回复
这是我的一端打印机的属性设置程序,直接利用打印机的属性窗体
设置,然后保存设置。
关于打印机的参数设置,把devmode结构搞清楚就全清楚了。

procedure Thxpreviewform.BitBtn4Click(Sender: TObject);
var
Device : array[0..255] of char;
Driver : array[0..255] of char;
Port : array[0..255] of char;
hDMode : THandle;
pname : Pchar;
hname : Thandle;
pdMode,pdOutMode,PdinMode : PdevMode;
buffersize:integer;
begin
Printer.PrinterIndex := Printer.PrinterIndex;
Printer.GetPrinter(Device, Driver, Port, hDMode);
if hDMode <> 0 then
begin
pDMode := GlobalLock(hDMode);
pname :=pchar(printer.Printers[printer.PrinterIndex]);
hname:= printer.Handle;
openprinter(Device,hname,nil);
buffersize:=DocumentProperties(hxpreviewform.Handle,hname,Driver,pdMode^,pdMode^,0);
Getmem(PdOutMode,Buffersize);
Getmem(PdinMode,Buffersize);
PrinterProperties(hxpreviewform.Handle,hname);
DocumentProperties(hxpreviewform.Handle,hname,Driver,pdoutMode^,pdInMode^,DM_COPY);
pdMode^:=pdOutMode^;
freemem(pdOutmode);
freemem(pdinmode);
GlobalUnlock(hDMode);
end;
Printer.PrinterIndex := Printer.PrinterIndex;
end;

5,386

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧