禁止WebBrowser关闭——怎样检测 window.close();?(200分)

  • 主题发起人 主题发起人 avant
  • 开始时间 开始时间
A

avant

Unregistered / Unconfirmed
GUEST, unregistred user!
许多页面最下方又这么一个按钮:
<center>
<form>
<input type="button" value="关闭本窗口" onClick="window.close();">
</form>
</center>
如果"确定",就把显示它的WebBrowser给释放掉了,露出后面的窗体,多不专业呀!
怎么能检测到这样的事件发生?好像没有onClose事件?只有
onbeforeunload Sets or retrieves a pointer to the event handler function associated with the event.
onblur Sets or retrieves a pointer to the event handler function associated with the event.
onerror Sets or retrieves a pointer to the event handler function associated with the event. An onerror event occurs when loading the object causes an error.
onfocus Sets or retrieves a pointer to the event handler function associated with the event.
onhelp Sets or retrieves a pointer to the event handler function associated with the event.
onload Sets or retrieves a pointer to the event handler function associated with the event. The onload event occurs immediately after the browser loads the object.
onresize Sets or retrieves a pointer to the event handler function associated with the event.
onscroll Sets or retrieves a pointer to the event handler function associated with the event.
onunload Sets or retrieves a pointer to the event handler function associated with the event.
即使是这些事件,onDocumentComplete时将其制定给一个返回boolean型的函数,也出现
"尚未完成"异常,立即执行该函数,确信此时webBrowser.busy=false,DocumentComplete,是
不是方法不对?
特向WebBrowser高手请教!
 
OnBeforeNavigat2

if pos('window.close(),url) > 0 then..........(只是说明)
 
不行的,点击按钮时根本没有触发这个事件,我刚时过.
 
用EmbeddedWB,里面有一个OnCloseQuery
 
我试验成功了,在EmbeddedWB中,似乎只有在OnTranslateURL中可以做到,反正我已经
搞定了。
function TForm1.Web0TranslateUrl(const dwTranslate: Cardinal;
const pchURLIn: PWideChar
var ppchURLOut: PWideChar): HRESULT;
var
DoC: IHTMLDocument2;
ActiveElement: IHTMLElement;
s, command: string;
begin
Doc := WebBrowser.Document as IHTMLDocument2;
if Doc <> nil then
begin
ActiveElement := Doc.Get_activeElement;
if ActiveElement <> nil then
begin
s := ActiveElement.Get_outerHTML;
command := 'javascript:window.close()';
if pos(command, s) <> 0 then
begin
if Application.MessageBox('确实要关闭页面吗?',
nil,
MB_ICONQUESTION + mb_yesno) = id_YES then
begin
CloseWindow
//自编过程
exit;
end
else
ppchURLOut := PWideChar(WebBrowser1.loCationURL);
end
end;
end;
end;
 
恕小弟笨拙,EmbeddedWB是什么?怎么用?MSDN里查不到!
 
CathyEagle大虾:
OnTranslateURL怎么声明?一般声明?怎么调用?望不吝赐教!谢谢
 
到这个网站http://www.intelligo.net/iedelphi/去找EmbeddedWB就行了。
 
谢谢.
还有没有别的办法?
 
暂时我还不知道。
 
先给分了!等着你更简单的方法!:)
 
放一个ApplicationEvents1

procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
begin
if (Msg.message = WM_CLOSE) and (Msg.hwnd = WebBrowser1.Handle) then
PeekMessage(Msg,Msg.Hwnd,0,0,PM_REMOVE)
else
inherited;
end;
 
unreal:你的方法非常好,谢谢!D5里的ApplicationEvents确实不错.
 
呵呵,开了个空头支票给我
 
unreal:到<a href="DispQ.asp?LID=331746">这里</a>领分.那个问题没人理我,不过我已经不需要了,那100分
不成敬意.不过你的方法有时候(窗体较多时)居然不灵,还是释放掉了,奇怪!我再试试.
 
试试这个。
procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
var
i : Integer;
begin
if (Msg.message = WM_CLOSE) then
begin
for i := 0 to Form1.ComponentCount -1 do
begin
if (Form1.Components is TWebBrowser) and
(Msg.hwnd = (Form1.Components as TWebBrowser).Handle)
then
begin
Handled := True;
Break;
end;
end;
end;
end;
 

Similar threads

I
回复
0
查看
499
import
I
I
回复
0
查看
641
import
I
I
回复
0
查看
3K
import
I
I
回复
0
查看
3K
import
I
I
回复
0
查看
3K
import
I
后退
顶部