unit PrinterComponent;
interface
uses
Windows, Messages, SysUtils, Classes, Printers, DesignEditors, DesignIntf;
type
TPrinterNameProperty =
class(TStringProperty)
public
function GetAttributes: TPropertyAttributes;
override;
procedure GetValueList(List: TStrings);
virtual;
procedure GetValues(Proc: TGetStrProc);
override;
end;
TPrinterComponent =
class(TComponent)
private
FPrinterName:
string;
function GetPrinters: TStrings;
public
function IsPrinterInList(
const PrinterName:
string): Boolean;
property Printers: TStrings
read GetPrinters;
published
property PrinterName:
string read FPrinterName
write FPrinterName;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('
Beispiele', [TPrinterComponent]);
RegisterPropertyEditor(
TypeInfo(
string), TPrinterComponent, '
PrinterName', TPrinterNameProperty);
end;
{ TPrinterComponent }
function TPrinterComponent.GetPrinters: TStrings;
begin
Result := Printer.Printers;
end;
{ TZStringProperty }
function TPrinterNameProperty.GetAttributes: TPropertyAttributes;
begin
Result := [paValueList, paSortList];
end;
procedure TPrinterNameProperty.GetValueList(List: TStrings);
begin
List.Assign((GetComponent(0)
as TPrinterComponent).Printers);
end;
procedure TPrinterNameProperty.GetValues(Proc: TGetStrProc);
var
i: Integer;
Values: TStringList;
begin
Values := TStringList.Create;
try
GetValueList(Values);
for i := 0
to Pred(Values.Count)
do
Proc(Values[i]);
finally
Values.Free;
end;
end;
function TPrinterComponent.IsPrinterInList(
const PrinterName:
string): Boolean;
begin
Result := GetPrinters.IndexOf(PrinterName) > -1;
end;
end.