- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
如何以编程方式启动 IE (iexplore.exe
) 并在当前运行的实例中导航(通过打开新选项卡或替换当前 URL)而不是创建新实例。
我搜索了一个命令行开关,并尝试使用 InternetExplorer.Application
,但无济于事。
这是我需要的伪(IE6-IE9 会很好):
ShellExecute(Handle, 'open', 'iexplore.exe',
'"http://google.com" -single_instance',
nil, SW_RESTORE);
下面是一些代码来演示我的尝试。 Delphi 中的代码(部分基于 How to get IHTMLDocument2 from a HWND ):
implementation
uses ShellApi, ComObj, ActiveX, SHDocVw, MSHTML;
function GetIEFromHWND(WHandle: HWND; var IE: IWebbrowser2): Boolean;
type
TObjectFromLResult = function(LRESULT: lResult; const IID: TIID;
wParam: WPARAM; out pObject): HRESULT; stdcall;
var
hInst: HMODULE;
lRes: Cardinal;
Msg: UINT;
pDoc: IHTMLDocument2;
ObjectFromLresult: TObjectFromLresult;
begin
Result := False;
hInst := LoadLibrary('oleacc.dll');
if hInst <> 0 then
try
@ObjectFromLresult := GetProcAddress(hInst, 'ObjectFromLresult');
if @ObjectFromLresult <> nil then
begin
Msg := RegisterWindowMessage('WM_HTML_GETOBJECT');
if SendMessageTimeOut(WHandle, Msg, 0, 0, SMTO_ABORTIFHUNG, 1000, lRes) <> 0 then
if ObjectFromLresult(lRes, IHTMLDocument2, 0, pDoc) = S_OK then
begin
(pDoc.parentWindow as IServiceprovider).QueryService(
IWebbrowserApp, IWebbrowser2, IE);
Result := IE <> nil;
end;
end;
finally
FreeLibrary(hInst);
end;
end;
function GetActiveIEServerWindow(const Activate: Boolean=True): HWND;
var
Wnd, WndChild: HWND;
begin
Result := 0;
Wnd := FindWindow('IEFrame', nil); // top level IE
if Wnd <> 0 then
begin
WndChild := FindWindowEx(Wnd, 0, 'Shell DocObject View', nil);
if WndChild <> 0 then
begin
WndChild := FindWindowEx(WndChild, 0, 'Internet Explorer_Server', nil);
if WndChild <> 0 then
begin
Result := WndChild;
if Activate then
begin
if IsIconic(Wnd) then
ShowWindow(Wnd, SW_RESTORE)
else
SetForegroundWindow(Wnd);
end;
end;
end;
end;
end;
// Method 1
procedure TForm1.Button1Click(Sender: TObject);
const
navOpenInNewTab = $800;
var
IEServerWnd: HWND;
IE: IWebBrowser2;
begin
IEServerWnd := GetActiveIEServerWindow;
if (IEServerWnd <> 0) and GetIEFromHWnd(IEServerWnd, IE) then
begin
// *** this opens the Default browser, e.g Google Chrome
// *** if IE is the Default browser, an empty new window is opened.
OleVariant(IE).Navigate('http://www.yahoo.com', Longint(navOpenInNewTab));
end
else
begin
ShellExecute(Handle, 'open', 'iexplore.exe',
'"http://google.com"',
nil, SW_RESTORE);
end;
end;
procedure InternetExplorerNavigate(URL: WideString);
const
navOpenInNewTab = $800;
var
IE: OleVariant;
begin
try
// *** this always fails (security constraints?)
IE := GetActiveOleObject('InternetExplorer.Application');
except
IE := CreateOleObject('InternetExplorer.Application');
end;
IE.Visible := True;
IE.Navigate(URL, Longint(navOpenInNewTab));
end;
// Method 2
procedure TForm1.Button2Click(Sender: TObject);
begin
InternetExplorerNavigate('http://google.com');
end;
最佳答案
您的方法“1”确实有效。至少在这里,问题是“Shell DocObject View”窗口不是顶级 ie 窗口的直接子窗口。对于 IE8,“Internet Explorer_Server”窗口是“Shell DocObject View”的子窗口,“TabWindowClass”窗口是“Frame Tab”的子窗口。如果您可以确认“FindWindowEx”在方法 1 中返回 0,这就是它失败的原因。下面是修改为使用 EnumChildWindows
的代码:
function EnumChilds(hwnd: HWND; lParam: LPARAM): BOOL; stdcall;
const
Server = 'Internet Explorer_Server';
var
ClassName: array[0..24] of Char;
begin
GetClassName(hwnd, ClassName, Length(ClassName));
Result := ClassName <> Server;
if not Result then
PLongWord(lParam)^ := hwnd;
end;
function GetActiveIEServerWindow(const Activate: Boolean=True): HWND;
var
Wnd, WndChild: HWND;
begin
Result := 0;
Wnd := FindWindow('IEFrame', nil); // top level IE
if Wnd <> 0 then
begin
// WndChild := FindWindowEx(Wnd, 0, 'Shell DocObject View', nil);
// if WndChild <> 0 then
// begin
// WndChild := FindWindowEx(WndChild, 0, 'Internet Explorer_Server', nil);
WndChild := 0;
EnumChildWindows(Wnd, @EnumChilds, LongWord(@WndChild));
if WndChild <> 0 then
begin
Result := WndChild;
if Activate then
begin
if IsIconic(Wnd) then
ShowWindow(Wnd, SW_RESTORE)
else
SetForegroundWindow(Wnd);
end;
end;
// end;
end;
end;
至于方法“2”,我在网上看到一些链接指出 IE 不支持为其事件对象返回引用,但我没有任何官方引用。
关于c# - 如何强制 Internet Explorer 打开它当前运行的实例而不是创建一个新实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10139727/
像其他人一样,我需要在 Internet Explorer 6 和 Internet Explorer 7 上测试我的代码。现在,Internet Explorer 8 为开发人员提供了一些很棒的工具
这个问题在这里已经有了答案: 10年前关闭。 Possible Duplicate: Running Internet Explorer 6, Internet Explorer 7, and Int
作为一名开发者,我发现新的 Internet Explorer 版本完全是一场噩梦。我关闭了 Windows 功能,但无法安装 Internet Explorer 10 。它说它已经安装,但事实并非如
我通过Blogger有一个博客-http://www.concertexperiencepeeps.com。每当我尝试通过Internet Explorer查看我的博客时,该页面都会崩溃,并且不允许我
这个问题在这里已经有了答案: 10年前关闭。 Possible Duplicate: Running Internet Explorer 6, Internet Explorer 7, and Int
我正在使用新的Internet Explorer 11开发人员工具将文档模式切换为“8”,但条件注释仍然被忽略,也就是说,它们没有被正确解析并且表现得像普通注释。因此,浏览器不会请求/加载条件注释内的
我在我的一个站点上遇到 Internet Explorer 6 问题,我真的希望我安装它而不是 Internet Explorer 7。有没有一种快速的方法来做到这一点? 最佳答案 下载Microso
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and t
如何访问 Internet Explorer 运行实例的经典 Internet Explorer COM 自动化对象?也就是说,如果我在多个窗口中打开 Internet Explorer,如何从 Po
string filename = Server.UrlPathEncode(Path.GetFileName(_Filename))); Response.AddHeader("Content-Di
当我尝试使用 Apache 2.2 在 Windows7(64 位)上的 IE9 或 IE10 中打开 localhost 时,解析 URL 需要很长时间。其他浏览器没有问题并立即解析 URL,只有
我有一个批处理文件,必须启动 Internet Explorer 并打开 www.google.com .当整个页面加载完成时,它应该终止 IE 进程,即关闭该系统中的所有 IE 实例。我的批处理文件
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 6 个月前关闭。 Improve t
基本上,无论我使用 IE 访问哪个网站,我都可以看到那些黑色/白色形状,一旦我将鼠标移到它们上,它们就会消失。 想知道是否有人知道可能导致这种情况的原因? 另外:http://imageshack.c
我检查了所有注册表,但找不到所有已安装扩展的列表。 此时,IE 扩展/加载项的任何一般位置都会有所帮助。 最佳答案 取自 here : 浏览器帮助对象 - 旨在增强浏览器功能的浏览器插件。 条目可以在
我在Internet Explorer 8的LAN设置中使用了2个不同的自动配置脚本地址。在工作过程中,我需要经常在它们之间进行切换。每次我必须手动进行。 有没有一种方法可以通过脚本等自动执行操作,以
这是一个两部分的问题。我正在构建一个网页,我需要知道: 有没有办法检测 IE 是否启用了平滑滚动(如果是,如何)? 有没有办法强制 IE 关闭我网页的平滑滚动功能? 明确地说,我不是在问如何关闭整个计
我想强制 IE10 在 IE9 模式下从我的 Intranet 呈现某些站点。 我曾尝试使用本地兼容性列表但无济于事(%LocalAppData%\Microsoft\Internet Explore
我在使用 IE8 时遇到问题,其中带有查询字符串的 URL 在下载时被破坏。 特别是,像这样的 URL http://domain.com/software/Software%202.1%20Beta
我有一个非常基本的页面,其中包括 元素。当我提交带有选定文件的表单时,服务器会响应一个在 Excel 中打开的电子表格(“新窗口”)。这种行为意味着初始屏幕和输入元素在 IE 中仍然可见。如果我更改所
我是一名优秀的程序员,十分优秀!