//有一点说错,第一列图标也可以放在右边
//下面是我改自深度历险的例子
unit IconList;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, Commctrl;
type
TForm1 = class(TForm)
ListView1: TListView;
procedure ListView1ColumnClick(Sender: TObject; Column: TListColumn);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
ArrowDown, NoArrow, ArrowUp: HBitmap;
FBitmap: TBitmap;
FAtoZ: Boolean;
OldCol: Byte;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
{$R Arrows.res}
var AtoZOrder: Boolean;
procedure TForm1.ListView1ColumnClick(Sender: TObject;
Column: TListColumn);
function CustomSortProc(Item1, Item2: TListItem; ParamSort: Integer): Integer; stdcall;
begin
case ParamSort of //Column
0://Sort Main column.
if AtoZOrder then
Result:=lstrcmp(PChar(TListItem(Item1).Caption), PChar(TListItem(Item2).Caption))
else
Result:=-lstrcmp(PChar(TListItem(Item1).Caption), PChar(TListItem(Item2).Caption));
else //Sort one of the subcolumns (subitems).
if AtoZOrder then
Result:=lstrcmp(PChar(TListItem(Item1).SubItems[ParamSort-1]), PChar(TListItem(Item2).SubItems[ParamSort-1]))
else
Result:=-lstrcmp(PChar(TListItem(Item1).SubItems[ParamSort-1]), PChar(TListItem(Item2).SubItems[ParamSort-1]));
end;
end;
var hditem:THdItem;
I: Byte;
begin
if OldCol=Column.Index then
FAtoZ:=not FAtoZ
else
OldCol:=Column.Index;
AtoZOrder:=FAtoZ;
ListView1.CustomSort(@CustomSortProc, Column.Index);
for I:=0 to ListView1.Columns.Count-1 do
begin
FBitmap.Releasehandle;
if I=Column.Index then
begin
if AtoZOrder then
FBitmap.Handle:=ArrowDown
else
FBitmap.Handle:=ArrowUp
end
else
FBitmap.Handle:=NoArrow;
hditem.Mask := HDI_FORMAT;
Header_GetItem(GetDlgItem(ListView1.Handle,0),I,hditem);
hditem.Mask:=HDI_BITMAP or HDI_FORMAT;
hditem.fmt:=hditem.fmt or HDF_BITMAP or HDF_BITMAP_ON_RIGHT;//
//hdf_bitmap_on_right表示图标在右
hditem.hbm:=FBitmap.Handle;
Header_SetItem(GetDlgItem(ListView1.Handle,0),I,hditem);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ArrowUp:=LoadBitmap(hInstance, 'ARROWUP');
ArrowDown:=LoadBitmap(hInstance, 'ARROWDOWN');
NoArrow:=LoadBitmap(hInstance, 'NOARROW');
FBitmap:=TBitmap.Create;
ListView1ColumnClick(Sender,ListView1.Columns[0]);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
FBitmap.ReleaseHandle;
FBitmap.Free;
DeleteObject(ArrowUp);
DeleteObject(ArrowDown);
DeleteObject(NoArrow);
end;
end.
//dfm文件
object Form1: TForm1
Left = 92
Top = 164
Width = 472
Height = 314
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = True
OnClose = FormClose
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object ListView1: TListView
Left = 4
Top = 14
Width = 410
Height = 109
Columns = <
item
Caption = 'AAA'
Width = 70
end
item
Caption = 'BBB'
Width = 100
end
item
Caption = 'CCC'
Width = 70
end
item
Caption = 'DDD'
Width = 150
end>
Items.Data = {
E40000000500000000000000FFFFFFFFFFFFFFFF030000000000000005537461
6D330365656503747474036D6D6D00000000FFFFFFFFFFFFFFFF030000000000
0000055374616D3103616161036262620363636300000000FFFFFFFFFFFFFFFF
0300000000000000055374616D35036F6F6F03707070037A7A7A00000000FFFF
FFFFFFFFFFFF0300000000000000055374616D32036A6A6A0372727203757575
00000000FFFFFFFFFFFFFFFF0300000000000000055374616D3403717171036E
6E6E03626262FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
FFFFFFFF}
TabOrder = 0
ViewStyle = vsReport
OnColumnClick = ListView1ColumnClick
end
end