列表视图中的链接标签 (Delphi)

发布于 2024-10-15 01:55:06 字数 53 浏览 8 评论 0原文

我怎样才能有一个列表视图,它的项目包含链接(引导我们到html页面)?

谢谢

how can i have a listview that its items contain links(direct us to html pages) ?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔戏命师 2024-10-22 01:55:06

使用支持此现成的列表视图或网格(tms 软件例如具有支持“迷你”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 中的链接标题的颜色取决于您的选择。如果您要遵守长期坚持的互联网标准,您可以将它们设为蓝色并加下划线。

Either use a listview or grid which supports this off-the-shelf (tms software for example has components that support a "mini" html) or with a standard TListView do something like:

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;

It's up to you how you want your color the link captions in your ListView. If you were to adhere to the long-held Internet standards you would make them blue and underlined.

紙鸢 2024-10-22 01:55:06

嗯,是的,使用 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;

Um, yes, it is easy to go ahead and call the default browser with Delphi. Here's a basic example with validation (so you can have non-URL values in your list):

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文