打印時自定義紙張大小(100分)

  • 主题发起人 主题发起人 wei2003
  • 开始时间 开始时间
W

wei2003

Unregistered / Unconfirmed
GUEST, unregistred user!
我把數據轉Excel時,我要用到自定義紙張,
我用以下程序控制:
procedure SetPaperSize(X, Y: Integer);
var
Device: array[0..255] of char;
Driver: array[0..255] of char;
Port: array[0..255] of char;
hDMode: THandle;
PDMode: PDEVMODE;
begin
Printer.PrinterIndex := Printer.PrinterIndex;
Printer.GetPrinter(Device, Driver, Port, hDMode);
if hDMode<>0 then
begin
pDMode := GlobalLock(hDMode);
if pDMode<>nil then
begin
if (x=0) or (y=0) then
begin
{Set to legal}
pDMode^.dmFields := pDMode^.dmFields or dm_PaperSize;
{pDMode^.dmPaperSize := DMPAPER_LEGAL; changed by wulianmin}
pDMode^.dmPaperSize := DMPAPER_FANFOLD_US;
end
else
begin
{Set to custom size}
pDMode^.dmFields := pDMode^.dmFields or
DM_PAPERSIZE or
DM_PAPERWIDTH or
DM_PAPERLENGTH;
pDMode^.dmPaperSize := DMPAPER_USER;
pDMode^.dmPaperWidth := x {SomeValueInTenthsOfAMillimeter};
pDMode^.dmPaperLength := y {SomeValueInTenthsOfAMillimeter};
end;
{Set the bin to use}
pDMode^.dmFields := pDMode^.dmFields or DMBIN_MANUAL;
pDMode^.dmDefaultSource := DMBIN_MANUAL;

GlobalUnlock(hDMode);
end;
end;
Printer.PrinterIndex := Printer.PrinterIndex;
//以下?始打印
end;

我做紙張大小及印表機的變更,但是卻在預覽時無法改變紙張大小及印表機
請各位大富翁賜教
 
你是用什么做预览的?
 
给一段设置预览大小的代码给你:
procedure TPreviewForm.SetPreviewSize;
begin
FPageWidth := trunc(GetDeviceCaps(Printer.Handle, PHYSICALWIDTH) / GetDeviceCaps(Printer.Handle, logpixelsx)*GetDeviceCaps(Image1.Canvas.Handle, logpixelsx));
FPageheight := trunc(GetDeviceCaps(Printer.Handle, PHYSICALHEIGHT) / GetDeviceCaps(Printer.Handle, logpixelsy)*GetDeviceCaps(Image1.Canvas.Handle, logpixelsy));
Image1.Width := trunc(FPageWidth * iScale/100);
Image1.Height := trunc(FPageHeight * iScale/100);
Image1.Picture.Bitmap.Width := trunc(FPageWidth * iScale/100);
Image1.Picture.Bitmap.Height := trunc(FPageHeight * iScale/100);
//Application.MessageBox(PChar('Image1.Height:'+IntToStr(Image1.Height)), szProgramTitle, MB_OK+MB_ICONSTOP);
//Application.MessageBox(PChar('Image1.Width:'+IntToStr(Image1.Width)), szProgramTitle, MB_OK+MB_ICONSTOP);
end;
 
不好意思,我看错了题目。你用你的以上程序只是改变当前程序(你的程序)的进程变量,并没有改变excel的进程变量,当然是打印、预览不正常了,如果要解决,只能使用VBA来解决了。
祝你好运!
 
还有,以上代码在win98下改变纸张设置是没有问题的,但在winnt/2k/xp下不能生效,请参照使用如下代码:
procedure ChangePaperTypeExt0(PageStyle, PageWidth, PageHeight, nOrientation: Integer);
var FDevice, FDriver, FPort:array [0..255] of char;
DeviceMode,HPrt:THandle;DevMode:PDeviceMode;
begin
iPageWidth:=PageWidth;
iPageHeight:=PageHeight;
//Application.MessageBox(PChar('宽度:'+IntToStr(nPageWidth)), szProgramTitle, MB_OK+MB_ICONERROR);
//Application.MessageBox(PChar('PageHeight:'+IntToStr(PageHeight)), szProgramTitle, MB_OK+MB_ICONSTOP);
//Application.MessageBox(PChar('PageWidth:'+IntToStr(PageWidth)), szProgramTitle, MB_OK+MB_ICONSTOP);
//Application.MessageBox(PChar('iPageHeight:'+IntToStr(iPageHeight)), szProgramTitle, MB_OK+MB_ICONSTOP);
//Application.MessageBox(PChar('iPageWidth:'+IntToStr(iPageWidth)), szProgramTitle, MB_OK+MB_ICONSTOP);
iLeftMargin:=nLeftMargin;
iTopMargin:=nTopMargin;
iRightMargin:=nRightMargin;
iBottomMargin:=nBottomMargin;

Printer.GetPrinter(FDevice, FDriver, FPort, DeviceMode);
DevMode := GlobalLock(DeviceMode);
OpenPrinter(FDevice, HPrt, nil);
begin
DevMode^.dmFields := DevMode^.dmFields or dm_paperwidth or dm_paperlength or dm_papersize;
if nOrientation=0 then
DevMode^.dmOrientation := DMORIENT_PORTRAIT
else
DevMode^.dmOrientation := DMORIENT_LANDSCAPE;
case nOrientation of
0:
begin
DevMode^.dmPaperWidth := PageWidth*10;
DevMode^.dmPaperLength := PageHeight*10;
end;
1:
begin
DevMode^.dmPaperWidth := PageHeight*10;
DevMode^.dmPaperLength := PageWidth*10;
end;
end;
DevMode^.dmPaperSize := PageStyle;
end;
//SetPrinter(HPrt,2,DeviceMode,PRINTER_CONTROL_SET_STATUS);
Printer.SetPrinter(FDevice, FDriver, FPort, DeviceMode);
DocumentProperties(DeviceMode,HPrt,FDevice,DevMode^,DevMode^,DM_IN_BUFFER or DM_UPDATE);
GlobalUnlock(DeviceMode);
ClosePrinter(HPrt);
end;
 
实例:http://www.softreg.com.cn/shareware_view.asp?id=/9DE34D01-6F82-4927-8408-4B111C1D2C48/
 
如果是excel就可以調用以下
sh1.pagesetup.papersize:=GetPaperno;
function GetPaperno:integer;
var
printername:string;
dwPapers: DWord;
lpwPapers: array[0..255] of Word;
papersize:array[0..63,0..1] of integer;
begin
result:=0;
printername:=printer.Printers[printer.PrinterIndex];
dwPapers:=DeviceCapabilities(PChar(PrinterName),'LPT1',DC_PAPERS,@lpwPapers,nil);
DeviceCapabilities(PChar(PrinterName),'LPT1',DC_PAPERsize,@Papersize,nil);
while (dwPapers > 0) do
begin
// showmessage('u:'+inttostr(Papersize[dwpapers,0]));
// showmessage('u:'+inttostr(Papersize[dwpapers,1]));
if (Papersize[dwpapers,0]=2150) and (Papersize[dwpapers,1]=1400) then
begin
result:=lpwPapers[dwPapers];
// showmessage(inttostr(lpwPapers[dwPapers]));
end;
{
case DWORD(lpwPapers[dwPapers]) of
DMPAPER_USER: showmessage(inttostr(lpwPapers[dwPapers]));
DMPAPER_A3: showmessage(inttostr(lpwPapers[dwPapers]));
DMPAPER_A4: showmessage(inttostr(DWORD(lpwPapers[dwPapers])));
DMPAPER_B5: showmessage(inttostr(lpwPapers[dwPapers]));
end; }
dwPapers:=dwPapers-1;
end;
end;
 
接受答案了.
 

Similar threads

S
回复
0
查看
700
SUNSTONE的Delphi笔记
S
S
回复
0
查看
695
SUNSTONE的Delphi笔记
S
I
回复
0
查看
854
import
I
后退
顶部