listview上加图标?(50分)

  • 主题发起人 主题发起人 aqudc
  • 开始时间 开始时间
A

aqudc

Unregistered / Unconfirmed
GUEST, unregistred user!
如何像Foxmail那样在ListView顶部的标签上左边显示汉字右边显示图标?
 
指定一个TListItem 的ImageIndex就可以了。
 
listview有lageimages,smallimages,stateimages
设置好后再指定每个item 的imageindex.
 
use a ImageList then add image.
 
补充一点不同的ViewStyle指定的Images不同,如vsReport方式用Smallimages
vsIcon方式用LargeImages等等。
 
<font color=red>"在ListView顶部的标签上"</font>
看清楚,人家问的是ListView的header,不是item
 
不过普通的设置方法图标是显示在左边,如何才能显示在右边呢?
 
他们说的得用delphi5才行,delphi4上必须用别的方法,
delphi深度历险上有一个例子,可用在delphi4上,叫icon??.zip
 
如果是要显示在Column Header上,好像除了用
专门的控件之外,最省事的办法就是自绘了。
程序员大本营里有这样的控件源代码。自己啃
啃吧。
 
delphi自己可以在header上显示图标,因为header的每个colmun也是有
imageindex属性的,但是只能显示在左边
 
to cakk:
listview的column只有第一个column的图标只能显示在左面,其余都可显示在右
 
to hubdog:
除第一个column外,其余的如何将图标显示在右面



 
//有一点说错,第一列图标也可以放在右边
//下面是我改自深度历险的例子
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
 
接受答案了.
 
后退
顶部