procedure TMainForm.PrintSetupDj(VWidth:integer;
Vheight:integer);
var
VwidthHigh,VwidthLow:integer;
{纸张宽度的高/低位}
VHeightHigh,VHeightLow:integer;
{纸张高度的高/低位}
Reg: TRegistry;
VPrintName:string;
VDefault_DevMode:array of byte;
VDefault_DevModeInt:integer;
VSize:integer;
begin
{数据存放低位在前,高位在后}
VwidthLow:=byte(Vwidth);
VwidthHigh:=byte(Vwidth Shr 8);{右移8位,再取低位}
VHeightLow:=byte(Vheight);
VHeightHigh:=byte(Vheight Shr 8);{右移8位,再取低位}
Reg := TRegistry.Create;
try
begin
Reg.RootKey := HKEY_LOCAL_MACHINE;
Reg.OpenKey('/Config/0001/System/CurrentControlSet/Control/Print/Printers',true);
{OpenKey:在指定的主键名不存在时,是否允许创建该主键,True表示允许。 }
VPrintName:=Reg.ReadString('Default');
Reg.OpenKey('/System/CurrentControlSet/Control/Print/Printers/'+VPrintName,True);
VSize:=Reg.GetDataSize('Default DevMode');
setlength(VDefault_DevMode,VSize);
//设置动态数组的长度
{读取二进制值,Name为二进制值名称,Buffer为接收缓冲区,BufSize为缓冲区大小,返回为实际读取的字节数。}
reg.ReadBinaryData('Default DevMode',VDefault_DevMode[0],VSize);
{设置当前纸张为自定义纸张:代码256}
VDefault_DevMode[40]:=byte(26127);
VDefault_DevMode[41]:=byte(26127 Shr 8);
VDefault_DevMode[46]:=byte(256);
VDefault_DevMode[47]:=byte(256 Shr 8);
VDefault_DevMode[277]:=byte(11008);
VDefault_DevMode[278]:=byte(11008 Shr 8);
{设定纸长}
VDefault_DevMode[48]:=VHeightLow;
VDefault_DevMode[49]:=VHeightHigh;
{设定纸长}
VDefault_DevMode[50]:=VwidthLow;
VDefault_DevMode[51]:=VwidthHigh;
try
reg.WriteBinaryData('Default DevMode',VDefault_DevMode[0],VSize);
finally
// freemem(buffer);
end;
end
finally
Reg.CloseKey;
Reg.Free;
inherited;
end;
end;