Jump to content

how to use autoit to popup a menu?


Recommended Posts

Hi,

I want to use AutoIt to do the following work. When pressing a predefined key, a menu pops up. Then I click one of the menu item, the correspond code executes. How to do this kind of thing? I can just use AutoIt to do jobs without GUI. Thanks.

Link to comment
Share on other sites

#include <Misc.au3>;include for _ispressed function
;#include <guiConstantsex.au3>;include for $gui_event_close value
$done= 0
;$guihandle= GUICreate("Hello world!, wtf am I supposed to do?¿", 420, 200);make a gui window
;$buttonmax= 5
;dim $button[$buttonmax]
;for $i= 0 to $buttonmax-1
;   $button[$i]= GUICtrlCreateButton("button "&$i, 10, 10+$i*22, 60, 20)
;next
;GUISetState();this code with empty set shows the gui window
while $done= 0;main loop
    ;$msg= GUIGetMsg();get messages from user interaction with gui
    if _IsPressed("0d") then 
        for $i= 500 to 2000
            beep($i, 1)
        next
    endif
    if _IsPressed("1b") then $done= 1
    ;if $msg= $gui_event_close then $done= 1;if the user clicks the X button at the top right of the gui window
wend

press ENTER for beep sound function

press ESC for a function to terminate the program

Not sure what you want do at be able.

Edited by songersoft
Link to comment
Share on other sites

  • Moderators

ogrish,

Not sure my Toast UDF will help you too much - but this might: ;)

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

; Set a HotKey to call the menu GUI - here it is Ctrl-Shft-A
HotKeySet("^+a", "_Visible")

; Set a HotKey to exit
HotKeySet("{ESC}", "On_Exit")

Global $aItems[6][3] = [ _ ; Set the 6 to be one more than the number of items you want
    [5, 0, 0], _ ; Set the 5 to be the number of items
    ["Item 1", "What you want to do 1", 0], _ ; Just add the items here - [Button title, what you want to do, 0]
    ["Item 2", "What you want to do 2", 0], _
    ["Item 3", "What you want to do 3", 0], _
    ["Item 4", "What you want to do 4", 0], _
    ["Item 5", "What you want to do 5", 0]]

; Cretae the GUI
$hGUI = GUICreate("My PopUp Menu", 200, 20 * $aItems[0][0], -1, -1, BitOR($WS_POPUP, $WS_BORDER)) ; Change the -1, -1 to the coordinates you want
; Create the buttons
For $i = 1 To $aItems[0][0]
    $aItems[$i][2] = GUICtrlCreateButton($aItems[$i][0], 0, (20 * $i) - 20, 200, 20)
Next
; Hide the GUI until the Hotkey makes it visible
GUISetState(@SW_HIDE)

While 1

    $iMsg = GUIGetMsg()
    Switch $iMsg
        ; Look for the buttons
        Case $aItems[1][2] To $aItems[$aItems[0][0]][2]
            ; Hide the GUI again once a button is pressed
            GUISetState(@SW_HIDE)
            ; Get the index of the button pressed
            $iIndex = $iMsg - $aItems[1][2] + 1
            ; Display the action - you could use Run here to start an app
            MsgBox(0, "Action", $aItems[$iIndex][1])
    EndSwitch
WEnd

Func _Visible()
    ; When the HotKey is pressed make the GUI visible
    GUISetState(@SW_SHOW)
EndFunc

Func On_Exit()
    Exit
EndFunc

Please ask if you have any questions. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

If you want a context-menu like interface, you can use this code:

#include<WindowsConstants.au3>
#include<GUIMenu.au3>
HotKeySet("{F10}", "_Show")
$hGUI = GUICreate("", 1, 1, -5, -5, $WS_POPUP, $WS_EX_TOOLWINDOW)
$ctlMenu = GUICtrlCreateContextMenu()
$ctlMenuAlert = GUICtrlCreateMenuItem("Alert", $ctlMenu)
$ctlMenuClose = GUICtrlCreateMenuItem("Exit", $ctlMenu)
While 1
    Switch GUIGetMsg()
        Case $ctlMenuAlert
            MsgBox(0, '', "Message")
        Case $ctlMenuClose
            Exit
    EndSwitch
WEnd

Func _Show()
    GUISetState(@SW_SHOW, $hGUI)
    _GUICtrlMenu_TrackPopupMenu(GUICtrlGetHandle($ctlMenu), $hGUI, -1, -1, 1, 1, 1)
    GUISetState(@SW_HIDE, $hGUI)
EndFunc

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

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...