源代码如下,Delphi6+Win98上通过完全达到你的要求。
稍稍改一下就可以做成控件。
类似的控件在网上还有许多,这只是一个例子,用标准控件做的(ListView+ImageList)
Pas文件:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, ImgList, ExtCtrls;
type
TForm1 = class(TForm)
ListView1: TListView;
Button1: TButton;
ImageList1: TImageList;
procedure Button1Click(Sender: TObject);
procedure ListView1DblClick(Sender: TObject);
procedure ListView1ColumnClick(Sender: TObject; Column: TListColumn);
private
{ Private declarations }
public
{ Public declarations }
end;
function CustomSortProc( Item1, Item2 : TListItem; lParam : LongInt ) : Integer; stdcall;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
List: TListItem;
I: Integer;
begin
ListView1.Items.Clear;
Randomize;
For i:=0 to 10 do
begin
List:=ListView1.Items.Add;
List.Caption:=IntToStr(Random(9));
List.SubItems.Add(IntToStr(Random(9)));
List.SubItems.Add(IntToStr(Random(9)));
List.SubItems.Add(IntToStr(Random(9)));
List.SubItems.Add(IntToStr(Random(9)));
List.ImageIndex:=-1;
List.Indent:=-1;
end;
end;
procedure TForm1.ListView1DblClick(Sender: TObject);
var
MousePosScreen,MousePosClient: TPoint;
//光标的屏幕坐标、光标的在LISTVIE上的坐标
i,j: integer;
CurrColumn: Integer;//被点击的Column
Find: Boolean;
begin
If ListView1.Selected<>nil then
begin
GetCursorPos(MousePosScreen);;
ListView1.GetItemAt(MousePosScreen.X,MousePosScreen.Y);
MousePosClient:=ListView1.ScreenToClient(MousePosScreen);
j:=ListView1.Column[0].Width;
Find:=False;
CurrColumn:=0;
i:=0;
while (not Find) and (i<ListView1.Columns.Count) do
begin
If j>=MousePosClient.X then
begin
Find:=True;
CurrColumn:=i+1;
end;
inc(i);
j:=j+ListView1.Column[i-1].Width;
end;
if Find then
ShowMessage(Format('第 %d 列',[CurrColumn]));
end;
end;
function CustomSortProc( Item1, Item2 : TListItem; lParam : LongInt ) : Integer; stdcall;
var
i: integer;
begin
if lParam >= 0 then //lParam中保存的是SubItem的Index
begin
If lParam=0 then
i := lstrcmp(PChar(Item1.Caption),PChar(Item2.Caption))
else
i := lstrcmp(PChar(Item1.SubItems.Strings[lParam-1]),PChar(Item2.SubItems.Strings[lParam-1]));
if Form1.ListView1.Column[lParam].Tag=0 then
result := -i
else
result := i;
end
else
result := 0;
end;
procedure TForm1.ListView1ColumnClick(Sender: TObject;
Column: TListColumn);
var
I: integer;
begin
ListView1.CustomSort(@CustomSortProc, Column.Index);
If Column.Tag=0 then
Column.Tag:=1
else
Column.Tag:=0;
For i:=0 to ListView1.Columns.Count-1 do
ListView1.Column.ImageIndex:=-1;
Column.ImageIndex:=Column.Tag;
end;
end.
DFM文件:
object Form1: TForm1
Left = 195
Top = 103
Width = 544
Height = 380
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object ListView1: TListView
Left = 88
Top = 160
Width = 377
Height = 177
Columns = <
item
Caption = 'asasdf'
ImageIndex = 0
Width = 70
end
item
Width = 70
end
item
Width = 70
end
item
Width = 70
end
item
Width = 70
end>
Items.Data = {
4200000002000000FFFFFFFFFFFFFFFFFFFFFFFF000000000000000008617364
6661736466FFFFFFFFFFFFFFFFFFFFFFFF000000000000000008617364667364
6166}
RowSelect = True
SmallImages = ImageList1
SortType = stText
TabOrder = 0
ViewStyle = vsReport
OnColumnClick = ListView1ColumnClick
OnDblClick = ListView1DblClick
end
object Button1: TButton
Left = 416
Top = 72
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 1
OnClick = Button1Click
end
object ImageList1: TImageList
Left = 416
Top = 200
Bitmap = {
494C010102000400040010001000FFFFFFFFFF00FFFFFFFFFFFFFFFF424D3600
0000000000003600000028000000400000001000000001001000000000000008
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000007B6F7B6F7B6F7B6F7B6F7B6F7B6F
7B6F7B6F7B6F7B6F7B6F7B6F7B6F7B6F7B6F7B6F7B6F7B6F7B6F7B6F7B6F7B6F
7B6F7B6F7B6F7B6F7B6F7B6F7B6F7B6F7B6F0000000000000000E07F00000000
000000000000000000000000000000000000000000000000000000000000E07F
0000000000000000000000000000000000000000F75EF75EF75EFF7F7B6F7B6F
7B6F7B6F7B6F7B6F7B6F7B6F7B6F7B6F7B6F0000F75EF75EF75EFF7F7B6F7B6F
7B6F7B6F7B6F7B6F7B6F7B6F7B6F7B6F7B6F0000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000FF7F
000000000000000000000000000000000000F75EF75EF75EF75EF75EF75EF75E
F75EFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FF75EF75EF75EF75EF75EF75EF75E
F75EFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7F00000000000000000000FF03FF03
FF03FF03FF03FF0300000000000000000000000000000000000000000000E07F
000000000000000000000000000000000000FF7FFF7FFF7FFF7FFF7FFF7FFF7F
FF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7F
FF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7F0000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000FF7F
0000FF7F00000000000000000000000000000000F75EF75EF75EFF7FFF7FFF7F
FF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7F0000F75EF75EF75EFF7FFF7FFF7F
FF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7F000000000000000000000000E07F
FF7FE07FFF7FE07F00000000000000000000000000000000000000000000E07F
0000E07F0000E07F00000000000000000000F75EF75EF75EF75EF75EF75EF75E
F75EFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FF75EF75EF75EF75EF75EF75EF75E
F75EFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7F00000000000000000000E07FFF7F
E07FFF7FE07FFF7FE07F0000000000000000000000000000000000000000FF7F
0000FF7F0000FF7F00000000000000000000F75EF75EF75EF75EF75EF75EF75E
F75EF75EF75EF75EF75EF75EF75EF75EF75EF75EF75EF75EF75EF75EF75EF75E
F75EF75EF75EF75EF75EF75EF75EF75EF75E00000000000000000000FF7FE07F
FF7FE07FFF7F0000FF7F00000000000000000000000000000000E07F0000E07F
FF7FE07FFF7FE07F00000000000000000000F75EF75EF75EF75EF75EF75EF75E
F75EF75EF75EF75EF75EF75EF75EF75EF75EF75EF75EF75EF75EF75EF75EF75E
F75EF75EF75EF75EF75EF75EF75EF75EF75E00000000000000000000E07FFF7F
E07FFF7FE07F0000E07F00000000000000000000000000000000FF7F0000FF7F
E07FFF7FE07FFF7F00000000000000000000F75EF75EF75EF75EF75EF75EF75E
F75EF75EF75EF75EF75EF75EF75EF75EF75EF75EF75EF75EF75EF75EF75EF75E
F75EF75EF75EF75EF75EF75EF75EF75EF75E00000000000000000000FF7F0000
FF7F0000FF7F0000000000000000000000000000000000000000E07FFF7FE07F
FF7FE07FFF7FE07F00000000000000000000F75EF75EF75EF75EF75EF75EF75E
F75EF75EF75EF75EF75EF75EF75EF75EF75EF75EF75EF75EF75EF75EF75EF75E
F75EF75EF75EF75EF75EF75EF75EF75EF75E00000000000000000000E07F0000
E07F0000E07F00000000000000000000000000000000000000000000E07FFF7F
E07FFF7FE07F000000000000000000000000F75EF75EF75EF75EF75EF75EF75E
F75EF75EF75EF75EF75EF75EF75EF75EF75EF75EF75EF75EF75EF75EF75EF75E
F75EF75EF75EF75EF75EF75EF75EF75EF75E0000000000000000000000000000
FF7F0000FF7F0000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000F75EF75EF75EF75EF75EF75EF75E
F75EFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FF75EF75EF75EF75EF75EF75EF75E
F75EFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7F0000000000000000000000000000
00000000E07F00000000000000000000000000000000000000000000FF03FF03
FF03FF03FF03FF0300000000000000000000FF7FFF7FFF7FFF7FFF7FFF7FFF7F
FF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7F
FF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7F0000000000000000000000000000
00000000FF7F0000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000FF7FFF7FFF7FFF7FFF7FFF7FFF7F
FF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7F
FF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7F0000000000000000000000000000
00000000E07F0000000000000000000000000000000000000000000000000000
0000000000000000E07F00000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000007B6F7B6F7B6F7B6F7B6F7B6F
7B6F7B6F7B6F7B6F7B6F7B6F7B6F7B6F7B6F7B6F7B6F7B6F7B6F7B6F7B6F7B6F
7B6F7B6F7B6F7B6F7B6F7B6F7B6F7B6F7B6F424D3E000000000000003E000000
2800000040000000100000000100010000000000800000000000000000000000
000000000000000000000000FFFFFF00E00FFDFFFFFFFFFFE00FF8FFFFFFFFFF
E00FF8FF00000000F00FF87F00000000F00FF81FFFFFFFFFF80FF80FFFFFFFFF
F007F00F0000E0FFF007E00F00000000F007E00FFFFFFFFFF00FE00FFFFFFFFF
F01FF01F00000000F81FF00F00000000FE1FF00FFFFFFFFFFF1FF007561F3800
FF1FF007107B2000FFBFF00700000000}
end
end