Sign in to follow this  
Followers 0
masterlink950

Closing tabs with middle-click

8 posts in this topic

Hello, First off, I want to say that I very much appreciate the hard work that people put in to replying on these forums and helping newbs. I've gotten 99% of my answers through google, forum searches, and documentation.

Okay, so. I'm actually sure this has been asked before, but I wasn't able to find anything and I'm having some difficulty.
I want to delete tabs in a control with a middle click (or specifically, detect when a tab has been middle-clicked).

Initially I was going to try to use GUIGetCursorInfo along with checking for a middle-click, the problem is, when the cursor is over a tab, it simply returns the ID of the whole control (I'm using the native tab functionality).

If I can avoid it, I REALLY don't want to manually check coordinates of tabs when the middle mouse is clicked, as tabs are going to be generated dynamically (anywhere from one to infinity tabs if the user lets things get out of hand), and the window I'm using will be resizable (thus having to run checks if a tab is out of view, etc), but I can if I have no other choice.

 

So basically, Is there either 1: a simple native/UDF way to detect middle-click on tabs, 2: a way to get GUIGetCursorInfo give the control ID of the tab itself instead of the control, or 3: something I haven't thought of all together?

Thanks in advance!

Share this post


Link to post
Share on other sites



Tab of what? Could you post the code you've tried?

1 person likes this

Share this post


Link to post
Share on other sites

I'm looking at find the tab ID of an AutoIt GUI.

Here is some code I threw together from other examples to show what I'm trying to do. (It crashes if you middle-click outside of the GUI window. I didn't want to spend anymore time on it).

#include <GUIConstantsEx.au3>
#include <Misc.au3>

$hGUI = GUICreate("Window with Tabs", 500, 500)
Global $controlID = 0
GUICtrlCreateLabel("ID of clicked control:", 200, 1)
$controlID = GUICtrlCreateLabel("0", 300, 1, 50)
$dll = DllOpen("user32.dll")
HotKeySet("{ESC}", "GetPos")

$hTab = GUICtrlCreateTab(10, 10, 480, 480)

For $i = 0 To 2
    GUICtrlCreateTabItem("Tab " & $i)
    GUICtrlCreateButton("Button " & $i, 20 + ($i * 100), 40 + ($i * 50), 80, 30)
Next
GUICtrlCreateTabItem("")

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
    If _IsPressed("04", $dll) Then GetPos()
WEnd

DllClose($dll)


Func GetPos()
    Local $a = GUIGetCursorInfo()
    GUICtrlSetData($controlID, $a[4])
EndFunc

You will see that when you middle-click anywhere inside the GUI tab control, it gives you the ID of the entire control. I need to make it so if you click on the individual tabs at the top, it will display the ID of only that tab.

Share this post


Link to post
Share on other sites

Perfect! Posting code takes away a lot of the guessing game and I don't have to spend the time putting together the GUI myself o:)

Here's a way to use WM_NOTIFY to get the index of the tab. It appears that WM_MBUTTONDOWN (and UP) are not being received when on a tab so I used the _IsPressed.

#include <GUIConstantsEx.au3>
#include <Misc.au3>
#include <WindowsConstants.au3>
#include <GUITab.au3>

Global $iTabIndex = -1
Global $hGUI = GUICreate("Window with Tabs", 500, 500)
Global $lblId = GUICtrlCreateLabel("ID of clicked control:", 200, 1)
Global $hTab = GUICtrlCreateTab(10, 10, 480, 480)

For $i = 0 To 2
    GUICtrlCreateTabItem("Tab " & $i)
    GUICtrlCreateButton("Button " & $i, 20 + ($i * 100), 40 + ($i * 50), 80, 30)
Next

GUIRegisterMsg($WM_NOTIFY, WM_NOTIFY)
GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
    If (_IsPressed(4)) Then DeleteTabItem()
WEnd

Func DeleteTabItem()
    If ($iTabIndex > -1) Then
        _GUICtrlTab_DeleteItem(GUICtrlGetHandle($hTab), $iTabIndex)
        $iTabIndex = -1
    EndIf
EndFunc   ;==>DeleteTabItem

Func WM_NOTIFY($hWndFrom, $iMsg, $wParam, $lParam)
    #forceref $hWndFrom, $iMsg, $wParam, $lParam
    Local $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    Local $iCode = DllStructGetData($tNMHDR, "Code")
    Local $iIdFrom = DllStructGetData($tNMHDR, "IDFrom")

    Switch ($hWndFrom)
        Case $hGUI
            Switch ($iCode)
                Case -530
                    $iTabIndex = $iIdFrom
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

I'll test a WNDPROC tomorrow, see if it captures the middle mouse click

Also, the -530 is used to parse the application for the tooltip text to display. It's in the ToolTipConstants.au3

Global Const $TTN_GETDISPINFOW = $TTN_FIRST - 10 ; [Unicode] Sent to retrieve information needed to display a ToolTip

We're not displaying a tooltip but it's a good enough value to use to determine that the tab is being highlighted.

1 person likes this

Share this post


Link to post
Share on other sites

That works pretty well, actually. The only problem is there is a delay in trying to use the tooltip display number to determine the tab that is highlighted. It also "keeps" that tab ID if you move outside of the GUI without hovering over another control, and if you middle-cick, it deletes that tab. same goes for if you hover over one tab long enough, and don't sit over another tab long enough for the tooltip ID to display.

Do you think there is any other way we can identify what tab is selected? I even tried playing with the example for "_GUICtrlTab_Create" to see if I could identify what tab is pressed, but that doesn't do it either. It still displays the same ID regardless of different tabs.

At this rate, I'm almost curious if I could just create my own tab system using labels as buttons.

 

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Idk, I feel like you're making it harder than it needs to be. There's nothing wrong with checking mouse coords for something if it works!

I was at work all day and came home to see a question that looked enticing I got carried away. Here's the solution to your dilemma. I even included all of the possible windows messages you can get from the Window procedure for our tab control ;) (There are like 3 values that I got that I could not find in the WindowsConstants.au3 file, even did a global search in the #include folder and didn't find it)

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GUITab.au3>
#include <WinApi.au3>
#include <WinAPIGdi.au3>

Global $hGUI = GUICreate("Window with Tabs", 500, 500)
Global $lblId = GUICtrlCreateLabel("Hovering over tab index: N/A | N/A", 190, 4, 200, 20)
GUICtrlSetFont(-1, 8, 400, "", "Segoe UI")
Global $tabMainTab = GUICtrlCreateTab(10, 30, 480, 480)
Global $hWndTab = GUICtrlGetHandle($tabMainTab)
Global $hWndProc = DllCallbackRegister("NewWndProc", "ptr", "hwnd;uint;wparam;lparam")
Global $pOldWndProc = _WinAPI_SetWindowLong($hWndTab, $GWL_WNDPROC, DllCallbackGetPtr($hWndProc))
Global $iTabItems = 13

For $i = 0 To $iTabItems
    GUICtrlCreateTabItem("Tab " & $i)
    GUICtrlCreateButton("Button " & $i, 20, 100, 80, 30)
Next

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

Func NewWndProc($hWndFrom, $iMsg, $wParam, $lParam)
    #forceref $hWndFrom, $iMsg, $wParam, $lParam
    ; Struct that can be used to get information for SOME of the Windows Messages below
    ; Use caution, using the $iCode or $iIdFrom on the wrong windows message will break your code.
    ; It's best to initialize them INSIDE the Switch ($iMsg) below.
    ; Check the URL to see what values you can get from the $lParam and $wParam
    Local $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    ;Local $iCode = DLLStructGetData($tNMHDR, "Code")
    ;Local $iIdFrom = DLLStructGetData($tNMHDR, "IDFrom")
    ; Only valid on mouse functions
    Local $iMouseX = _WinAPI_LoWord($lParam)
    Local $iMouseY = _WinAPI_HiWord($lParam)

    Switch ($hWndFrom)
        Case $hWndTab
            Switch ($iMsg)
                Case $WM_LBUTTONDOWN
                    ; https://msdn.microsoft.com/en-us/library/windows/desktop/ms645607(v=vs.85).aspx
                Case $WM_LBUTTONUP
                    ; https://msdn.microsoft.com/en-us/library/windows/desktop/ms645608%28v=vs.85%29.aspx
                Case $WM_RBUTTONDOWN
                    ; https://msdn.microsoft.com/en-us/library/windows/desktop/ms646242(v=vs.85).aspx
                Case $WM_RBUTTONUP
                    ; https://msdn.microsoft.com/en-us/library/windows/desktop/ms646243%28v=vs.85%29.aspx
                Case $WM_MBUTTONDOWN
                    ; https://msdn.microsoft.com/en-us/library/windows/desktop/ms645610(v=vs.85).aspx
                Case $WM_MBUTTONUP
                    ; https://msdn.microsoft.com/en-us/library/windows/desktop/ms645611%28v=vs.85%29.aspx
                    For $i = 0 To $iTabItems
                        Local $aTabRect = _GUICtrlTab_GetItemRect($hWndTab, $i)
                        If (Not @error) Then
                            If (_WinAPI_PtInRectEx($iMouseX, $iMouseY, $aTabRect[0], $aTabRect[1], $aTabRect[2], $aTabRect[3])) Then
                                ConsoleWrite("Deleting tab index: " & $i & " | Tab text: " & _GUICtrlTab_GetItemText($hWndTab, $i))
                                _GUICtrlTab_DeleteItem($hWndTab, $i)
                                $iTabItems -= 1
                                ExitLoop
                            EndIf
                        EndIf
                    Next
                Case $WM_MOUSEMOVE
                    ; https://msdn.microsoft.com/en-us/library/windows/desktop/ms645616%28v=vs.85%29.aspx
                    For $i = 0 To $iTabItems
                        Local $aTabRect = _GUICtrlTab_GetItemRect($hWndTab, $i)
                        If (Not @error) Then
                            If (_WinAPI_PtInRectEx($iMouseX, $iMouseY, $aTabRect[0], $aTabRect[1], $aTabRect[2], $aTabRect[3])) Then ExitLoop
                        EndIf
                    Next
                    GUICtrlSetData($lblId, "Hovering over tab index: " & $i & " | " & _GUICtrlTab_GetItemText($hWndTab, $i))
                Case $WM_NCHITTEST
                    ; https://msdn.microsoft.com/en-us/library/windows/desktop/ms645618%28v=vs.85%29.aspx
                Case $WM_SETCURSOR
                    ; https://msdn.microsoft.com/en-us/library/windows/desktop/ms648382%28v=vs.85%29.aspx
                Case $WM_PAINT
                    ; https://msdn.microsoft.com/en-us/library/windows/desktop/dd145213(v=vs.85).aspx
                Case $WM_MOUSELEAVE
                    ; https://msdn.microsoft.com/en-us/library/windows/desktop/ms645615%28v=vs.85%29.aspx
                    GUICtrlSetData($lblId, "Hovering over tab index: N/A | N/A")
                Case $WM_MOUSEACTIVATE
                    ; https://msdn.microsoft.com/en-us/library/windows/desktop/ms645612%28v=vs.85%29.aspx
                Case $WM_NOTIFY
                    ; https://msdn.microsoft.com/en-us/library/windows/desktop/bb775583%28v=vs.85%29.aspx
                Case $WM_NOTIFYFORMAT
                    ; https://msdn.microsoft.com/en-us/library/windows/desktop/bb775584(v=vs.85).aspx
                Case $WM_UPDATEUISTATE
                    ; https://msdn.microsoft.com/en-us/library/windows/desktop/ms646361%28v=vs.85%29.aspx
                Case $WM_GETDLGCODE
                    ; https://msdn.microsoft.com/en-us/library/windows/desktop/ms645425%28v=vs.85%29.aspx
                Case $WM_IME_SETCONTEXT
                    ; https://msdn.microsoft.com/en-us/library/windows/desktop/dd374142(v=vs.85).aspx
                Case $WM_SETFOCUS
                    ; https://msdn.microsoft.com/en-us/library/windows/desktop/ms646283(v=vs.85).aspx
                Case $WM_ERASEBKGND
                    ; https://msdn.microsoft.com/en-us/library/windows/desktop/ms648055%28v=vs.85%29.aspx
                Case $WM_NCPAINT
                    ; https://msdn.microsoft.com/en-us/library/windows/desktop/dd145212%28v=vs.85%29.aspx
                Case 0x001308, 0x00130A, 0x002006, 0x00133E, 0x00130C, 0x000129, 0x000210
                    ; Unknown messages that are constantly sent to the tab control but could not identify in WindowsConstants.au3
                Case 0x00133C
                    ; Unknown message sent to the tab control when highlighting an item
                Case Else
                    ConsoleWrite('[' & @HOUR & ":" & @MIN & ":" & @SEC & ":" & @MSEC & "] Unknown Message: 0x" & Hex($iMsg, 6) & @LF & "############################################" & @LF)
            EndSwitch
    EndSwitch

    Return _WinAPI_CallWindowProc($pOldWndProc, $hWndFrom, $iMsg, $wParam, $lParam)
EndFunc   ;==>NewWndProc

Also, I didn't actually test it but I'm guessing _GUICtrlTab_GetItemRect is just the same as _WinApi_GetRect which will get the rect of a given handle. Can be used on buttons, edits, etc. And since tab items are really just buttons

Edited by InunoTaishou
Added more messages to switch
1 person likes this

Share this post


Link to post
Share on other sites

Wow. That is incredibly helpful! A great reference too, I like it! I was actually working on a script that when the middle mouse button is pressed, it changes a variable and emulate a L-mouse click if its inside the window. Whenever a tab switched, and if that earlier var was changed to true, it would delete the tab and revert back to the previous tab. A bit hacky, but it probably would have worked. However, this is much better.

Thanks a ton!

Share this post


Link to post
Share on other sites

Lmao, sounds like a work around I would have done if I hadn't known about window procedures. I hope you were at least using a mouse hook so the WM_MBUTTONDOWN was never sent to the GUI :P

1 person likes this

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Similar Content

    • nassausky
      By nassausky
      Hi all,
       
      Anyone have any idea how to close all open tabs except a specific one I manually open.  Assuming I don't know what is open in all the tabs except just the one I want to keep open.
       
      I didn't want to use sendkeys and I was trying to use the following code to list the title (or url) of the 3 open tabs and  after I got that part working I would just close the other 2. This sample only displays the title of the first open tab
      #include <IE.au3> Const $ie_new_in_tab = 0x0800 $oIE = _IECreate("https://www.autoitscript.com") __IENavigate($oIE, "https://www.autoitscript.com/forum/", 1, $ie_new_in_tab) ;(obj,url,wait,param) __IENavigate($oIE, "https://www.google.com/", 1, $ie_new_in_tab) ;(obj,url,wait,param) Local $aIE[1] $aIE[0] = 0 Local $i = 1, $oIE While 1     $oIE = _IEAttach("", "instance", $i)     If @error = $_IEStatus_NoMatch Then ExitLoop     ConsoleWrite(_IEPropertyGet($oIE, "title") & @CRLF)     ReDim $aIE[$i + 1]     $aIE[$i] = $oIE ;each item holds object     $aIE[0] = $i ;first item holds count     $i += 1 WEnd MsgBox($MB_SYSTEMMODAL, "Browsers Found", "Number of browser instances in the array: " & $aIE[0]) ; This doesn't return the list of tabs in the console just the first tab  
      Thanks for any and all help
    • islandspapand
      By islandspapand
      Hi all
      i am currently trying to click on an element in a HTML Table, but just can get it to work.
      i am able to click the top of the table so it changes to sort  but just can't click on the element in the table.
      an i need to click on element to continue in the site.
      i have attached the code so far and pictures of the table  element want to click plus the source of the table.
      i am able to get data in the table with $oTable = _IETableGetCollection($oIE, 2) but not able to click on them.
       
      Help is very much appreciated
       
      #cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.14.2 Author: myName Script Function: Template AutoIt script. #ce ---------------------------------------------------------------------------- ; Script Start - Add your code below here #include <IE.au3> #include "DOM.au3" #include <Array.au3> #include <MsgBoxConstants.au3> Global $oIE = _IECreate("*") _IELoadWait($oIE) Sleep(2000) _PageLogin($oIE) _PageLoadWait() _PageNewReq($oIE) _PageLoadWait() _InputModelInf($oIE) _PageLoadWait() Sleep(1000) $aTableLink = BGe_IEGetDOMObjByXPathWithAttributes($oIE, "//table/tbody/tr/td[.='Name Of user']", 2000) ;~ $aTableLink = BGe_IEGetDOMObjByXPathWithAttributes($oIE, "//table/tbody/tr", 2000) ;~ _ArrayDisplay($aTableLink,"$aTableLink") If IsArray($aTableLink) Then ConsoleWrite("Able to BGe_IEGetDOMObjByXPathWithAttributes($oIE, //table/tbody/tr/td[.='Name Of user'])" & @CRLF) For $i = 0 To UBound($aTableLink)-1 ConsoleWrite(" OuterHTML : " & $aTableLink[$i].outerHTML & @CRLF) ConsoleWrite(" Parentnode : " & $aTableLink[$i].parentnode & @CRLF) ConsoleWrite(" Parentnode.click : " & $aTableLink[$i].parentnode.fireEvent("onclick","click") & @CRLF) $objClick = $aTableLink[$i].parentnode ;~ _IEAction($aTableLink[$i] , "focus") _IEAction($objClick , "focus") ;~ If _IEAction($aTableLink[$i], "click") Then If _IEAction($objClick, "click") Then ConsoleWrite("Able to _IEAction($aForumLink[0], 'click')" & @CRLF) _IELoadWait($oIE) Else ConsoleWrite("UNable to _IEAction($aForumLink[0], 'click')" & @CRLF) Exit 3 EndIf Next Else ConsoleWrite("Unable to BGe_IEGetDOMObjByXPathWithAttributes($oIE, //table/tbody/tr/td[.='Name Of user'])" & @CRLF) Exit 2 EndIf _PageLoadWait() Func _InputModelInf($oTmpIE) ; Add Var for Model & Serial in Func $oModelInput = _IEGetObjById($oTmpIE,"model") _IEAction($oModelInput,"focus") _IEDocInsertText($oModelInput, "*") $oSerialInput = _IEGetObjById($oTmpIE,"serial") _IEAction($oModelInput,"focus") _IEDocInsertText($oSerialInput, "*") $links = $oTmpIE.document.getElementsByClassName("btn btn-primary ng-scope") For $link In $links If $link.innertext = "Søg" Or $link.innertext = "Search" Then $link.click() ExitLoop EndIf Next Return True EndFunc Func _PageNewReq($oTmpIE) $links = $oTmpIE.document.getElementsByClassName("ng-scope k-link") For $link In $links If $link.innertext = "Send ny fejlmelding" Or $link.innertext = "Submit a New Service Request" Then $link.click() ExitLoop EndIf Next Return True EndFunc Func _PageLogin($oTmpIE) $oUserInput = _IEGetObjById($oTmpIE,"loginid") _IEDocInsertText($oUserInput, "*") $oPasswordInput = _IEGetObjById($oTmpIE,"password") _IEDocInsertText($oPasswordInput, "*") $links = $oTmpIE.document.getElementsByClassName("btn btn-primary login ng-scope") For $link In $links If $link.innertext = "Sign in" Then $link.click() ExitLoop EndIf Next Return True EndFunc Func _PageLoadWait() Local $PageLoadWait = False ;~ nav navbar-nav navbar-right ng-hide ;~ nav navbar-nav navbar-right $tags = $oIE.document.GetElementsByTagName("ul") For $tag in $tags $class_value = $tag.GetAttribute("class") If $class_value = "nav navbar-nav navbar-right" Then ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : Webpage loading :) ' & @CRLF) ;### Debug Console $PageLoadWait = True ExitLoop EndIf Next Do sleep(250) For $tag in $tags $class_value = $tag.GetAttribute("class") If $class_value = "nav navbar-nav navbar-right ng-hide" Then ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : Webpage load finished :)'& @CRLF) ;### Debug Console $PageLoadWait = False ExitLoop EndIf Next Until $PageLoadWait = False EndFunc  
      Thanks in advance
       
       


    • Miliardsto
      By Miliardsto
      Hello I got these tabs
       
      Global $TabMain = GUICtrlCreateTabItem("Main") Global $Action = GUICtrlCreateTabItem("Actions") Global $TabThings = GUICtrlCreateTabItem("Things") As we know to change tab we have to click with mouse on tab name like here it looks like below.
      Is there possibility to change tab without click on the tab but change by click on button for example?

    • OtenMoten
      By OtenMoten
      Hello dear community,
       
      definitely I need external help now. I've did all 'ControlClick() - Tutorials' and read a lot of topics about interaction betweend AutoIT and external programs.
      The most of them are about "Clicking a button ...". In my case it's different because I need to click a specific element within the program.
       
      The 'program' what I am talking about is called Mendeley. It's library software for documents administration.
       
      In the attachements in picture '1.jpg' you can see the red marked Help element. 
      In the picture '2.jpg' you see the context menu of the Help element. This is what I'm trying to do with the ControlClick() function.
       
      Following I represent my code which not worked for me:
      #include <MsgBoxConstants.au3> Opt("WinTitleMatchMode", 2) $hWnd = WinGetHandle("Mendeley Desktop", "") MsgBox($MB_SYSTEMMODAL, "handle check", $hWnd) ControlClick($hWnd, "Mendeley Desktop", "Help") The MsgBox tells me the number of the handle of the Mendeley window. It's not 0 so for me it means the window is recognized correctly.
       
      When I'm execute the script the Help element is not clicked.
      Any suggestions for me?
       
      Kind regards
      OtenMoten