How to Create Custom Print Styles
- 3 minutes to read
The topic describes how to implement a custom print style. The example in the topic is not intended to show how to programmatically customize report settings, but illustrates how to implement the HasStyleOptionsDlg and ShowStyleOptionsDlg methods that are used for displaying a custom print style dialog named in the example “Print Style Types“ (see the code at the end of the topic).
The implementation provides the capability to dynamically add the following print styles to the report at runtime, using the Print Style Types dialog:
Built-in print style “Base Style“ (TBasedxReportLink).
Built-in print style “Standard Style“ (TdxPSPrintStyle).
Custom print style “Custom Style“ (TCustomPrintStyle).
When an application starts, only the “Custom Style“ is initially available to end-users in the report:
To add another style to the report, click the Option… button in the Page Setup dialog to display the Print Style Types dialog. Then, select the type of print style, and then click the dialog’s OK button:
The new style is automatically added to the report:
Unit CustomPrintStyle implements a custom print style (class TCustomPrintStyle):
unit CustomPrintStyle;
interface
uses
// ...
cxButtons, dxPgsDlg;
// ...
type
TfmCustomPrintStyles = class(TForm)
dxPSFileBasedExplorer1: TdxPSFileBasedExplorer;
dxPSEngineController1: TdxPSEngineController;
dxComponentPrinter1: TdxComponentPrinter;
dxPrintStyleManager1: TdxPrintStyleManager;
btnPreview: TcxButton;
btnPageSetup: TcxButton;
dxComponentPrinter1Link1: TcxShellListViewReportLink;
procedure FormCreate(Sender: TObject);
procedure btnPreviewClick(Sender: TObject);
procedure btnPageSetupClick(Sender: TObject);
end;
var
fmCustomPrintStyles: TfmCustomPrintStyles;
implementation
uses
CustomDlg;
{$R *.dfm}
type
{ TCustomPrintStyle }
TCustomPrintStyle = class(TBasedxPrintStyle)
public
function DefaultStyleCaption: string; override;
function HasStyleOptionsDlg: Boolean; override;
function ShowStyleOptionsDlg: Boolean; override;
end;
{ TCustomPrintStyle }
function TCustomPrintStyle.DefaultStyleCaption: string;
begin
Result := 'Custom Style';
end;
function TCustomPrintStyle.HasStyleOptionsDlg: Boolean;
begin
// Return True to enable the "Options..." button in the Page Setup dialog
Result := True;
end;
// Show a custom dialog with print styles
function TCustomPrintStyle.ShowStyleOptionsDlg: Boolean;
begin
with TfmCustomDialog.Create(nil) do
try
Result := ShowModal = mrOk;
if Result then
// Register a selected print style with the style manager
fmCustomPrintStyles.dxPrintStyleManager1.AddStyle(TdxPrintStyleClass(cbPrintStyles.ItemObject));
finally
Free;
end;
end;
{ TfmCustomPrintStyles }
procedure TfmCustomPrintStyles.btnPreviewClick(Sender: TObject);
begin
dxComponentPrinter1Link1.Preview;
end;
procedure TfmCustomPrintStyles.btnPageSetupClick(Sender: TObject);
begin
dxComponentPrinter1Link1.CurrentPrintStyle.PageSetup;
end;
procedure TfmCustomPrintStyles.FormCreate(Sender: TObject);
begin
// Register the custom print style with the style manager when the application starts
dxPrintStyleManager1.AddStyle(TCustomPrintStyle);
end;
initialization
// Register the TCustomPrintStyle class for streaming purposes
dxPSRegisterPrintStyle(TCustomPrintStyle, True);
end.
Unit CustomDlg implements a custom print style dialog (“Print Style Types“):
unit CustomDlg;
interface
uses
// ...
cxButtons, cxDropDownEdit;
// ...
type
TfmCustomDialog = class(TForm)
cbPrintStyles: TcxComboBox;
btnOK: TcxButton;
btnCancel: TcxButton;
procedure FormCreate(Sender: TObject);
end;
var
fmCustomPrintStyles: TfmCustomPrintStyles;
implementation
uses
dxPgsDlg;
{$R *.dfm}
procedure TfmCustomDialog.FormCreate(Sender: TObject);
begin
// Populate the cbPrintStyles control with print styles
dxPSGetRegisteredPrintStylesList(cbPrintStyles.Properties.Items);
cbPrintStyles.ItemIndex := 0;
end;
end.