type
TSortListView =
class(TListView)
private
fCurrentCol : Integer;
procedure SetLVColumnShading(ColIdx: Integer);
published
procedure ColClick(Column: TListColumn);
override;
// Procedures for handling background-color of sorted column
function CustomDraw(
const ARect: TRect; Stage: TCustomDrawStage): Boolean;
override;
function CustomDrawItem(Item: TListItem; State: TCustomDrawState;
Stage: TCustomDrawStage): Boolean;
override;
function CustomDrawSubItem(Item: TListItem; SubItem: Integer;
State: TCustomDrawState; Stage: TCustomDrawStage): Boolean;
override;
end;
.....
procedure TSortListView.ColClick(Column: TListColumn);
begin
if Column.Tag = 1
then
Column.Tag := -1
else
Column.Tag := 1;
// Updates the index of the shaded column
fCurrentCol := Column.
Index;
inherited colclick(Column);
CustomSort(@CustomSortProc, Column.
index);
end;
// Displays shading in any area not occupied by list items
function TSortListView.CustomDraw(
const ARect: TRect; Stage: TCustomDrawStage): Boolean;
var
ColLeft: Integer;
// left edge of selected column
ColBounds: TRect;
// bounds of the selected column
I: Integer;
// loops thru columns
begin
// Calculate left side of selected column
ColLeft := ARect.Left;
for I := 0
to Pred(fCurrentCol)
do
ColLeft := ColLeft + ListView_GetColumnWidth(
Handle, I);
// Calculate bounding rectangle of selected column
ColBounds := Rect(
ColLeft,
ARect.Top,
ColLeft + ListView_GetColumnWidth(
Handle, fCurrentCol),
ARect.Bottom
);
// Shade the column
// other event handlers overwrite this where there are list
// items but this code ensures shading extends to bottom of
// list view client rectangle
Canvas.Brush.Color := clSilver;
Canvas.FillRect(ColBounds);
result :=
inherited CustomDraw(ARect, Stage);
end;
// Helper-procedure called from CustomDrawItem and CustomDrawSubItem
procedure TSortListView.SetLVColumnShading(ColIdx: Integer);
begin
if fCurrentCol = ColIdx
then
// given column is selected: shade it
Canvas.Brush.Color := clSilver
else
// given column not shaded: ensure correct background used
Canvas.Brush.Color := ColorToRGB(Color);
end;
// Shade the first column (index 0) if this is selected
function TSortListView.CustomDrawItem(Item: TListItem; State: TCustomDrawState;
Stage: TCustomDrawStage): Boolean;
begin
SetLVColumnShading(0);
result :=
inherited CustomDrawItem(Item, State, Stage);
end;
// Shade a sub item column if selected
function TSortListView.CustomDrawSubItem(Item: TListItem; SubItem: Integer;
State: TCustomDrawState; Stage: TCustomDrawStage): Boolean;
begin
if SubItem > 0
then // ensure not column 0 (Delphi 4)
SetLVColumnShading(SubItem);
result :=
inherited CustomDrawSubItem(Item, SubItem, State, Stage);
end;