本人写了三个函数,全部无用,现贴出来让大家看看究竟有什么问题?
//更改打印机的页面高度和宽度
procedure TfrmUserList.AlterPrintMetric1(pageWidth, pageHeight: integer);
var
DevMode
DeviceMode;
subDevMode:TDeviceMode;
DeviceMode:THandle;
DevSize:integer;
ADevice, ADriver, APort
char;
DeviceHandle:THandle;
PrintHandle:THandle;
begin
getmem(ADevice,255);
getmem(ADriver,255);
getmem(APort,255);
try
Printer.GetPrinter(ADevice, ADriver, APort, DeviceHandle);
if OpenPrinter(ADevice, PrintHandle, nil) then
begin
DevSize:=DocumentProperties(0, PrintHandle, ADevice, subDevMode,subDevMode, 0);
DeviceMode := GlobalAlloc(GHND,DevSize);
if DeviceMode <> 0 then
begin
DevMode := GlobalLock(DeviceMode);
ifdo
cumentProperties(0, PrintHandle, ADevice, DevMode^,
DevMode^, DM_OUT_BUFFER) < 0 then
begin
GlobalUnlock(DeviceMode);
GlobalFree(DeviceMode);
exit;
end;
DevMode^.dmfields:=dm_papersize or dm_paperlength or dm_paperwidth or DM_ORIENTATION;
if pageWidth>pageHeight then
DevMode^.dmOrientation:=DMORIENT_LANDSCAPE
else
DevMode^.dmOrientation:=DMORIENT_PORTRAIT;
DevMode^.dmpapersize:=DMPAPER_USER;
DevMode^.dmpaperlength:=pageHeight;
DevMode^.dmpaperwidth:=pageWidth;
ifdo
cumentProperties(0,printHandle,ADevice,DevMode^,
DevMode^,DM_IN_BUFFER or DM_OUT_BUFFER)<0 then
begin
;
end;
GlobalUnlock(DeviceMode);
GlobalFree(DeviceMode);
ClosePrinter(printHandle);
//SendMessage ($FFFF, WM_WININICHANGE, 0, 0);
end;
end;
finally
freemem(ADevice,255);
freemem(ADriver,255);
freemem(APort,255);
end;
end;
//更改打印机的页面高度和宽度
procedure TfrmUserList.AlterPrintMetric2(pageWidth, pageHeight: integer);
var
ADevice, ADriver, APort
char;
DeviceHandle:THandle;
pDevmode: PDeviceMode;
PrintHandle:THandle;
newForm:TFormInfo1;
newSize:TSize;
msgError
char;
errorCode:word;
begin
getmem(ADevice,255);
getmem(ADriver,255);
getmem(APort,255);
try
newForm.Flags:=FORM_USER;
newForm.pName:=pchar('jghPaper');
newSize.cx:=round(pageWidth/getdevicecaps(printer.Handle,LOGPIXELSX)*25.4*1000);
newSize.cy:=round(pageHeight/getdevicecaps(printer.Handle,LOGPIXELSY)*25.4*1000);
newForm.Size:=newSize;
newForm.ImageableArea:=rect(0,0,newSize.cx,newSize.cy);
Printer.GetPrinter(ADevice, ADriver, APort, DeviceHandle);
if OpenPrinter(ADevice, PrintHandle, nil) then
begin
deleteForm(printhandle,pchar('jghPaper'));
if not AddForm(PrintHandle,1,@newForm)then
begin
errorCode:=GetLastError();
getmem(msgerror,255);
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER or FORMAT_MESSAGE_FROM_SYSTEM,
nil,
errorCode,
SUBLANG_SYS_DEFAULT,
msgError,
255,
nil
);
showmessage(strpas(msgerror));
freemem(msgerror);
end
else
begin
Printer.GetPrinter(ADevice, ADriver, APort, DeviceHandle);
if DeviceHandle <> 0 then
begin
pDevMode := GlobalLock(DeviceHandle);
if pDevmode <> nil then
begin
GlobalUnlock(DeviceHandle);
pDevmode^.dmFields :=DM_FORMNAME or DM_PAPERSIZE;
pDevmode^.dmPaperSize := DMPAPER_USER;
StrLCopy( pDevmode^.dmFormName, PChar('jghPaper'), CCHFORMNAME-1 );
Printer.SetPrinter(ADevice, ADriver, APort, DeviceHandle);
end;
end;
end;
end;
closePrinter(PrintHandle);
finally
freemem(ADevice,255);
freemem(ADriver,255);
freemem(APort,255);
end;
end;
//更改打印机的页面高度和宽度
procedure TfrmUserList.AlterPrintMetric3(pageWidth, pageHeight: integer);
function WinNTSetForm(PDevMode: PDeviceMode;
Device: PChar): Boolean;
var
hPrinter: THandle;
pForm: Pointer;
cbNeeded: DWORD;
cReturned: DWORD;
FormInfo1: TFormInfo1;
begin
Result := False;
if OpenPrinter(Device, hPrinter, nil) then
begin
pForm := nil;
EnumForms(hPrinter, 1, pForm, 0, cbNeeded, cReturned);
GetMem(pForm, cbNeeded);
//取pForm的大小并分配内存
try
if EnumForms(hPrinter, 1, pForm, cbNeeded, cbNeeded, cReturned) then
begin
if DeleteForm(hPrinter, PChar('jghPaper')) then
Dec(cReturned);
//删除旧的Form
with FormInfo1do
begin
Flags := 0;
pName := PChar('jghPaper');
Size.cx := round(pageWidth/getdevicecaps(printer.Handle,LOGPIXELSX)*25.4*1000);
Size.cy := round(pageHeight/getdevicecaps(printer.Handle,LOGPIXELSY)*25.4*1000);
with ImageAbleAreado
begin
Left := 0;
Top := 0;
Right := Size.cx;
Bottom := Size.cy;
end;
end;
if AddForm(hPrinter, 1, @FormInfo1) then
begin
PDevMode.dmFields := PDevMode.dmFields or DM_PAPERSIZE or DM_FORMNAME;
PDevMode.dmPaperSize :=cReturned + 1;
PDevMode.dmFormName:='jghPaper';
Result := True;
end;
end;
finally
FreeMem(pForm);
end;
end;
end;
var
Device, Driver, Port: array[0..127] of char;
hDevMode: THandle;
PDevMode: PDeviceMode;
begin
Printer.GetPrinter(Device, Driver, Port, hDevMode);
if hDevMode <> 0 then
begin
PDevMode := GlobalLock(hDevMode);
try
WinNTSetForm(PDevMode, Device);
//printer.PrinterIndex:=printer.PrinterIndex;
Printer.SetPrinter(Device, Driver, Port,hDevMode);
finally
GlobalUnlock(hDevMode);
end;
end ;
end;