Hallo,
Ersetze HDI_IMAGE einfach durch HDF_BITMAP.
Ich habe den Code - vielleicht auch interessant für andere - noch etwas erweitert.
Die SortDirection habe ich jedoch nicht vollständig implementiert.
Auch sollte der Code in eine eigene Komponente gepackt werden.
Delphi-Quellcode:
private
{ Private declarations }
FSortUpGlyph: TBitmap;
FSortDownGlyph: TBitmap;
procedure SetColumnImage(List: TListView; Column, Image: Integer; ShowImage: Boolean);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
CommCtrl;
type
TSortDirection = (sdAscending, sdDescending);
procedure TForm1.SetColumnImage(List: TListView; Column, Image: Integer;
ShowImage: Boolean);
var
Align,hHeader: integer;
HD: HD_ITEM;
begin
hHeader := SendMessage(List.Handle, LVM_GETHEADER, 0, 0);
with HD do
begin
FillChar(HD, SizeOf(HD), 0);
Mask := (HDI_BITMAP) or (HDI_FORMAT);
pszText := PChar(List.Columns[Column].Caption);
case List.Columns[Column].Alignment of
taLeftJustify: Align := HDF_LEFT;
taCenter: Align := HDF_CENTER;
taRightJustify: Align := HDF_RIGHT;
else
Align := HDF_LEFT;
end;
if SortDirection = sdAscending then
Hbm := SortUpGlyph.Handle
else
Hbm := SortDownGlyph.Handle;
if ShowImage then
fmt := HDF_STRING or HDF_BITMAP or HDF_BITMAP_ON_RIGHT
else
fmt := HDF_STRING or Align;
iImage := Image;
end;
SendMessage(hHeader, HDM_SETITEM, Column, Integer(@HD));
end;
function CustomSortProc(Item1, Item2: TListItem; ParamSort: integer): integer; stdcall;
begin
Result := -lstrcmp(PChar(TListItem(Item1).Caption),
PChar(TListItem(Item2).Caption));
end;
procedure TForm1.ListView1ColumnClick(Sender: TObject;
Column: TListColumn);
var
i : integer;
begin
ListView1.CustomSort( @CustomSortProc, Column.Index );
// This loop displays the icon in the selected column.
for i := 0 to ListView1.Columns.Count - 1 do
SetColumnImage(ListView1, i, 0, i = Column.Index);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FSortUpGlyph := TBitmap.Create;
FSortDownGlyph := TBitmap.Create;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FSortUpGlyph.Free;
FSortDownGlyph.Free;
end;