Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

How to use Post/SendMessage to close tabs in IE7 or IE8


  • Please log in to reply
8 replies to this topic
dvda2k
  • Members
  • 48 posts
  • Last active: Mar 12 2012 09:41 AM
  • Joined: 19 Apr 2007
I want to close a tab when my mouse is hovering on IE (without activate it). I tried the following first:

XButton1::
  MouseGetPos, , , WinID, CtrlClass
  ControlSend, %CtrlClass%, ^w, ahk_id %WinID%
  Return
It works occasional but very unstable. Sometimes it sends "^w" and sometimes "w" only. So I tried different approaches including:

1, use "{Ctrl Down}w{Ctrl Up}" instead of "^w"
2, leave blank or use "ahk_parent" instead of "%CtrlClass%"
3, send to the control directly by using "MouseGetPos, , , , CtrlHWnd, 2" mode

None of the above works as expected. So I decided to go with PostMessage/SendMessage.

I'm on Windows 7 build 7106 with IE8. Most debug tools hang the system including Winspector. The only working one is Microsoft Spy++ v9.0 (ripped from Visual Studio 2008 SP1). I played with it and found some messages I can use to create new tab, switch tabs, close IE without prompt... But I just cannot find the message to close a single tab. Here is the messages recorded by spy++ when ctrl-w to close a tab:

<00001> 00510BF0 S WM_GETICON fType:True [wParam]
<00002> 00510BF0 R WM_GETICON hicon:00300F25 [lResult]
<00003> 00510BF0 S WM_WINDOWPOSCHANGING lpwp:002CEA3C [wParam]
<00004> 00510BF0 R WM_WINDOWPOSCHANGING lResult:00000000
<00005> 00510BF0 S WM_WINDOWPOSCHANGED lpwp:002CEA3C [wParam]
<00006> 00510BF0 R WM_WINDOWPOSCHANGED lResult:00000000
<00007> 00510BF0 S WM_ACTIVATEAPP fActive:True dwThreadID:00000DC8 [wParam]
<00008> 00510BF0 R WM_ACTIVATEAPP lResult:00000000
<00009> 00510BF0 S WM_NCACTIVATE fActive:True [wParam]
<00010> 00510BF0 R WM_NCACTIVATE lResult:00000001
<00011> 00510BF0 S WM_ACTIVATE fActive:WA_ACTIVE fMinimized:False hwndPrevious:(null) [wParam]
<00012> 00510BF0 R WM_ACTIVATE lResult:00000000
<00013> 00510BF0 P message:0x80C9 [User defined:WM_APP+201] wParam:00000194 lParam:00000000
<00014> 00510BF0 S WM_ERASEBKGND hdc:31013A0C [wParam]
<00015> 00510BF0 R WM_ERASEBKGND fErased:True [lResult]
<00016> 00510BF0 S WM_SETTEXT lpsz:002CE6AC ("Blank Page - Windows Internet Explorer") [wParam]
<00017> 00510BF0 S message:0x00AE [Unknown] wParam:00000009 lParam:00000000
<00018> 00510BF0 R message:0x00AE [Unknown] lResult:00000000
<00019> 00510BF0 R WM_SETTEXT fSucceeded:True [lResult]
<00020> 00510BF0 S WM_QUERYUISTATE wParam:00000000 lParam:00000000
<00021> 00510BF0 R WM_QUERYUISTATE lResult:00000002
<00022> 00510BF0 P WM_PAINT hdc:00000000 [wParam]
<00023> 00510BF0 S message:0xC238 [Registered] wParam:00000BDD lParam:00000A8F
<00024> 00510BF0 R message:0xC238 [Registered] lResult:00000000
<00025> 00510BF0 S message:0xC238 [Registered] wParam:00000BE8 lParam:00000A91
<00026> 00510BF0 R message:0xC238 [Registered] lResult:00000000
<00027> 00510BF0 S WM_PARENTNOTIFY fwEvent:WM_DESTROY idChild:0000 (0) hwndChild:000D0ECA [wParam]
<00028> 00510BF0 R WM_PARENTNOTIFY lResult:00000000
<00029> 00510BF0 S WM_GETICON fType:True [wParam]
<00030> 00510BF0 R WM_GETICON hicon:00300F25 [lResult]
<00031> 00510BF0 S WM_GETICON fType:True [wParam]
<00032> 00510BF0 R WM_GETICON hicon:00300F25 [lResult]
<00033> 00510BF0 S message:0xC238 [Registered] wParam:00000BE9 lParam:00000A9A
<00034> 00510BF0 S WM_CHANGEUISTATE wParam:00020002 lParam:00000000
<00035> 00510BF0 S WM_UPDATEUISTATE wParam:00020002 lParam:00000000
<00036> 00510BF0 S WM_GETICON fType:True [wParam]
<00037> 00510BF0 R WM_GETICON hicon:00300F25 [lResult]
<00038> 00510BF0 R WM_UPDATEUISTATE lResult:00000000
<00039> 00510BF0 R WM_CHANGEUISTATE lResult:00000000
<00040> 00510BF0 R message:0xC238 [Registered] lResult:00000000
<00041> 00510BF0 S WM_NCACTIVATE fActive:False [wParam]
<00042> 00510BF0 R WM_NCACTIVATE fDeactivateOK:True [lResult]
<00043> 00510BF0 S message:0xC238 [Registered] wParam:00000BE9 lParam:00000A9B
<00044> 00510BF0 S WM_CHANGEUISTATE wParam:00020002 lParam:00000000
<00045> 00510BF0 R WM_CHANGEUISTATE lResult:00000000
<00046> 00510BF0 R message:0xC238 [Registered] lResult:00000000
<00047> 00510BF0 S WM_ACTIVATE fActive:WA_INACTIVE fMinimized:False hwndPrevious:(null) [wParam]
<00048> 00510BF0 R WM_ACTIVATE lResult:00000000
<00049> 00510BF0 S message:0xC238 [Registered] wParam:00000BE8 lParam:00000A9C
<00050> 00510BF0 R message:0xC238 [Registered] lResult:00000000
<00051> 00510BF0 S WM_ACTIVATEAPP fActive:False dwThreadID:00000DC8 [wParam]
<00052> 00510BF0 R WM_ACTIVATEAPP lResult:00000000
Is there anyone can help me? BTW, I guess IE7 and IE8 may share the same message.

dvda2k
  • Members
  • 48 posts
  • Last active: Mar 12 2012 09:41 AM
  • Joined: 19 Apr 2007
Working messages I found with IE8 on Windows 7 build 7106 (and probably work with XP/Vista IE8):

PostMessage, 0x0465, , , , ahk_class IEFrame ; close IE without prompt even with multiple tabs opened
PostMessage, 0x0466, , , , ahk_class IEFrame ; new tab (ctrl-t)
PostMessage, 0x0467, , , , ahk_class IEFrame ; next tab (ctrl-tab)
PostMessage, 0x0467, 1, , , ahk_class IEFrame ; previous tab (shift-ctrl-tab)
PostMessage, 0x0468, , , , ahk_class IEFrame ; open favorites toggle (ctrl-i)
PostMessage, 0x0469, , , , ahk_class IEFrame ; tab thumbnail view toggle (ctrl-q)
PostMessage, 0x0470, , , , ahk_class IEFrame ; toolbar on/off toggle
PostMessage, 0x0471, , , , ahk_class IEFrame ; find on page (ctrl-f)


dvda2k
  • Members
  • 48 posts
  • Last active: Mar 12 2012 09:41 AM
  • Joined: 19 Apr 2007
Anyone can help?

  • Guests
  • Last active:
  • Joined: --
Ever thought of this?

Send {Ctrl Down}{w}{Ctrl Up}

BTW Thx for your post! I'm using PostMsg to switch among tabs. Works perfectly!

dvda2k
  • Members
  • 48 posts
  • Last active: Mar 12 2012 09:41 AM
  • Joined: 19 Apr 2007

Ever thought of this?

Send {Ctrl Down}{w}{Ctrl Up}

BTW Thx for your post! I'm using PostMsg to switch among tabs. Works perfectly!


Thanks, but what I need is close tab under mouse, even if IE window is not activated.

I figured out lately. Here is my code:

F4::
	If WinGetAll(WinClass, WinID, CtrlClass, CtrlHwnd)
	{
			If WinClass = IEFrame
			{
				ControlGet, TabHwnd, Hwnd, , TabWindowClass1, ahk_id %WinID%
				PostMessage, 0x111, 0x1017E, , , ahk_id %TabHwnd%
			}
			Else PostMessage, 0x112, 0xF060, , , ahk_id %WinID%
	}
	Return

WinGetAll(ByRef WinClass, ByRef WinID, ByRef CtrlClass, ByRef CtrlHwnd)
{
	MouseGetPos, , , WinID, CtrlClass
	MouseGetPos, , , WinID, CtrlHwnd, 2
	WinGetClass, WinClass, ahk_id %WinID%
	If (WinClass="") || (WinClass="Button") || (WinClass="Progman") || (WinClass="WorkerW") || (WinClass="Shell_TrayWnd")
		|| (WinClass="DV2ControlHost") || (WinClass="Desktop User Picture")||(WinClass="IPTip_AutoHide_Window")
		|| (WinClass="SideBar_HTMLHostWindow") || (WinClass="TaskListThumbnailWnd") || (WinClass="#32768")
		ErrLevel = 0
	Else ErrLevel = 1
	Return ErrLevel
}


r7393s
  • Members
  • 22 posts
  • Last active: Apr 02 2012 05:06 PM
  • Joined: 26 Feb 2011

Working messages I found with IE8 on Windows 7 build 7106 (and probably work with XP/Vista IE8):

PostMessage, 0x0465, , , , ahk_class IEFrame ; close IE without prompt even with multiple tabs opened
PostMessage, 0x0466, , , , ahk_class IEFrame ; new tab (ctrl-t)
PostMessage, 0x0467, , , , ahk_class IEFrame ; next tab (ctrl-tab)
PostMessage, 0x0467, 1, , , ahk_class IEFrame ; previous tab (shift-ctrl-tab)
PostMessage, 0x0468, , , , ahk_class IEFrame ; open favorites toggle (ctrl-i)
PostMessage, 0x0469, , , , ahk_class IEFrame ; tab thumbnail view toggle (ctrl-q)
PostMessage, 0x0470, , , , ahk_class IEFrame ; toolbar on/off toggle
PostMessage, 0x0471, , , , ahk_class IEFrame ; find on page (ctrl-f)


Hi all. First post here. First thing I want to do is thank this community. It is incredible and the wealth of information is amazing.

I have been searching for a way to activate an existing tab in Internet Explorer. I have not been able to find a solution. I saw some commands that have to do with COM objects. I am able to run commands on that inactive tab, but I'm unable to make it the active tab. I thought that can probably be done with some Post/SendMessage commands, so I got to this page, but I don't know of a way to do this. Can anyone help me? Whether is with Post/SendMessage commands, or COM objects (preferrably code for AHK_L).

Thanks in advance! :)

sinkfaze
  • Moderators
  • 6367 posts
  • Last active: Nov 30 2018 08:50 PM
  • Joined: 18 Mar 2008

I have been searching for a way to activate an existing tab in Internet Explorer. I have not been able to find a solution.


See Sean's code here.

a4u
  • Guests
  • Last active:
  • Joined: --

See Sean's code here.

Here's an updated version (using Sean's ComUtils):
TabActivate(tabName, hwnd=[color=#666666]""[/color]) { ; http://www.autohotkey.com/forum/topic37651.html&p=231093#231093
	[color=#107095]ControlGet[/color], hTabUI, hWnd,, DirectUIHWND1, % hwnd? [color=#666666]"ahk_id"[/color] hwnd:[color=#666666]"ahk_class IEFrame"[/color]
	Acc := Acc_ObjectFromWindow(hTabUI) ; access "Tabs" control
	tabs := (Acc.accChildCount > 1)?  Acc.accChild(3): ; test # of tabs
	[color=#107095]while[/color] (tabs.accChildCount >= [color=brown]A_Index[/color]) [color=#107095]and[/color] (tab := tabs.accChild([color=brown]A_Index[/color]))
		[color=#107095]if[/color] (tab.accName(0) = tabName) ; test vs. "tabName"
			[color=#107095]return[/color], tab.accDoDefaultAction(0) ; invoke tab
}


r7393s
  • Members
  • 22 posts
  • Last active: Apr 02 2012 05:06 PM
  • Joined: 26 Feb 2011

See Sean's code here.

Here's an updated version (using Sean's ComUtils):
TabActivate(tabName, hwnd=[color=#666666]""[/color]) { ; http://www.autohotkey.com/forum/topic37651.html&p=231093#231093
	[color=#107095]ControlGet[/color], hTabUI, hWnd,, DirectUIHWND1, % hwnd? [color=#666666]"ahk_id"[/color] hwnd:[color=#666666]"ahk_class IEFrame"[/color]
	Acc := Acc_ObjectFromWindow(hTabUI) ; access "Tabs" control
	tabs := (Acc.accChildCount > 1)?  Acc.accChild(3): ; test # of tabs
	[color=#107095]while[/color] (tabs.accChildCount >= [color=brown]A_Index[/color]) [color=#107095]and[/color] (tab := tabs.accChild([color=brown]A_Index[/color]))
		[color=#107095]if[/color] (tab.accName(0) = tabName) ; test vs. "tabName"
			[color=#107095]return[/color], tab.accDoDefaultAction(0) ; invoke tab
}


This code worked! I had tried Sean's code but it wasn't working. After installing the ComUtils, your code worked beautifully!