Jump to content

How to trigger GUIGetMsg with Right Click?


Burgaud
 Share

Recommended Posts

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

Example()

Func Example()
    Local $sMESSAGE = "The following buttons have been clicked"

    GUICreate("My GUI list") ; will create a dialog box that when displayed is centered

    Local $idAdd = GUICtrlCreateButton("Add", 64, 32, 75, 25)
    Local $idClear = GUICtrlCreateButton("Clear", 64, 72, 75, 25)
    Local $idMylist = GUICtrlCreateList("buttons that have been clicked", 176, 32, 121, 97)
    GUICtrlSetLimit(-1, 200) ; to limit horizontal scrolling
    GUICtrlSetData(-1, $sMESSAGE)
    Local $idClose = GUICtrlCreateButton("my closing button", 64, 160, 175, 25)

    GUISetState(@SW_SHOW)

    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_SECONDARYUP 
                Local $a = GUIGetCursorInfo()
                if $a[4] = $idMylist then MsgBox(0,"Info","Right click on List")
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $idAdd
                GUICtrlSetData($idMylist, "You clicked button No1|")
            Case $idClear
                GUICtrlSetData($idMylist, "")
            Case $idClose
                MsgBox($MB_SYSTEMMODAL, "", "the closing button has been clicked", 2)
                Exit
        EndSwitch
    WEnd
EndFunc   ;==>Example

$GUI_EVENT_SECONDARYUP  Event ist fired, when you release the Right Mousbutton

$GUI_EVENT_SECONDARYDOWN  Event ist fired, when you press the Right Mousbutton

The 4. ArrayElement from GUIGetCursorInfo() is the Id from the Control, where the Mousecursor is.

Edited by Raupi
Link to comment
Share on other sites

Burgaud,

Detecting the control is nice, but, then what?  To get the item that is under the cursor when you R-CLK on the control you can do this... 

#include <GuiListBox.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <array.au3>

Example()

Func Example()
    Local $sMESSAGE = "The following buttons have been clicked", $aCurPos, $aCTLPos

    GUICreate("My GUI list") ; will create a dialog box that when displayed is centered

    Local $idAdd = GUICtrlCreateButton("Add", 64, 32, 75, 25)
    Local $idClear = GUICtrlCreateButton("Clear", 64, 72, 75, 25)
    Local $idMylist = GUICtrlCreateList("buttons that have been clicked", 176, 32, 121, 97)
    GUICtrlSetLimit(-1, 200) ; to limit horizontal scrolling
    GUICtrlSetData(-1, $sMESSAGE)
    Local $idClose = GUICtrlCreateButton("my closing button", 64, 160, 175, 25)

    GUISetState(@SW_SHOW)

    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_SECONDARYUP

                $aCurPos = GUIGetCursorInfo()
                $aCTLPos = ControlGetPos('', '', $idMylist)

                If $aCurPos[4] = $idMylist Then
                    $str = _GUICtrlListBox_GetText($idMylist, _
                            _GUICtrlListBox_ItemFromPoint($idMylist, $aCurPos[0] - $aCTLPos[0], $aCurPos[1] - $aCTLPos[1]))
                    MsgBox(0, 'ListBox Item', (($str = "") ? 'Cursor is NOT over an item' : 'The item at cursor location is:' & @CRLF & $str))
                EndIf

            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $idAdd
                GUICtrlSetData($idMylist, "You clicked button No1|")
            Case $idClear
                GUICtrlSetData($idMylist, "")
            Case $idClose
                MsgBox($MB_SYSTEMMODAL, "", "the closing button has been clicked", 2)
                Exit
        EndSwitch
    WEnd
EndFunc   ;==>Example

All in all, I think you will find ListViews easier to work with if you need to use R-CLK or popup menus, among other things.

kylomas

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Here is an example of using a listview.  See comments in the code...

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

Local $sHelp = 'Right click on any entry and choose one of the popup menu items.  Watch the console area for results'

; Define message numbers to be associated with popup menu items.
; These will also be the message id's that the application message hander (WM_COMMAND) will use to action a control.
Global Enum $idproc1 = 1000, $idproc2

; Create the gui and listview control.
Local $gui010 = GUICreate('Listview Example', 200, 200)
Local $lv010 = GUICtrlCreateListView('Types', 20, 20, 65, 150)

; Populate the listview control using an array and resize the column width to automatically fit the data length.
Local $aTranTypes[7][1] = [['Auto'], ['Bus'], ['Airplane'], ['Train'], ['Boat'], ['Motorcycle'], ['Helicopter']]
_GUICtrlListView_AddArray($lv010, $aTranTypes)
_GUICtrlListView_SetColumnWidth($lv010, 0, $LVSCW_AUTOSIZE)

Local $help = GUICtrlCreateLabel($sHelp, 100, 20, 100, 150)

; The are dummy controls that will be actioned by the applications message handler based on the message id associated with the popup menu.
; E.G. $dummy_proc1 will be actioned by application message $idproc1 from message handler WM_COMMAND.
Local $dummy_proc1 = GUICtrlCreateDummy()
Local $dummy_proc2 = GUICtrlCreateDummy()

; Set in the notification message handler (WM_NOTIFY) to get the item number of the listview item clicked on.
Local $iItem = 0

; Popup menu...each item is associated with an application defined message ($idproc1 and $idproc2).
local $hMenu = _GUICtrlMenu_CreatePopup()
_GUICtrlMenu_InsertMenuItem($hMenu, 0, "Run Procedure #1", $idproc1)
_GUICtrlMenu_InsertMenuItem($hMenu, 1, "Run Procedure #2", $idproc2)

GUISetState()

; Register the message handlers
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

While 1
    Switch GUIGetMsg()
        Case $gui_event_close
            Exit
        Case $dummy_proc1
            ConsoleWrite('You have choosen to run Procedure #1 on ' & _GUICtrlListView_GetItemText($lv010, $iItem) & @CRLF)
        Case $dummy_proc2
            ConsoleWrite('You have choosen to run Procedure #2 on ' & _GUICtrlListView_GetItemText($lv010, $iItem) & @CRLF)
    EndSwitch
WEnd

; Notification message handler.  This is what will detect the right click.
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)

    ; structure to map $ilParam ($tNMHDR - see Help file)
    Local $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)

    Switch $tNMHDR.IDFrom
        Case $lv010
            Switch $tNMHDR.Code
                Case $NM_RCLICK
                    ; another structure to remap $ilParam...used to get the item that was right clicked
                    $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
                    If $tInfo.Item > -1 Then
                        $iItem = $tInfo.Item
                        ; positions the popup menu at the right clicked item
                        _GUICtrlMenu_TrackPopupMenu($hMenu, $gui010)
                    endif
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

; Application message handler...this is what will action the controls in the message loop
Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
    ; $iwParam contains the application messages that we defined earlier
    Switch $iwParam
        Case $idproc1
            guictrlsendtodummy($dummy_proc1)
        Case $idproc2
            guictrlsendtodummy($dummy_proc2)
    EndSwitch
EndFunc   ;==>WM_COMMAND

kylomas

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Awesome!

I was struggling on that part and what I came up with... suck.

All in all, I think you will find ListViews easier to work with if you need to use R-CLK or popup menus, among other things.

 

Exactly what I was trying to do. to bring out a popup - to edit that particular entry and such!

Your 2nd example made me think.. currently, i got 2 buttons below my listviews and are ugly. a pop up appears cleaner!

Im new to autoit, and the best way to learn is to translate/convert some of my perl apps over.

Link to comment
Share on other sites

Good, glad it helped.  If the comments in the code seem obvious it is because I had no idea where you are at based on your posts.  M23 has a listview UDF that most people use >here.

edit: additional info

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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