Listview中的链接标签(Delphi)

时间:2011-01-29 16:47:59

标签: delphi listview hyperlink linklabel

我怎样才能让listview中的项目包含链接(将我们引导到html页面)?

谢谢

2 个答案:

答案 0 :(得分:6)

要么使用支持这种现成的列表视图或网格(例如tms software具有支持“迷你”html的组件),要么使用标准TListView执行以下操作:

type
  TLinkItem = class(TObject)
  private
    FCaption: string;
    FURL: string;
  public
    constructor Create(const aCaption, aURL: string);
    property Caption: string read FCaption write FCaption;
    property URL: string read FURL write FURL;
  end;

constructor TLinkItem.Create(const aCaption, aURL: string);
begin
  FCaption := aCaption;
  FURL := aURL;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  Item: TListItem;
  i: Integer;
begin
  FLinkItems := TObjectList.Create({AOwnsObjects=}True);
  FLinkItems.Add(TLinkItem.Create('StackOverflow', 'http://www.stackoverflow.com'));
  FLinkItems.Add(TLinkItem.Create('BJM Software', 'http://www.bjmsoftware.com'));

  for i := 0 to FLinkItems.Count - 1 do
  begin
    Item := ListView1.Items.Add;
    Item.Caption := TLinkItem(FLinkItems[i]).Caption;
    Item.Data := Pointer(FLinkItems[i]);
  end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FreeAndNil(FLinkItems);
end;

procedure TForm1.ListView1Click(Sender: TObject);
var
  LinkItem: TLinkItem;
  URL: string;
begin
  LinkItem := TLinkItem(ListView1.Items[ListView1.ItemIndex].Data);
  URL := LinkItem.URL;
  ShellExecute(Handle, 'open', PChar(URL), nil, nil, SW_SHOW);
end;

由您如何将您的颜色设置为ListView中的链接标题。如果你坚持长期持有的互联网标准,你会把它们变成蓝色并加下划线。

答案 1 :(得分:2)

嗯,是的,很容易继续使用Delphi调用默认浏览器。这是验证的基本示例(因此您可以在列表中包含非URL值):

uses ShLwApi, ShellApi;

procedure TForm1.ListView1DblClick(Sender: TObject);
begin
  if PathIsURL(PChar(ListView1.Selected.Caption)) then
  begin
    ShellExecute(self.WindowHandle, 'open', PChar(ListView1.Selected.Caption),
      nil, nil, SW_SHOWNORMAL);
  end;
end;