Jump to content

[Solved] How to Know When a ContextMenu Opened?


Zohar
 Share

Recommended Posts

Hi

I am trying to Automate an application where when RightClicking it,

some times the ContextMenu opens,

and sometimes it doesn't, and need another RightClick to succeed.

Is there any way to know when a ContextMenu opens?

Thank you

Edited by Zohar
Link to comment
Share on other sites

Hi Ian :)

The AutoIt Window Info Tool does not see open menus.

As If they are not a window..

If anyone has a trick here, it would really help me,

because not always when I ask to open a Menu, it is indeed opened..

So I need a way to verify it, to make my code more robust..

Link to comment
Share on other sites

Hi Zohar, I am new to Autoit but I hope to be able to help. The ContextMenu is considered a window, you can try to find out how many windows are there before the click and then after the click, if the number is the same the ContextMenu is not open if they are more after the click the ContextMenu is open. You can use this code to find out how many windows are there:

Local $var = WinList()

MsgBox(0, "Window Number", Ubound($var) )

Ubound($var) can be used for the comparison

Link to comment
Share on other sites

What is the application that you are attempting this with. Because the ContextMenu might be custom made therefore it might not be a window but using PixelSearch function could help.

Link to comment
Share on other sites

Hi Fixis

That's definately a creative idea..

If indeed a ContextMenu is a Window, then it should work,

however there's one problem with is: If another window (which is not the ContextMenu) opens during the time I wait for the ContextMenu to open,

then my script wil be wrong, thinking the ContextMenu opened.

So it's not a deterministic method for knowing.

(but I will definitely remember it for other places where coutnring the number of windows might help)

BTW during the past day, I thought about a way to solve my problem here:

Instead of opening the ContextMenu, and then moving with the arrows and selecting the appropriate MeniItem, and then simulating [Enter],

I am going to try something else:

To capture the Windows Message that is sent when clicking the appropriate MenuItem,

and then to simulate that Windows Message.

That way, I can achieve the functionality I wanted, without even opening the ContextMenu!

Hi Aipion.

the appliction is TeamViewer.

Whenever I use TeamViewer, there're some operations that I usually do at the beginning,

like Change Resolution of target machine,

Change Quality,

and move to Full Screen.

So I want all these operations to be done on the beginning, automatically.

I will post here If I managed to do it via SendMessage()

Thank you both

Zohar

Link to comment
Share on other sites

Hi Zohar,

You can use the code below to know when the ContextMenu is opened.

#include <GuiMenu.au3>
While 1
   Sleep(10)
   If WinActive("TeamViewer") Then ;Makes sure not any active ContextMenu is accepted
   If WinExists("[CLASS:#32768]") Then ;Makes sure it is a ContextMenu.
   $SomeName = _GUICtrlMenu_GetMenuBarInfo(WinGetHandle("[CLASS:#32768]" ), 0, 0)
   MsgBox(64,"Information about the current ContextMenu",$SomeName[0]&" - X coordinate of the upper left corner of the rectangle"&@CRLF& _
   $SomeName[1]&" - Y coordinate of the upper left corner of the rectangle"&@CRLF& _
   $SomeName[2]&" - X coordinate of the lower right corner of the rectangle"&@CRLF& _
   $SomeName[3]&" - Y coordinate of the lower right corner of the rectangle"&@CRLF& _
   $SomeName[4]&" - Handle to the menu bar or popup menu"&@CRLF& _
   $SomeName[5]&" - Handle to the submenu"&@CRLF& _
   $SomeName[6]&" - True if the menu bar has focus, otherwise False"&@CRLF& _
   $SomeName[7]&" - True if the menu item has focus, otherwise False"&@CRLF)
   EndIf
   EndIf
WEnd
Link to comment
Share on other sites

Hi Aipion

This works amazing.

Thank you very much for it.

I would like to ask you something:

The AutoIt Window Info tool, doesn't "see" menus. So how did you find it?

And also:

Is there any way to verify that the Menu that I found(in code), is indeed the menu I am looking for?

For example by getting its first MenuItem, and comparing the Text init to see that it's indeed what I'm expacting for?

Thank you verymuch

Zohar

Edited by Zohar
Link to comment
Share on other sites

I would like to ask you something:

The AutoIt Window Info tool, doesn't "see" menus. So how did you find it?

When you run the tool and unfreeze it then click on the menu button that you want to get info of, you will see the menu drop just then the Info tool will pick up the menus class. you just use that class to know when the menu gets opened.

And also:

Is there any way to verify that the Menu that I found(in code), is indeed the menu I am looking for?

For example by getting its first MenuItem, and comparing the Text init to see that it's indeed what I'm expacting for?

This code will help.

#include <GuiMenu.au3>
While 1
   Sleep(10)
   If WinActive("TeamViewer") Then ;Makes sure not any active ContextMenu is accepted
   If WinExists("[CLASS:#32768]") Then ;Makes sure it is a ContextMenu.
   $ContextMenuWin = WinGetHandle("[CLASS:#32768]" ) ;Handle of the ContextMenu window
   $SomeName = _GUICtrlMenu_GetMenuBarInfo($ContextMenuWin, 0, 0)
   $iText = ""
   For $I = 0 To _GUICtrlMenu_GetItemCount($SomeName[4]) -1
   $iText &= $I&" = " & _GUICtrlMenu_GetItemText($SomeName[4], $I, True) & @CRLF
   Next
   MsgBox(0, "All the Items in the current ContextMenu",$iText)
   MsgBox(64,"Information about the current ContextMenu",$SomeName[0]&" - X coordinate of the upper left corner of the rectangle"&@CRLF& _
   $SomeName[1]&" - Y coordinate of the upper left corner of the rectangle"&@CRLF& _
   $SomeName[2]&" - X coordinate of the lower right corner of the rectangle"&@CRLF& _
   $SomeName[3]&" - Y coordinate of the lower right corner of the rectangle"&@CRLF& _
   $SomeName[4]&" - Handle to the menu bar or popup menu"&@CRLF& _
   $SomeName[5]&" - Handle to the submenu"&@CRLF& _
   $SomeName[6]&" - True if the menu bar has focus, otherwise False"&@CRLF& _
   $SomeName[7]&" - True if the menu item has focus, otherwise False"&@CRLF)
   EndIf
   EndIf
WEnd
Edited by Guest
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...