function TForm1.GetDocInterface(hwnd:THandle):IHtmlDocument2; var hInst: THandle; hr:HResult; lRes:Cardinal; MSG: Integer; spDisp:IDispatch; spDoc:IHTMLDocument; pDoc2:IHTMLDocument2; spWin:IHTMLWindow2; ObjectFromLresult: TObjectFromLresult; begin hInst := LoadLibrary('Oleacc.dll'); if hInst=0 then exit; @ObjectFromLresult := GetProcAddress(hInst, 'ObjectFromLresult'); if @ObjectFromLresult <> nil then begin try MSG := RegisterWindowMessage('WM_HTML_GETOBJECT'); SendMessageTimeOut(Hwnd, MSG, 0, 0, SMTO_ABORTIFHUNG, 1000, lRes); hr:= ObjectFromLresult(lRes, IHTMLDocument2, 0, spDoc); if SUCCEEDED(hr) then begin spDisp:=spDoc.Script; spDisp.QueryInterface(IHTMLWindow2,spWin); //spWin:=IHTMLWindow2(spDisp); result:=spWin.document; end; finally FreeLibrary(hInst); end; end; end;procedure TForm1.GetPassword(pdoc2:IHTMLDocument2;pt:TPoint); var ltype:string; pwd:string; pElement:IHTMLElement; pPwdElement:IHTMLInputTextElement; hr:HRESULT; begin if (pDoc2=Nil) then exit; pElement:=pDoc2.elementFromPoint(pt.X,pt.Y); hr:=pElement.QueryInterface(IID_IHTMLInputTextElement,pPwdElement); if(SUCCEEDED(hr)) then begin if (pPwdElement.type_='password') and (pPwdElement.value<>'') then begin Edit1.text:=pPwdElement.value; end else if (pPwdElement.type_='text') and (pPwdElement.value<>'') then Edit2.Text:=pPwdElement.value; end;end;procedure TForm1.Timer1Timer(Sender: TObject); var pt:TPoint; handle:Thandle; buffer:PChar; strbuffer:string; begin //GetCursorPos(pt); pt:=GetPoint; handle:=WindowFromPoint(pt); if handle<>0 then begin GetClassName(handle,buffer,100); strbuffer:=strpas(buffer); if strbuffer='Internet Explorer_Server' then begin // pt:=ScreenToClient(pt); Windows.ScreenToClient(handle,pt); Edit3.Text:=IntToStr(pt.x)+' '+IntToStr(pt.Y); GetPassword(GetDocInterface(handle),pt); end; end; end; TObjectFromLResult :声明 type TObjectFromLResult = function(LRESULT: lResult; const IID: TIID; WPARAM: wParam; out pObject): HRESULT; stdcall;TIID这个报错不知道什么原因
5楼说的是不是这段代码? Hwnd := FindWindow(PChar('#32770'), PChar('Windows Internet Explorer')); if Hwnd <> 0 then SendMessage(Hwnd, WM_CLOSE, 0, 0);可是这段代码具体是干么的放在什么地方呢? #32770和Windows Internet Explorer是什么意思呢?
那段代码一般放在Timer控件里,检测到就关闭它
就是查找窗口标题为"Windows Internet Explorer",类名为#32770的窗口放到Timer里不行,弹出提示窗口的时候,主进程就挂起了,timer就不能运行了Type TFindDialogThread = class(TTHread) private procedure Execute: override; end;procedure TFindDialogThread.Execute; begin inherited; while not terminated do begin Hwnd := FindWindow(PChar('#32770'), PChar('Windows Internet Explorer')); if Hwnd <> 0 then SendMessage(Hwnd, WM_CLOSE, 0, 0); Sleep(1000); end; end; 调用var FindDialogThread: TFIndDialogThread; begin FindDialogThread := TFindDialogThread.Create(False); end; 没在Delphi里验证,你自己测试一下
var
hInst: THandle;
hr:HResult;
lRes:Cardinal;
MSG: Integer;
spDisp:IDispatch;
spDoc:IHTMLDocument;
pDoc2:IHTMLDocument2;
spWin:IHTMLWindow2;
ObjectFromLresult: TObjectFromLresult;
begin
hInst := LoadLibrary('Oleacc.dll');
if hInst=0 then exit;
@ObjectFromLresult := GetProcAddress(hInst, 'ObjectFromLresult');
if @ObjectFromLresult <> nil then begin
try
MSG := RegisterWindowMessage('WM_HTML_GETOBJECT');
SendMessageTimeOut(Hwnd, MSG, 0, 0, SMTO_ABORTIFHUNG, 1000, lRes);
hr:= ObjectFromLresult(lRes, IHTMLDocument2, 0, spDoc);
if SUCCEEDED(hr) then
begin
spDisp:=spDoc.Script;
spDisp.QueryInterface(IHTMLWindow2,spWin);
//spWin:=IHTMLWindow2(spDisp);
result:=spWin.document;
end;
finally
FreeLibrary(hInst);
end;
end;
end;procedure TForm1.GetPassword(pdoc2:IHTMLDocument2;pt:TPoint);
var
ltype:string;
pwd:string;
pElement:IHTMLElement;
pPwdElement:IHTMLInputTextElement;
hr:HRESULT;
begin
if (pDoc2=Nil) then exit;
pElement:=pDoc2.elementFromPoint(pt.X,pt.Y);
hr:=pElement.QueryInterface(IID_IHTMLInputTextElement,pPwdElement);
if(SUCCEEDED(hr)) then
begin
if (pPwdElement.type_='password') and (pPwdElement.value<>'') then
begin
Edit1.text:=pPwdElement.value;
end else
if (pPwdElement.type_='text') and (pPwdElement.value<>'') then
Edit2.Text:=pPwdElement.value;
end;end;procedure TForm1.Timer1Timer(Sender: TObject);
var
pt:TPoint;
handle:Thandle;
buffer:PChar;
strbuffer:string;
begin
//GetCursorPos(pt);
pt:=GetPoint;
handle:=WindowFromPoint(pt);
if handle<>0 then
begin
GetClassName(handle,buffer,100);
strbuffer:=strpas(buffer);
if strbuffer='Internet Explorer_Server' then
begin
// pt:=ScreenToClient(pt);
Windows.ScreenToClient(handle,pt);
Edit3.Text:=IntToStr(pt.x)+' '+IntToStr(pt.Y);
GetPassword(GetDocInterface(handle),pt);
end;
end;
end; TObjectFromLResult :声明
type
TObjectFromLResult = function(LRESULT: lResult; const IID: TIID; WPARAM: wParam; out pObject): HRESULT; stdcall;TIID这个报错不知道什么原因
据说要放在线程里,嘿嘿,要不对话框把主窗口卡死了
Hwnd := FindWindow(PChar('#32770'), PChar('Windows Internet Explorer'));
if Hwnd <> 0 then SendMessage(Hwnd, WM_CLOSE, 0, 0);可是这段代码具体是干么的放在什么地方呢?
#32770和Windows Internet Explorer是什么意思呢?
就是查找窗口标题为"Windows Internet Explorer",类名为#32770的窗口放到Timer里不行,弹出提示窗口的时候,主进程就挂起了,timer就不能运行了Type
TFindDialogThread = class(TTHread)
private
procedure Execute: override;
end;procedure TFindDialogThread.Execute;
begin
inherited;
while not terminated do
begin
Hwnd := FindWindow(PChar('#32770'), PChar('Windows Internet Explorer'));
if Hwnd <> 0 then SendMessage(Hwnd, WM_CLOSE, 0, 0);
Sleep(1000);
end;
end;
调用var
FindDialogThread: TFIndDialogThread;
begin
FindDialogThread := TFindDialogThread.Create(False);
end;
没在Delphi里验证,你自己测试一下
我一直是放在Timer里面的,可是不行
{
IHTMLWindow2 win = (IHTMLWindow2)webBrowser1.Document.Window.DomWindow;
string s = "window.alert = null;\r\nwindow.confirm = null;\r\nwindow.open = null;\r\nwindow.showModalDialog = null;";
win.execScript(s, "javascript");
}
方法参见http://www.delphidabbler.com/articles?article=18&part=1