Delphi 如何关闭ie8选项卡

Delphi 如何关闭ie8选项卡,delphi,winapi,internet-explorer-8,Delphi,Winapi,Internet Explorer 8,下面的代码未关闭Internet Explorer 8中的选项卡。如果我将wm_close命令发布到Wnd,它将关闭Internet Explorer,但我希望关闭当前选项卡,而不是整个“ieframe”。FindWindowEX(Wnd,0,'Frame Tab',nil)是否应该向ie Frame重新运行句柄?如果是,为什么不关闭Internet Explorer中的当前选项卡 var Wnd, WndChild : hwnd; begin Wnd := FindWindow('

下面的代码未关闭Internet Explorer 8中的选项卡。如果我将wm_close命令发布到Wnd,它将关闭Internet Explorer,但我希望关闭当前选项卡,而不是整个“ieframe”。FindWindowEX(Wnd,0,'Frame Tab',nil)是否应该向ie Frame重新运行句柄?如果是,为什么不关闭Internet Explorer中的当前选项卡

var
   Wnd, WndChild : hwnd;
begin
   Wnd := FindWindow('IEFrame', nil);
   WndChild := FindWindowEX(Wnd, 0, 'Frame Tab', nil);
   postmessage(WndChild, wm_close, 0, 0);
end;
IE8窗口结构如下面的屏幕截图所示


您错过了一层,选项卡本身,除此之外,一切正常

var
  Wnd, WndChild: THandle;
begin
  Wnd := FindWindow('IEFrame', nil); // Top most IE
  if Wnd > 0 then
  begin
    WndChild := FindWindowEx(Wnd, 0, 'Frame Tab', nil); // Tabs holder
    if WndChild > 0 then
    begin
      WndChild := FindWindowEX(WndChild, 0, 'TabWindowClass', nil); // top most tab
      if WndChild > 0 then
        if PostMessage(WndChild, WM_CLOSE, 0, 0) then
          ShowMessage('Close request succeeded...')
        else
          ShowMessage('Failed!');
    end
    else
      // not tabbed, close IE
        if PostMessage(Wnd, WM_CLOSE, 0, 0) then
          ShowMessage('Close request succeeded...')
        else
          ShowMessage('Failed!');
  end
  else
    ShowMessage('No IE');
end;

乔治:好吧,理论上这应该是可能的,如果IE8正在为选项卡创建实际的窗口,这似乎是可能的。诀窍在于找到正确的窗口句柄,这可能确实非常困难——或者甚至不可能,正如您所说的那样。:-)谢谢你的回答,我有点困惑:1.你为什么用dword而不是hwnd。2.closehandle(htab)与htab相等:=nil 3。closehandle是必要的吗?一旦我的程序退出,句柄会自动关闭吗?非常糟糕的代码示例。1) 你应该检查FindWindow[Ex]的结果。2) 您不需要关闭FindWindow[Ex]返回的句柄。实际上,这是为了说明应该如何做,为思考(错误捕获)留下一些空间。您在哪里发现了不关闭句柄(更不用说了)?是的,关闭接收到的窗口句柄WM_CLOSE确实会生成异常(这就是我没有关闭它的原因)。FindWindow的帮助[Ex]并没有说明关闭句柄的必要性。你从FindWindow[Ex]得到的是一个全局可见的句柄;该调用不会为您“打开”某些内容,因此无需关闭任何内容。+1表示Spy++中的screencap,-1表示即使(正确)告知代码错误,也没有从代码中删除
CloseHandle()
var
  Wnd, WndChild: THandle;
begin
  Wnd := FindWindow('IEFrame', nil); // Top most IE
  if Wnd > 0 then
  begin
    WndChild := FindWindowEx(Wnd, 0, 'Frame Tab', nil); // Tabs holder
    if WndChild > 0 then
    begin
      WndChild := FindWindowEX(WndChild, 0, 'TabWindowClass', nil); // top most tab
      if WndChild > 0 then
        if PostMessage(WndChild, WM_CLOSE, 0, 0) then
          ShowMessage('Close request succeeded...')
        else
          ShowMessage('Failed!');
    end
    else
      // not tabbed, close IE
        if PostMessage(Wnd, WM_CLOSE, 0, 0) then
          ShowMessage('Close request succeeded...')
        else
          ShowMessage('Failed!');
  end
  else
    ShowMessage('No IE');
end;