当我在我的应用程序上使用Twebbrowser
时,我遇到一个非常烦人的问题,在网上冲浪几分钟后,弹出一些广告,当我试图离开它们时,它向我显示了这条消息:
您确定要离开此页吗?
问题是:
如何阻止Twebbrowser
显示此类对话框?我试图设置webbrowser.silent := true;
,它只是停止了恼人的javascripts错误。
我希望在没有任何弹出窗口和提醒的情况下,以twebbrowser
静默上网。
如果您有解决方案,请添加一个来源示例,因为我不太了解"不够专业"。
你认为安装另一个webbrowser组件会解决这个问题吗?如果它,请告诉我一个webbrowser组件,因为我搜索了,我没有找到。
答案 0 :(得分:1)
WebBrowser
控件本身不提供阻止这些对话框的可能性,因此Silent
无效。我现在无法提供引用,但我确定我已经从某个地方的MSDN读过它。
幸运的是,您可以观察Windows消息并根据需要阻止这些对话框。以下是implements all you need as a VCL的着名组件TEmbeddedWB
。如果您认为它太大,您可以将相关代码提取到项目中。
procedure TEmbeddedWB.FormWndProc(var AMsg: Messages.TMessage);
begin
if AMsg.Msg = WM_ACTIVATE then
begin
HandleDialogBoxes(AMsg);
end;
FOldWindowProc(AMsg);
end;
procedure TEmbeddedWB.HandleDialogBoxes(var AMsg: Messages.TMessage);
var
PopHandle: Integer;
DlgCaption, DlgClss: string;
Msg: TWMActivate;
WI: TWindowInfo;
begin
Msg := TWMActivate(AMsg);
if Msg.Active = 0 then
begin
PopHandle := Msg.ActiveWindow;
FillChar(WI, SizeOf(WI), 0);
if PopHandle <> 0 then
begin
WI.dwStyle := Abs(GetWindowLong(PopHandle, GWL_STYLE));
WI.dwExStyle := Abs(GetWindowLong(PopHandle, GWL_EXSTYLE));
end;
DlgClss := GetWinClass(PopHandle);
if (DlgClss = 'Internet Explorer_TridentDlgFrame') or ((DlgClss = '#32770') and
((GetWinClass(Windows.GetParent(PopHandle)) <> 'TApplication') and
(FindControl(Windows.GetParent(PopHandle)) = nil))) then
begin
DlgCaption := GetWinText(PopHandle);
if (PopHandle <> 0) and Assigned(FOnShowDialog) then
FOnShowDialog(Self, PopHandle, WI.dwExStyle, DlgCaption, FDialogBoxes.FNewCaption, FDialogBoxes.FDisableAll);
if FDialogBoxes.FDisableAll then
SendMessage(PopHandle, WM_CLOSE, 0, 0);
if FDialogBoxes.FReplaceIcon then
SendMessage(PopHandle, WM_SETICON, ICON_SMALL, Forms.Application.Icon.Handle);
if FDialogBoxes.FReplaceCaption then
begin
DlgCaption := StringReplace(DlgCaption, 'Microsoft ', '', []);
DlgCaption := StringReplace(DlgCaption, 'Internet Explorer', FDialogBoxes.FNewCaption, []);
SetWindowText(PopHandle, PChar(DlgCaption));
end;
if FDisableErrors.FScriptErrorsSuppressed then
begin
if (AnsiPos('SCRIPT', AnsiUpperCase(DlgCaption)) <> 0) then
begin
PostMessage(PopHandle, WM_LBUTTONDOWN, 0, 0);
PostMessage(PopHandle, WM_LBUTTONUP, 0, 0);
SendMessage(PopHandle, WM_CLOSE, 0, 0);
Forms.Application.ProcessMessages;
Exit;
end;
if (AnsiPos('ERROR', AnsiUpperCase(DlgCaption)) <> 0) or (WI.dwExStyle = 4260097) then
begin
DestroyWindow(PopHandle);
Exit;
end;
end;
if FPrintOptions.FEnabled then
begin
bPrintOptionsEnable := True;
if bInvokingPageSetup then
begin
bInvokingPageSetup := False;
if PrintingWithOptions then
begin
SetWindowPos(0, 0, -4400, 0, 0, 0, 0); //SetWindowPos(Wnd, 0, -600, 0, 0, 0, 0);
PrintingWithOptions := False;
end;
if FPrintOptions.FOrientation = poPortrait then
SendDlgItemMessage(PopHandle, $0420, BM_CLICK, 0, 0)
else
SendDlgItemMessage(PopHandle, $0421, BM_CLICK, 0, 0);
SetDlgItemText(PopHandle, $1FD3, PChar(FPrintOptions.FHeader));
SetDlgItemText(PopHandle, $1FD5, PChar(FPrintOptions.FFooter));
SetDlgItemText(PopHandle, $0483, PChar(PrintMarginStr(FPrintOptions.FMargins.FLeft)));
SetDlgItemText(PopHandle, $0484, PChar(PrintMarginStr(FPrintOptions.FMargins.FTop)));
SetDlgItemText(PopHandle, $0485, PChar(PrintMarginStr(FPrintOptions.FMargins.FRight)));
SetDlgItemText(PopHandle, $0486, PChar(PrintMarginStr(FPrintOptions.FMargins.FBottom)));
if (Win32Platform = VER_PLATFORM_WIN32_NT) and (Win32MajorVersion > 4) then
PostMessage(FindWindowEx(PopHandle, 0, 'Button', nil), BM_CLICK, 0, 0) //Win2000
else
SendDlgItemMessage(PopHandle, 1, BM_CLICK, 0, 0);
end;
end;
end;
end;
end;
答案 1 :(得分:0)
要阻止显示网页的弹出广告,您必须使用TWebBrowser的OnNewWindow2和OnBeforeNavigate2事件取消操作。您必须在两个事件中取消它,因为当OnNewWindow2被取消时,TWebBrowser将尝试在主窗口中再次加载相同的URL。因此,需要对OnBeforeNavigate2处理程序进行编码,以便知道事先取消OnNewWindow2以及哪个URL。否则,如果您取消所有内容,则根本无法访问任何网站。
对于脚本警报,将Silent设置为true应该处理这些。