📄 enhlistview.pas
字号:
{$I DFS.INC} { Standard defines for all Delphi Free Stuff components }
// Delphi 2 and C++B 1 have incorrectly declared InsertItem as private.
{$IFDEF DFS_COMPILER_3_UP}
{$DEFINE DFS_FIXED_LIST_VIEW}
{$ENDIF}
{.$DEFINE DFS_DEBUG}
{------------------------------------------------------------------------------}
{ TdfsEnhListView v3.72 }
{------------------------------------------------------------------------------}
{ A list view control that provides enhanced functionality beyond the }
{ standard list view. For example, automatic sorting of simple data types, }
{ owner draw event for vsReport mode, and more. This does NOT require any }
{ special version of COMCTL32.DLL. }
{ }
{ Copyright 1998-2001, Brad Stowers. All Rights Reserved. }
{ }
{ Copyright: }
{ All Delphi Free Stuff (hereafter "DFS") source code is copyrighted by }
{ Bradley D. Stowers (hereafter "author"), and shall remain the exclusive }
{ property of the author. }
{ }
{ Distribution Rights: }
{ You are granted a non-exlusive, royalty-free right to produce and distribute }
{ compiled binary files (executables, DLLs, etc.) that are built with any of }
{ the DFS source code unless specifically stated otherwise. }
{ You are further granted permission to redistribute any of the DFS source }
{ code in source code form, provided that the original archive as found on the }
{ DFS web site (http://www.delphifreestuff.com) is distributed unmodified. For }
{ example, if you create a descendant of TDFSColorButton, you must include in }
{ the distribution package the colorbtn.zip file in the exact form that you }
{ downloaded it from http://www.delphifreestuff.com/mine/files/colorbtn.zip. }
{ }
{ Restrictions: }
{ Without the express written consent of the author, you may not: }
{ * Distribute modified versions of any DFS source code by itself. You must }
{ include the original archive as you found it at the DFS site. }
{ * Sell or lease any portion of DFS source code. You are, of course, free }
{ to sell any of your own original code that works with, enhances, etc. }
{ DFS source code. }
{ * Distribute DFS source code for profit. }
{ }
{ Warranty: }
{ There is absolutely no warranty of any kind whatsoever with any of the DFS }
{ source code (hereafter "software"). The software is provided to you "AS-IS", }
{ and all risks and losses associated with it's use are assumed by you. In no }
{ event shall the author of the softare, Bradley D. Stowers, be held }
{ accountable for any damages or losses that may occur from use or misuse of }
{ the software. }
{ }
{ Support: }
{ Support is provided via the DFS Support Forum, which is a web-based message }
{ system. You can find it at http://www.delphifreestuff.com/discus/ }
{ All DFS source code is provided free of charge. As such, I can not guarantee }
{ any support whatsoever. While I do try to answer all questions that I }
{ receive, and address all problems that are reported to me, you must }
{ understand that I simply can not guarantee that this will always be so. }
{ }
{ Clarifications: }
{ If you need any further information, please feel free to contact me directly.}
{ This agreement can be found online at my site in the "Miscellaneous" section.}
{------------------------------------------------------------------------------}
{ The lateset version of my components are always available on the web at: }
{ http://www.delphifreestuff.com/ }
{ See ELV.txt for notes, known issues, and revision history. }
{------------------------------------------------------------------------------}
{ Date last modified: June 28, 2001 }
{------------------------------------------------------------------------------}
unit EnhListView;
interface
{$IFNDEF DFS_WIN32}
ERROR! This unit only available for Delphi 2.0 or higher!!!
{$ENDIF}
uses
Forms, Windows, Messages, Classes, Controls, ComCtrls, CommCtrl, SysUtils,
{$IFDEF DFS_COMPILER_4_UP} ImgList, {$ENDIF} Graphics, StdCtrls, Menus;
const
{ This shuts up C++Builder 3 about the redefiniton being different. There
seems to be no equivalent in C1. Sorry. }
{$IFDEF DFS_CPPB_3_UP}
{$EXTERNALSYM DFS_COMPONENT_VERSION}
{$ENDIF}
DFS_COMPONENT_VERSION = 'TdfsEnhListView v3.72';
DRAWTEXTEX_FLAGS = DT_NOPREFIX or DT_SINGLELINE or DT_VCENTER or
DT_END_ELLIPSIS;
DRAWTEXTEX_ALIGNMENT: array[TAlignment] of UINT = (DT_LEFT, DT_RIGHT,
DT_CENTER);
WM_OWNERDRAWCOLUMNS = WM_USER + 143;
type
TIntArray = array[0..(MaxInt div SizeOf(Integer)-1)] of Integer;
PIntArray = ^TIntArray;
TResizeMethod = (rmFitText, rmFitHeader);
TAutoColumnSort = (acsNoSort, acsSort, acsSortToggle);
TAutoSortStyle = (assSmart, assDefault);
TSortAs = (saNone, saString, saNumeric, saDateTime);
TLVStyle = (lvStandard, lvOwnerDrawFixed);
TLVHDrawItemEvent = procedure(Control: TWinControl; var ACanvas: TCanvas;
Index: Integer; var ARect: TRect; Selected: boolean;
var DefaultDrawing: boolean) of object;
TLVMeasureItemEvent = procedure(Control: TWinControl;
var AHeight: UINT) of object;
TLVDrawItemEvent = procedure(Control: TWinControl; var ACanvas: TCanvas;
Index: Integer; ARect: TRect; State: TOwnerDrawState;
var DefaultDrawing, FullRowSelect: boolean) of object;
TLVDrawSubItemEvent = procedure(Control: TWinControl; var ACanvas: TCanvas;
Index, SubItem: Integer; ARect: TRect; State: TOwnerDrawState;
var DefaultDrawing: boolean) of object;
TLVAfterDrawItemEvent = procedure(Control: TWinControl; var ACanvas: TCanvas;
Index: Integer; ARect: TRect; State: TOwnerDrawState) of object;
TLVSortItemsEvent = procedure(Sender: TObject; Item1, Item2: TListItem;
SortColumn: integer; var SortAs: TSortAs; var CompResult: integer) of object;
TLVSortStatusEvent = procedure(Sender: TObject; SortColumn: integer;
Ascending: boolean) of object;
TLVEditCanceled = procedure(Sender: TObject; Item: TListItem) of object;
{$IFNDEF DFS_COMPILER_4_UP}
TLVNotifyEvent = procedure(Sender: TObject; Item: TListItem) of object;
{$ENDIF}
// Class for saved settings
TdfsEnhLVSaveSettings = class(TPersistent)
private
FAutoSave: boolean;
FRegistryKey: string;
FSaveColumnSizes: boolean;
FSaveCurrentSort: boolean;
FSaveViewStyle: boolean;
public
constructor Create; virtual;
procedure StoreColumnSizes(ColCount: integer;
const IntArray: array of integer);
procedure ReadColumnSizes(ColCount: integer;
var IntArray: array of integer);
procedure StoreCurrentSort(Ascending: boolean; SortCol: integer);
procedure ReadCurrentSort(var Ascending: boolean; var SortCol: integer);
procedure StoreViewStyle(Style: TViewStyle);
function ReadViewStyle(Default: TViewStyle): TViewStyle;
published
property AutoSave: boolean read FAutoSave write FAutoSave default FALSE;
property RegistryKey: string read FRegistryKey write FRegistryKey;
property SaveColumnSizes: boolean
read FSaveColumnSizes
write FSaveColumnSizes
default TRUE;
property SaveCurrentSort: boolean
read FSaveCurrentSort
write FSaveCurrentSort
default TRUE;
property SaveViewStyle: boolean
read FSaveViewStyle
write FSaveViewStyle
default TRUE;
end;
{ The new class }
TCustomEnhListView = class(TCustomListView)
private
FSortDirty: boolean;
FUpdateCount: integer;
FStyle: TLVStyle;
FAutoColumnSort: TAutoColumnSort;
FAutoSortStyle: TAutoSortStyle;
FAutoResort: boolean;
FAutoSortAscending: boolean;
FTmpAutoSortAscending: boolean;
FLastColumnClicked: Integer;
FSaveSettings: TdfsEnhLVSaveSettings;
FShowSortArrows: boolean;
FReverseSortArrows: boolean;
FSortUpBmp,
FSortDownBmp: TBitmap;
FCreatingWindowHandle: boolean;
{$IFDEF BACKGROUND_FIXED}
FBackgroundImage: TBitmap;
{$ENDIF}
FNoColumnResize: boolean;
FOldHeaderWndProc: pointer;
FHeaderInstance: pointer;
FSearchStr: string;
FSearchTickCount: Double;
FColumnSearch: boolean;
FOnSortBegin: TLVSortStatusEvent;
FOnSortFinished: TLVSortStatusEvent;
FOnMeasureItem: TLVMeasureItemEvent;
FOnDrawItem: TLVDrawItemEvent;
FOnDrawSubItem: TLVDrawSubItemEvent;
FOnAfterDefaultDrawItem: TLVAfterDrawItemEvent;
FOnDrawHeader: TLVHDrawItemEvent;
FOnSortItems: TLVSortItemsEvent;
FOnEditCanceled: TLVEditCanceled;
{$IFNDEF DFS_COMPILER_4_UP}
FOnGetImageIndex: TLVNotifyEvent;
{$ENDIF}
procedure HeaderWndProc(var Message: TMessage);
{ Message handlers }
procedure CMSysColorChange(var Message: TWMSysColorChange);
message CM_SYSCOLORCHANGE;
procedure CMFontChanged(var Messsage: TMessage); message CM_FONTCHANGED;
procedure CNMeasureItem(var Message: TWMMeasureItem); message CN_MEASUREITEM;
procedure CNDrawItem(var Message: TWMDrawItem); message CN_DRAWITEM;
procedure CNNotify(var Message: TWMNotify); message CN_NOTIFY;
procedure WMDestroy(var Message: TWMDestroy); message WM_DESTROY;
procedure WMDrawHeader(var Message: TWMDrawItem); message WM_DRAWITEM;
procedure WMNotify(var Message: TWMNotify); message WM_NOTIFY;
procedure WMOwnerDrawColumns(var Message: TMessage);
message WM_OWNERDRAWCOLUMNS;
procedure WMParentNotify(var Message: TWMParentNotify); message WM_PARENTNOTIFY;
protected
{ USE WITH CARE. This can be NIL }
FCanvas: TCanvas;
FHeaderHandle: HWND;
procedure KeyUp(var Key: Word; Shift: TShiftState); override;
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
{$IFNDEF DFS_COMPILER_4_UP}
function GetItem(Value: TLVItem): TListItem;
{$ENDIF}
procedure ResetOwnerDrawHeight;
procedure InvalidateColumnHeader(Index: integer); virtual;
procedure DoSort(ColumnIndex:integer; Descending: boolean); virtual;
procedure SortBegin(ColumnIndex: integer; Ascending: boolean); virtual;
procedure SortFinished(ColumnIndex: integer; Ascending: boolean); virtual;
procedure SortItems(const Item1, Item2: TListItem; SortColumn: integer;
var CompResult: integer); virtual;
procedure MeasureItem(var Height: UINT); virtual;
procedure DefaultDrawItem(Index: Integer; Rect: TRect;
State: TOwnerDrawState; FullRowSelect: boolean); virtual;
procedure DefaultDrawSubItem(Index, SubItem: integer; Rect: TRect;
State: TOwnerDrawState); virtual;
procedure ProcessDrawItemMsg(Index: Integer;
Rect: TRect; State: TOwnerDrawState; var DefaultDrawing,
FullRowSelect: boolean); virtual;
function ActualColumnIndex(Index: integer): integer; virtual;
function GetActualColumn(Index: integer): TListColumn; virtual;
function GetSubItemText(Index, SubItem: integer): string; virtual;
procedure DrawSubItem(Index, SubItem: Integer; Rect: TRect;
State: TOwnerDrawState; var DefaultDrawing: boolean); virtual;
procedure DrawItem(var Canvas: TCanvas; Index: Integer; Rect: TRect;
State: TOwnerDrawState; var DefaultDrawing,
FullRowSelect: boolean);
{$IFDEF DFS_COMPILER_4_UP} reintroduce; overload; {$ENDIF} virtual;
procedure AfterDrawItem(var Canvas: TCanvas; Index: Integer;
Rect: TRect; State: TOwnerDrawState); virtual;
procedure Edit(const Item: TLVItem); override;
procedure EditCanceled(const Item: TLVItem); virtual;
{ Overriden ancestor methods }
procedure ColClick(Column: TListColumn); override;
{$IFDEF DFS_FIXED_LIST_VIEW}
procedure InsertItem(Item: TListItem); override;
{$ENDIF}
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure CreateWnd; override;
procedure DestroyWnd; override;
procedure ProcessDrawHeaderMsg(Index: Integer; Rect: TRect;
State: TOwnerDrawState; var DefaultDrawing: boolean); virtual;
procedure DrawHeader(var Canvas: TCanvas; Index: Integer; var Rect: TRect;
Selected: boolean; var DefaultDrawing: boolean); virtual;
procedure DefaultDrawHeader(var Canvas: TCanvas; Index: Integer;
var Rect: TRect; Selected: boolean); virtual;
procedure SetOnDrawHeader(Value: TLVHDrawItemEvent); virtual;
procedure SetColumnsOwnerDrawFlag(OwnerDrawn: boolean); virtual;
procedure CreateSortBmps(var UpBmp, DownBmp: TBitmap); virtual;
{$IFNDEF DFS_COMPILER_4_UP}
procedure GetImageIndex(Item: TListItem); virtual;
{$ENDIF}
{$IFDEF BACKGROUND_FIXED}
procedure BackgroundImageChanged(Sender: TObject); virtual;
{$ENDIF}
{ Property methods }
procedure SetAutoColumnSort(Value: TAutoColumnSort);
procedure SetAutoSortStyle(Value: TAutoSortStyle);
procedure SetCurrentSortAscending(Value: boolean);
procedure SetAutoSortAscending(Value: boolean);
procedure SetStyle(Value: TLVStyle);
procedure SetShowSortArrows(Value: boolean);
procedure SetReverseSortArrows(Value: boolean);
procedure SetLastColumnClicked(Value: integer);
procedure SetAutoResort(Value: boolean);
{$IFDEF BACKGROUND_FIXED}
procedure SetBackgroundImage(const Value: TBitmap);
{$ENDIF}
function GetSmallImages: {$IFDEF DFS_COMPILER_4_UP} TCustomImageList {$ELSE}
TImageList {$ENDIF};
procedure SetSmallImages(Val: {$IFDEF DFS_COMPILER_4_UP} TCustomImageList
{$ELSE} TImageList {$ENDIF});
function GetVersion: string; virtual;
procedure SetVersion(const Val: string);
function GetCurrentColumnWidth(Index: integer): integer;
procedure CreateParams(var Params: TCreateParams); override;
procedure Loaded; override;
{ Should probably remain protected }
property SortUpBmp: TBitmap
read FSortUpBmp;
property SortDownBmp: TBitmap
read FSortDownBmp;
{ Should be made public by descendants as needed }
property LastColumnClicked: Integer
read FLastColumnClicked
write SetLastColumnClicked;
{ Should be published by descendants as needed }
property HeaderHandle: HWnd
read FHeaderHandle;
property AutoColumnSort: TAutoColumnSort
read FAutoColumnSort
write SetAutoColumnSort
default acsNoSort;
property AutoSortStyle: TAutoSortStyle
read FAutoSortStyle
write SetAutoSortStyle
default assSmart;
property AutoResort: boolean
read FAutoResort
write SetAutoResort
default TRUE;
property AutoSortAscending: boolean
read FAutoSortAscending
write SetAutoSortAscending
default TRUE;
property ColumnSearch: boolean
read FColumnSearch
write FColumnSearch
default FALSE;
property ShowSortArrows: boolean
read FShowSortArrows
write SetShowSortArrows
default FALSE;
property ReverseSortArrows: boolean
read FReverseSortArrows
write SetReverseSortArrows
default FALSE;
property CurrentSortAscending: boolean
read FTmpAutoSortAscending
write SetCurrentSortAscending;
property SaveSettings: TdfsEnhLVSaveSettings
read FSaveSettings
write FSaveSettings;
property Style: TLVStyle
read FStyle
write SetStyle
default lvStandard;
property CurrentColumnWidth[Index: integer]: integer
read GetCurrentColumnWidth;
{$IFDEF BACKGROUND_FIXED}
property BackgroundImage: TBitmap
read FBackgroundImage
write SetBackgroundImage;
{$ENDIF}
property NoColumnResize: boolean
read FNoColumnResize
write FNoColumnResize;
// We have to redeclare this so we can hook into the read/write methods.
property SmallImages:
{$IFDEF DFS_COMPILER_4_UP} TCustomImageList {$ELSE} TImageList {$ENDIF}
read GetSmallImages
write SetSmallImages;
{ Events }
property OnDrawHeader: TLVHDrawItemEvent
read FOnDrawHeader
write SetOnDrawHeader;
property OnMeasureItem: TLVMeasureItemEvent
read FOnMeasureItem
write FOnMeasureItem;
property OnDrawItem: TLVDrawItemEvent
read FOnDrawItem
write FOnDrawItem;
property OnDrawSubItem: TLVDrawSubItemEvent
read FOnDrawSubItem
write FOnDrawSubItem;
property OnAfterDefaultDrawItem: TLVAfterDrawItemEvent
read FOnAfterDefaultDrawItem
write FOnAfterDefaultDrawItem;
property OnSortItems: TLVSortItemsEvent
read FOnSortItems
write FOnSortItems;
property OnSortBegin: TLVSortStatusEvent
read FOnSortBegin
write FOnSortBegin;
property OnSortFinished: TLVSortStatusEvent
read FOnSortFinished
write FOnSortFinished;
property OnEditCanceled: TLVEditCanceled
read FOnEditCanceled
write FOnEditCanceled;
{$IFNDEF DFS_COMPILER_4_UP}
property OnGetImageIndex: TLVNotifyEvent
read FOnGetImageIndex
write FOnGetImageIndex;
{$ENDIF}
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -