doGetPaperName implemented for windows, fixed selecting default printer

git-svn-id: trunk@8348 -
This commit is contained in:
jesus 2005-12-20 14:31:27 +00:00
parent c392f80e68
commit 6fece2d9f5
7 changed files with 67 additions and 23 deletions

View File

@ -1,4 +1,4 @@
{ To jest automatycznie wygenerowany plik zasobów lazarusa } { This is an automatically generated lazarus resource file }
LazarusResources.Add('TForm1','FORMDATA',[ LazarusResources.Add('TForm1','FORMDATA',[
'TPF0'#6'TForm1'#5'Form1'#13'ActiveControl'#7#7'Button2'#7'Caption'#6#5'Form1' 'TPF0'#6'TForm1'#5'Form1'#13'ActiveControl'#7#7'Button2'#7'Caption'#6#5'Form1'

View File

@ -85,6 +85,11 @@ begin
SGrid.Clean; SGrid.Clean;
with Printer do with Printer do
begin begin
if Printers.Count=0 then
begin
AddInfo('printer', 'no printers are installed');
exit;
end;
AddInfo('Printer',Printers[PrinterIndex]); AddInfo('Printer',Printers[PrinterIndex]);
case Orientation of case Orientation of
poPortrait : AddInfo('Orientation','Portrait'); poPortrait : AddInfo('Orientation','Portrait');
@ -129,7 +134,7 @@ end;
procedure TForm1.Button3Click(Sender: TObject); procedure TForm1.Button3Click(Sender: TObject);
begin begin
Printer.PrinterIndex := 0; Printer.PrinterIndex := -1;
UpdatePrinterInfo; UpdatePrinterInfo;
end; end;

View File

@ -16,24 +16,24 @@
<LazDoc Paths=""/> <LazDoc Paths=""/>
<Units Count="4"> <Units Count="4">
<Unit0> <Unit0>
<CursorPos X="1" Y="14"/> <CursorPos X="19" Y="15"/>
<Filename Value="selectprinter.lpr"/> <Filename Value="selectprinter.lpr"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<TopLine Value="1"/> <TopLine Value="1"/>
<UnitName Value="selectprinter"/> <UnitName Value="selectprinter"/>
<UsageCount Value="140"/> <UsageCount Value="150"/>
</Unit0> </Unit0>
<Unit1> <Unit1>
<CursorPos X="29" Y="32"/> <CursorPos X="47" Y="102"/>
<EditorIndex Value="0"/> <EditorIndex Value="0"/>
<Filename Value="frmselprinter.pas"/> <Filename Value="frmselprinter.pas"/>
<ComponentName Value="Form1"/> <ComponentName Value="Form1"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<Loaded Value="True"/> <Loaded Value="True"/>
<ResourceFilename Value="frmselprinter.lrs"/> <ResourceFilename Value="frmselprinter.lrs"/>
<TopLine Value="21"/> <TopLine Value="85"/>
<UnitName Value="frmselprinter"/> <UnitName Value="frmselprinter"/>
<UsageCount Value="140"/> <UsageCount Value="150"/>
</Unit1> </Unit1>
<Unit2> <Unit2>
<CursorPos X="10" Y="296"/> <CursorPos X="10" Y="296"/>
@ -41,7 +41,7 @@
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<TopLine Value="1"/> <TopLine Value="1"/>
<UnitName Value="text"/> <UnitName Value="text"/>
<UsageCount Value="86"/> <UsageCount Value="96"/>
</Unit2> </Unit2>
<Unit3> <Unit3>
<CursorPos X="1" Y="1"/> <CursorPos X="1" Y="1"/>
@ -49,7 +49,7 @@
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<TopLine Value="1"/> <TopLine Value="1"/>
<UnitName Value="text"/> <UnitName Value="text"/>
<UsageCount Value="86"/> <UsageCount Value="96"/>
</Unit3> </Unit3>
</Units> </Units>
<PublishOptions> <PublishOptions>

View File

@ -324,6 +324,22 @@ begin
end; end;
end; end;
function TWinPrinter.DoGetPaperName: string;
var i : Integer;
PDev : TPrinterDevice;
begin
Result:=inherited DoGetPaperName;
if (Printers.Count>0) then
begin
PDev:=TPrinterDevice(Printers.Objects[PrinterIndex]);
i:=PaperSize.SupportedPapers.IndexOfObject(TObject(ptrint(PDev.Devmode.dmPaperSize)));
if i<>-1 then
Result:=PaperSize.SupportedPapers.Strings[i];
end;
end;
function TWinPrinter.DoGetDefaultPaperName: string; function TWinPrinter.DoGetDefaultPaperName: string;
var i : Integer; var i : Integer;
PDev : TPrinterDevice; PDev : TPrinterDevice;

View File

@ -28,6 +28,9 @@ uses
Classes, SysUtils,Printers,LCLType,{Forms,}Windows;//,dialogs; Classes, SysUtils,Printers,LCLType,{Forms,}Windows;//,dialogs;
Type Type
{ TWinPrinter }
TWinPrinter = class(TPrinter) TWinPrinter = class(TPrinter)
private private
//fDefaultPrinter : String; //fDefaultPrinter : String;
@ -51,6 +54,7 @@ Type
procedure DoResetPrintersList; override; procedure DoResetPrintersList; override;
procedure DoEnumPapers(Lst : TStrings); override; procedure DoEnumPapers(Lst : TStrings); override;
function DoGetPaperName: string; override;
function DoGetDefaultPaperName: string; override; function DoGetDefaultPaperName: string; override;
procedure DoSetPaperName(aName : string); override; procedure DoSetPaperName(aName : string); override;
function DoGetPaperRect(aName : string; Var aPaperRc : TPaperRect) : Integer; override; function DoGetPaperRect(aName : string; Var aPaperRc : TPaperRect) : Integer; override;

View File

@ -75,7 +75,7 @@ begin
CopyMemory(@PDev.DevMode,DevMode,sizeof(Pdev.DevMode)); CopyMemory(@PDev.DevMode,DevMode,sizeof(Pdev.DevMode));
PDev.DevMode.dmSize := SizeOf(PDev.DevMode); PDev.DevMode.dmSize := SizeOf(PDev.DevMode);
PDev.DevMode.dmDriverExtra := 0; PDev.DevMode.dmDriverExtra := 0;
PDev.DefaultPaper := DevMode^.dmPaperSize; PDev.DevMode.dmPaperSize := DevMode^.dmPaperSize;
finally finally
GlobalUnlock(lpp.hDevMode); GlobalUnlock(lpp.hDevMode);
end; end;
@ -158,7 +158,7 @@ begin
CopyMemory(@PDev.DevMode,DevMode,sizeof(Pdev.DevMode)); CopyMemory(@PDev.DevMode,DevMode,sizeof(Pdev.DevMode));
PDev.DevMode.dmSize := SizeOf(PDev.DevMode); PDev.DevMode.dmSize := SizeOf(PDev.DevMode);
PDev.DevMode.dmDriverExtra := 0; PDev.DevMode.dmDriverExtra := 0;
PDev.DefaultPaper := DevMode^.dmPaperSize; PDev.DevMode.dmPaperSize := Devmode^.dmPaperSize;
TWinPrinter(Printer).Handle := hDC; TWinPrinter(Printer).Handle := hDC;
finally finally
GlobalUnlock(lpp.hDevMode); GlobalUnlock(lpp.hDevMode);
@ -261,11 +261,11 @@ begin
CopyMemory(@PDev.DevMode,DevMode,sizeof(Pdev.DevMode)); CopyMemory(@PDev.DevMode,DevMode,sizeof(Pdev.DevMode));
PDev.DevMode.dmSize := SizeOf(PDev.DevMode); PDev.DevMode.dmSize := SizeOf(PDev.DevMode);
PDev.DevMode.dmDriverExtra := 0; PDev.DevMode.dmDriverExtra := 0;
if Printer.PaperSize.SupportedPapers.IndexOfObject(TObject(Integer(DevMode^.dmPaperSize))) <> -1 if Printer.PaperSize.SupportedPapers.IndexOfObject(TObject(ptrint(DevMode^.dmPaperSize))) <> -1
then then
PDev.DefaultPaper := DevMode^.dmPaperSize PDev.DevMode.dmPaperSize := DevMode^.dmPaperSize
else else
PDev.DefaultPaper := Integer(Printer.PaperSize.SupportedPapers.Objects[0]); PDev.DevMode.dmPaperSize := PDev.DefaultPaper;
if nCopies=1 then Copies := DevMode^.dmCopies if nCopies=1 then Copies := DevMode^.dmCopies
else else
Copies := nCopies; Copies := nCopies;

View File

@ -355,8 +355,25 @@ end;
//Set the current printer //Set the current printer
procedure TPrinter.SetPrinter(aName: String); procedure TPrinter.SetPrinter(aName: String);
var i : Integer; var
i,oldIndex : Integer;
begin begin
if aName='*' then begin
// select default printer
OldIndex := FPrinterIndex;
fPrinterIndex := -1; // avoid to remember last printer
Refresh;
if Printers.count>0 then begin
i:= doSetprinter(FPrinters[0]); // now first printer is default
if i<>0 then begin
// something went wrong, try to restore old printer
if OldIndex>=0 then
FPrinterIndex := doSetPrinter(FPrinters[OldIndex]);
raise EPrinter.Create('Unable to set default printer!');
end else
FPrinterIndex := i;
end;
end else
if (Printers.Count>0) then if (Printers.Count>0) then
begin begin
if (aName<>'') then if (aName<>'') then
@ -511,16 +528,17 @@ begin
CheckPrinting(False); CheckPrinting(False);
if Printers.Count>0 then if Printers.Count>0 then
begin begin
aName:='*';
if AValue=-1 then if AValue=-1 then
AValue:=0 {Default printer} aName:='*'
else else
if AValue<Printers.Count then if (AValue>=0) and (AValue<Printers.Count) then
aName:=Printers.Strings[AValue]; aName:=Printers.Strings[AValue]
else
raise EPrinter.Create('Printer index out of range !');
SetPrinter(aName); SetPrinter(aName);
end end
else raise EPrinter.Create('No printers defined !'); else
raise EPrinter.Create('No printers defined !');
end; end;
//If not Printer selected, Select the default printer //If not Printer selected, Select the default printer
@ -723,7 +741,8 @@ begin
if aName<>DefaultPaperName then if aName<>DefaultPaperName then
fOwnedPrinter.DoSetPaperName(aName) fOwnedPrinter.DoSetPaperName(aName)
end end
else raise EPrinter.Create(Format('Paper "%s" not supported !',[aName])); else
raise EPrinter.Create(Format('Paper "%s" not supported !',[aName]));
end; end;
//Return an TPaperRect corresponding at an paper name //Return an TPaperRect corresponding at an paper name