Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
163
Добавлен:
11.05.2015
Размер:
4.73 Mб
Скачать

Example

The following example shows how to use the Mode property to check DevMode settings for the Printer properties saved with the Report (Orientation in this case). Note that PMode could be used instead, which would eliminate the need for GlobalLock/GlobalUnlock:

var

 

hMode

: THandle;

pMode

: PDevMode;

nOrientation : integer; begin

{Set ReportName}

Crpe1.ReportName := 'C:\Company.rpt'; {Get Printer settings from Report} Crpe1.Printer.Retrieve;

{Check the Orientation} hMode := Crpe1.Printer.Mode; pMode := GlobalLock(hMode); case pMode^.dmOrientation of

DMORIENT_PORTRAIT : nOrientation := 0; DMORIENT_LANDSCAPE : nOrientation := 1;

end; GlobalUnlock(hMode);

end;

Printer Name Property

Declaration

property Name: TCrPrinterName;

Type

TCrPrinterName = string[80];

Description

The Name property contains the Printer name of the Printer.

Traditionally, the Crystal VCL used to require the Name, Driver, Port and Mode properties to be set before it would change the Printer for a Report. With the new Crystal component, however, this is not required. The only property that needs to be specified is the Name. The component will automatically fill in the other values (if the Printer Name is valid), when the component's Execute method is called.

VCL Reference

491

If the Retrieve method is used, the Name property will contain the Printer name for the Printer that was specified in the Report (or the default printer if the Printer specified in the Report is not valid). Alternatively, the GetCurrent method can be called right after showing Delphi's PrintDialog, and it will also fill the Printer properties with values corresponding to the selected Printer.

Additionally, the Name can be specified directly from the Delphi Printers array (you will need to add the Printers unit to the Uses clause before you can use the Printers array):

Crpe1.Printer.Name := Printer.Printers[2];

Example

To change Printers, only the Name property needs to be set (although it will not cause any problems if Driver, Port and Mode properties are set as well). The rest will be retrieved within the VCL:

Crpe1.ReportName := 'C:\Company.rpt';

Crpe1.Printer.Name := 'HP Laserjet 4';

Crpe1.Execute;

This code uses the Printer properties to change Printers to one selected via the Print Dialog. It also makes sure that the Orientation defined in the Report is preserved.

procedure RunReport; var

lpPrinter, lpDriver, lpPort : pChar;

hMode

: THandle;

pMode

: PDevMode;

nOrientation

: integer;

begin

{Set ReportName}

Crpe1.ReportName := 'C:\Company.rpt'; {Get Printer settings from Report} Crpe1.Printer.Retrieve;

{Check the Orientation} hMode := Crpe1.Printer.Mode; pMode := GlobalLock(hMode); case pMode^.dmOrientation of

DMORIENT_PORTRAIT : nOrientation := 0; DMORIENT_LANDSCAPE : nOrientation := 1; end;

GlobalUnlock(hMode);

{Allocate memory for Printer property variables} try

lpPrinter := StrAlloc(255); lpDriver := StrAlloc(255); lpPort := StrAlloc(255); except

VCL Reference

492

if lpPrinter <> nil then StrDispose(lpPrinter); if lpDriver <> nil then StrDispose(lpDriver);

if lpPort <> nil then

StrDispose(lpPort);

Exit; end;

{Show the PrintDialog}

if PrintDialog1.Execute then {if OK was clicked} begin

{Get Selected Printer; could also use Crpe1.SelectPrinter} Printer.GetPrinter(lpPrinter, lpDriver, lpPort, hMode); {Set PrinterName}

Crpe1.Printer.Name := StrPas(lpPrinter);

{Set PrinterDriver: Check for Null - Win95 32-bit problem} if Length(StrPas(lpDriver)) <> 0 then Crpe1.Printer.Driver := StrPas(lpDriver)

else

Crpe1.Printer.Driver := StrPas(lpPrinter); {Set PrinterPort}

Crpe1.Printer.Port := StrPas(lpPort); {Set PrinterMode}

Crpe1.Printer.Mode := hMode;

{Make sure the Orientation remains as in Report} pMode := GlobalLock(hMode);

case nOrientation of

0:pMode^.dmOrientation := DMORIENT_PORTRAIT;

1:pMode^.dmOrientation := DMORIENT_LANDSCAPE;

end; GlobalUnlock(hMode);

{Run the Report} Crpe1.Execute; end;

StrDispose(lpPort);

StrDispose(lpDriver);

StrDispose(lpPrinter); end;

The above code can be greatly simplified by using some of the other Printer object methods & properties:

procedure RunReport; begin

{Set ReportName}

Crpe1.ReportName := 'C:\test.rpt';

VCL Reference

493

{Preserve Orientation from Report} Crpe1.Printer.PreserveRptSettings[prOrientation]; {Prompt for Printer at Execute} Crpe1.Printer.ShowDialog := True;

{Run the Report} Crpe1.Execute;

end;

Printer Orientation Property

Declaration

property Orientation: TCrOrientation;

Type

TCrOrientation = (orDefault, orPortrait, orLandscape);

Description

The Orientation property can be used to specify a particular orientation for the Report. To preserve the Orientation as it was when the Report was designed, use the PreserveRptSettings property.

Example

This example illustrates the use of the Orientation property:

{Set ReportName}

Crpe1.ReportName := 'C:\Company.rpt'; {Set the Orientation to Landscape} Crpe1.Printer.Orientation := orLandscape; {Run the Report to Window}

Crpe1.Output := toWindow; Crpe1.Execute;

Printer PMode Property

Declaration

property PMode: Pointer;

VCL Reference

494

Соседние файлы в папке crystal