Link lable in Listview (Delphi)
how can i have a listview that its items contain links(d开发者_如何学编程irect us to html pages) ?
Thank you
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.
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;
精彩评论