⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pjlistview.pas

📁 GREATIS Print Suite Pro for Delphi (3-7,2005,2006,2007) and C++ Builder (3-6) Set of components for
💻 PAS
字号:
(*  GREATIS PRINT SUITE PRO                          *)
(*  unit version 1.85.014                            *)
(*  Copyright (C) 2001-2007 Greatis Software         *)
(*  http://www.greatis.com/delphicb/printsuite/      *)
(*  http://www.greatis.com/delphicb/printsuite/faq/  *)
(*  http://www.greatis.com/bteam.html                *)

unit PJListView;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  PSJob, PJGrid, PJTextGrid, ComCtrls;

type
  TCustomListViewPrintJob = class(TSpecialTextGridPrintJob)
  private
    { Private declarations }
    FListView: TListView;
    FDrawIcons: Boolean;
    FSelectedOnly: Boolean;
    procedure SetListView(const Value: TListView);
    procedure SetDrawIcons(const Value: Boolean);
    procedure SetSelectedOnly(const Value: Boolean);
    function GetRowIndex(ARow: Integer): Integer;
  protected
    { Protected declarations }
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
    property ListView: TListView read FListView write SetListView;
    property DrawIcons: Boolean read FDrawIcons write SetDrawIcons default False;
    property SelectedOnly: Boolean read FSelectedOnly write SetSelectedOnly default False;
  public
    { Public declarations }
    procedure Update; override;
    procedure DrawCell(TheCanvas: TCanvas; TheRect: TRect; PageIndex,ACol,ARow: Integer; Target: TDrawTarget); override;
    function GetColWidth(TheCanvas: TCanvas; ACol: Integer): Integer; override;
    function GetCellText(APageIndex,ACol,ARow: Integer): string; override;
    function GetCellAlignment(ACol,ARow: Integer): TAlignment; override;
  published
    { Published declarations }
  end;

  TListViewPrintJob = class(TCustomListViewPrintJob)
  published
    // TCustomListViewPrintJob properties
    property ListView;
    property DrawIcons;
    property SelectedOnly;
    // TCustomTextGridPrintJob properties
    property HeaderFont;
    property PageFont;
    property FooterFont;
    property ClipMode;
    // TCustomGridPrintJob properties
    property OnCustomizeCellCanvas;
    property RowsPerPage;
    property TableBorders;
    property HeaderCellBorders;
    property PageCellBorders;
    property FooterCellBorders;
    property IndexColumn;
    property IndexStart;
    // TCustomPrintJob properties
    property MultiDoc;
    property Title;
    property Margins;
    property MarginsUnits;
    property MarginsError;
    property Header;
    property HeaderUnits;
    property Footer;
    property FooterUnits;
    property PageMode;
    property PageWidth;
    property PageHeight;
    property PageUnits;
    property Orientation;
    property Options;
    property RelativeCoords;
    property DefaultDrawing;
    // TCustomPrintJob events
    property OnCreate;
    property OnDestroy;
    property OnDraw;
    property OnPrinterSetupChange;
    property OnStartPrint;
    property OnEndPrint;
    property OnPrintProgress;
    property OnStartPrintPage;
    property OnEndPrintPage;
    property OnUpdate;
  end;

procedure Register;

implementation

procedure TCustomListViewPrintJob.SetListView(const Value: TListView);
begin
  if Value<>FListView then
  begin
    FListView:=Value;
    Update;
  end;
end;

procedure TCustomListViewPrintJob.SetDrawIcons(const Value: Boolean);
begin
  if Value<>FDrawIcons then
  begin
    FDrawIcons:=Value;
    Update;
  end;
end;

procedure TCustomListViewPrintJob.SetSelectedOnly(const Value: Boolean);
begin
  if Value<>FSelectedOnly then
  begin
    FSelectedOnly:=Value;
    Update;
  end;
end;

function TCustomListViewPrintJob.GetRowIndex(ARow: Integer): Integer;
var
  i,Index: Integer;
begin
  if Assigned(FListView) then
    with FListView do
      if FSelectedOnly then
      begin
        Index:=-1;
        Result:=-1;
        for i:=0 to Pred(Items.Count) do
        begin
          if Items[i].Selected then Inc(Index);
          if Index=ARow then
          begin
            Result:=i;
            Break;
          end;
        end;
      end
      else Result:=ARow
  else Result:=-1;
end;

procedure TCustomListViewPrintJob.Notification(AComponent: TComponent; Operation: TOperation);
begin
  inherited;
  if (Operation=opRemove) and Assigned(FListView) and (AComponent=FListView) then
    FListView:=nil;
end;

procedure TCustomListViewPrintJob.Update;
begin
  if Assigned(FListView) then
    with FListView,Items do
    begin
      ColCount:=Columns.Count;
      if FSelectedOnly then
      begin
        if GetRowIndex(0)=-1 then RowCount:=0
        else RowCount:=SelCount;
      end
      else RowCount:=Items.Count;
    end
  else
  begin
    ColCount:=0;
    RowCount:=0;
  end;
  inherited;
end;

procedure TCustomListViewPrintJob.DrawCell(TheCanvas: TCanvas; TheRect: TRect; PageIndex,ACol,ARow: Integer; Target: TDrawTarget);
var
  Bitmap: TBitmap;
  R: TRect;
  Images: TImageList;
begin
  if Assigned(FListView) and
    FDrawIcons and
    (ARow<>rowHeader) and
    (ARow<>rowFooter) and
    (GetRowIndex(ARow)<>-1) and 
    (ACol=0) then
    with FListView,TheCanvas do
    begin
      CustomizeCellCanvas(ACol,ARow,TheCanvas);
      Images:=TImageList(LargeImages);
      if not Assigned(Images) then Images:=TImageList(SmallImages);
      if Assigned(Images) then
      begin
        Bitmap:=TBitmap.Create;
        try
          Images.GetBitmap(Items[GetRowIndex(ARow)].ImageIndex,Bitmap);
          with TheRect do
          begin
            R.Top:=((Bottom+Top)-Abs(Font.Height)) div 2;
            R.Bottom:=R.Top+Abs(Font.Height);
            R.Left:=Left+R.Top-Top;
            R.Right:=R.Left+Abs(Font.Height);
          end;
          DrawBitmap(TheCanvas,R,Bitmap,Target);
          TheRect.Left:=R.Right;
          inherited;
        finally
          Bitmap.Free;
        end;
      end
      else inherited;
    end
  else inherited;
end;

function TCustomListViewPrintJob.GetColWidth(TheCanvas: TCanvas; ACol: Integer): Integer;
var
  W,CW,C: Integer;
  OldFont: TFont;
begin
  if Assigned(FListView) then
    with TheCanvas do
    begin
      OldFont:=TFont.Create;
      try
        OldFont.Assign(Font);
        Font.Assign(PageFont);
        try
          if ACol=colIndex then Result:=inherited GetColWidth(TheCanvas,ACol)
          else
          begin
            with GetPageRect do W:=Right-Left;
            if IndexColumn then Dec(W,inherited GetColWidth(TheCanvas,colIndex));
            CW:=0;
            with FListView,Columns do
            begin
              for C:=0 to Pred(Count) do Inc(CW,Columns[C].Width);
              if ACol<Count then Result:=Pred(W*Columns[ACol].Width div CW)
              else Result:=0;
            end;
          end;
        finally
          Font.Assign(OldFont);
        end;
      finally
        OldFont.Free;
      end;
    end
  else Result:=100;
end;

function TCustomListViewPrintJob.GetCellText(APageIndex,ACol,ARow: Integer): string;
begin
  if Assigned(FListView) and (FListView.Items.Count>0) then
    with FListView,Items do
      case ARow of
        rowHeader:
          case ACol of
            colIndex: Result:='#'
          else Result:=Columns[ACol].DisplayName;
          end;
        rowFooter: Result:=inherited GetCellText(APageIndex,ACol,ARow);
      else
        if GetRowIndex(ARow)<>-1 then
          with Items[GetRowIndex(ARow)] do
            case ACol of
              colIndex: Result:=inherited GetCellText(APageIndex,ACol,ARow);
              0: Result:=Caption;
            else
              if Pred(ACol)<SubItems.Count then Result:=SubItems[Pred(ACol)]
              else Result:='';
            end
        else Result:='';
      end
  else Result:='';
end;

function TCustomListViewPrintJob.GetCellAlignment(ACol,ARow: Integer): TAlignment;
begin
  if Assigned(FListView) and (ACol<FListView.Columns.Count) then
    if ACol=colIndex then Result:=taRightJustify
    else Result:=FListView.Columns[ACol].Alignment
  else Result:=taLeftJustify;
end;

procedure Register;
begin
  RegisterComponents('Print Jobs', [TListViewPrintJob]);
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -