Delphi 中根据鼠标位置获取网页元素
[ 2010-12-10 12:33 | by 草山湖 ]
根据鼠标位置获取网页元素,主要是对 IHTMLDocument2 方法 elementFromPoint 的使用
with TWebBrowserWithUI(Sender) do
begin
pt := Mouse.CursorPos;
pt := ScreenToClient(pt);
if PtInRect(Rect(Left, Top, Left + Width, Top + Height), pt) then
try
elem := nil;
elem := (Document as IHTMLDocument2).elementFromPoint(pt.X, pt.Y);
if (elem <> nil) and ((elem.tagName = 'A') or (elem.tagName = 'IMG'))
then
begin
url := elem.getAttribute('href', 0);
CreateSheet(url);
end;
except
on E: Exception do
ShowMessage(e.Message);
end;
end;
Cancel := True;
with TWebBrowserWithUI(Sender) do
begin
pt := Mouse.CursorPos;
pt := ScreenToClient(pt);
if PtInRect(Rect(Left, Top, Left + Width, Top + Height), pt) then
try
elem := nil;
elem := (Document as IHTMLDocument2).elementFromPoint(pt.X, pt.Y);
if (elem <> nil) and ((elem.tagName = 'A') or (elem.tagName = 'IMG'))
then
begin
url := elem.getAttribute('href', 0);
CreateSheet(url);
end;
except
on E: Exception do
ShowMessage(e.Message);
end;
end;
Cancel := True;