procedure TForm1.Button3Click(Sender: TObject);
var
aDevice,aDriver,aPort:array[0..255] of char;
DeviceHandle:Thandle;
DevMode
DeviceMode;// 指向TDeviceMode 结构的指针
begin
printer:=Tprinter.Create;
printer.GetPrinter(aDevice,aDriver,aPort,Devicehandle);
//首先获取Tprinter的TDeviceMode 结构的句柄
if deviceHandle=0 then
//若handle=0 ,则设备没有装入,设置printer index,再装入
begin
printer.printerIndex:=Printer.PrinterIndex;
printer.GetPrinter(aDevice,aDriver,aPort,Devicehandle);
end;
if deviceHandle=0 then
//若handle=0,则发生错误,产生异常
raise Exception.create('不能初始化TDeviceMode 结构')
else
// 否则用GlobalLock获取TDeviceMode 结构的指针
begin
DevMode:=GlobalLock(DeviceHandle);
with DevMode^do
begin
dmFields :=dmFields or DMBIN_MANUAL;
//设置纸张来源
case ComboBox1.Items.IndexOf(combobox1.text) of
0: dmDefaultSource:=DMBIN_Auto;
1: dmDefaultSource:=DMBIN_MANUAL;//手动送纸
end;
end;
end;
//else
if not DeviceHandle=0 then
GlobalUnlock(DeviceHandle);
end;