给TWebBrowser添加鼠标事件
学习用IsChild(WebBrowser1.Handle, Msg.Hwnd)判断鼠标事件是否来自TWebBrowser
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleCtrls, SHDocVw, StdCtrls, MSHTML;
type
TForm1 = class(TForm)
WebBrowser1: TWebBrowser;
procedure FormCreate(Sender: TObject);
procedure WebBrowser1DocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
private
{ Private declarations }
FLoaded: Boolean;
procedure AppMsg(var Msg: TagMsg; var Handled: Boolean);
procedure GetContronl;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.AppMsg(var Msg: TagMsg; var Handled: Boolean);
var
mPoint : TPoint;
begin
if IsChild(WebBrowser1.Handle, Msg.Hwnd) and
(Msg.Message = WM_MOUSEMOVE) and FLoaded then
begin
GetCursorPos(mPoint);
GetContronl;
//PopupMenu1.Popup(mPoint.X, mPoint.Y);
Handled:=True;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FLoaded := False;
WebBrowser1.Navigate( 'http://www.kitop.com ');
Application.OnMessage := AppMsg;
end;
procedure TForm1.GetContronl;
var
E: IHTMLElement;
tmpStr: String;
myPoint: TPoint;
begin
myPoint := Mouse.CursorPos;
myPoint := WebBrowser1.ScreenToClient(myPoint);
{
myPoint.X := myPoint.X + 100;
myPoint.Y := myPoint.Y + 100;
}
E := (WebBrowser1.Document as IHTMLDocument2).elementFromPoint(myPoint.X, myPoint.Y);
//E := (WebBrowser1.Document as IHTMLDocument2).elementFromPoint(100, 100);
//ShowMessage(E.title);
tmpStr := Format( '%s,%s,%s,%s;X:%d;Y:%d ',[E.className,E.id,E.tagName,E.innerHTML,
myPoint.X,myPoint.Y]);
Caption := tmpStr;
end;
procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
begin
FLoaded := True;
end;
end.
posted on 2010-12-04 21:56 Delphi7456 阅读(2355) 评论(0) 编辑 收藏 举报