Jump to content

Right click on ListView


Recommended Posts

Can someone help me add a right click function on a listview. Basicly in short I would rightclick a ListViewItem and it would get the text from the first section, then a msgbox pops up saying what it is.

Say it looks like this

Number | Other

10 |?

20 |O

If I were to right click the item were 10 is. A menu shows and you select Option "Find text", msgbox shows up saying "You have selected 10"

Example up there was just an example.

Link to comment
Share on other sites

You can use this as a basis for experimentation. You might want to switch to a UDF listview if you're going to rely on more UDF functions though.

#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <WindowsConstants.au3>

;basic GUI
Global $GUI = GUICreate("", 300, 450)
Global $hListView = GUICtrlCreateListView("A|B|C|D|E|F|G", 25, 25, 250, 400)
For $i = 0 To 20
    GUICtrlCreateListViewItem("A" & $i & "|B" & $i & "|C" & $i & "|D" & $i & "|E" & $i & "|F" & $i & "|G" & $i, $hListView)
Next

;Context Menu
Global $hCMenu = GUICtrlCreateContextMenu($hListView) ;add a context menu to the listview. I don't think you can add a seperate one to each item unless you write your own function.
Global $hCMenuText = GUICtrlCreateMenuItem("Get Text", $hCMenu) ;add the get text option to the menu.
Global $sItemText ;this will store the text of the last right-clicked item.

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY") ;This intercepts the Notification windows sends your GUI when you right click the listview (and many others) amd sends it to the "WM_NOTIFY" function.

GUISetState()

While 1
    Switch GUIGetMsg()
        Case -3
            Exit
        Case $hCMenuText
            _ShowText()
    EndSwitch
WEnd

;All Notify messages for your GUI will be send to this function.
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo

    If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)

    ;$ilParam is a pointer. This reads what's at that adress. (not sure why the name suggests its an int)
    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom ;Check what control triggered the msg
        Case $hWndListView ;If it was the listview...
            Switch $iCode ;Check what action triggered the msg
                Case $NM_RCLICK ;If it was a right click...
                    $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam) ;get the information about the item clicked.
                    $sItemText =  _GUICtrlListView_GetItemText($hWndListView, DllStructGetData($tInfo, "Index"), DllStructGetData($tInfo, "SubItem")) ;store the item text in a global variable in case the get text option is clicked.
                    ;You could also just store the Index and SubItem values, the entire $tInfo struct, or whatever works best for you.

                    ;Uncomment the next part to get more information about your click.
;~                  _DebugPrint("$NM_RCLICK" & @LF & "--> hWndFrom:" & @TAB & $hWndFrom & @LF & _
;~                          "-->IDFrom:" & @TAB & $iIDFrom & @LF & _
;~                          "-->Code:" & @TAB & $iCode & @LF & _
;~                          "-->Index:" & @TAB & DllStructGetData($tInfo, "Index") & @LF & _
;~                          "-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @LF & _
;~                          "-->NewState:" & @TAB & DllStructGetData($tInfo, "NewState") & @LF & _
;~                          "-->OldState:" & @TAB & DllStructGetData($tInfo, "OldState") & @LF & _
;~                          "-->Changed:" & @TAB & DllStructGetData($tInfo, "Changed") & @LF & _
;~                          "-->ActionX:" & @TAB & DllStructGetData($tInfo, "ActionX") & @LF & _
;~                          "-->ActionY:" & @TAB & DllStructGetData($tInfo, "ActionY") & @LF & _
;~                          "-->lParam:" & @TAB & DllStructGetData($tInfo, "lParam") & @LF & _
;~                          "-->KeyFlags:" & @TAB & DllStructGetData($tInfo, "KeyFlags"))

            EndSwitch
        EndSwitch

    ;Returning this allows the GUI to handle the messages in the usual way once you're done. Returning anything else will block default behavior. (giving you a largely unresponsive GUI)
    Return $GUI_RUNDEFMSG
EndFunc

Func _ShowText()
    MsgBox(0,"Test",$sItemText)
EndFunc

Func _DebugPrint($s_text, $line = @ScriptLineNumber)
    ConsoleWrite( _
            "!===========================================================" & @LF & _
            "+======================================================" & @LF & _
            "-->Line(" & StringFormat("%04d", $line) & "):" & @TAB & $s_text & @LF & _
            "+======================================================" & @LF)
EndFunc
Link to comment
Share on other sites

Melba23 has made a Udf.

While the exact function you are after may not be in there, I'm sure one could be modified to suit your purpose, as items can be selected and moved at will on the fly.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

@Tvern Thank you, your code worked slightly what I needed.

@JohnOne I will definitly take a look at the UDF.

@Tvern Whats not working is if you select the row A0 is on, right click and click get text, it shows A0 like its supost to, but if you select the row that has A1 in it, it responds with B0 on mine when it should get A1 instead. I am still looking at it seeing what I can change and stuff, and see if I can fiddle with it to change stuff depending on what I right click and select. Like say if I right clicked and clicked an option called "Get Second Text" Id right click the A0 Row and it would reply B0(example only), you get what I mean right? just letting you know.

@JohnOne I am going to look at the UDF now and see what I can fiddle with that. Even if it might not have what I am looking for the UDF looks to be very interesting.

Link to comment
Share on other sites

Without looking at Melba23 code, I am just assuming that since it can select and move the items around, that it must hold the variable of what it is moving in order to keep track of it, (famous last words).

So if you have that, its win.

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

@JohnOne What you just said, made perfect sense. It must be able to keep track with a variable if its moving the order, how else would it work out?

I have found out what is wrong. The program code you gave me works exactly as its supost to, the problem is it only works on windows xp machines. It fails to comprehend the workings of Vista or Windows 7. I have tested and verified that it is the problem.

Edited by KurogamineNox
Link to comment
Share on other sites

I think the problem is that the 64 bit version stores its data in memory as a different datatype, so you'd have to adjust your Structs to request the correct datatypes, but at this point I don't see a reason to use x64, so I don't usually bother with trying to figure it out.

Link to comment
Share on other sites

Makes sense, I guess there isnt any reason to fix the coding as as long as it works and I am not dependent on a cross platform server I should be fine. Both my computer and my laptop run 64bit processors and I don't see any use for having a 64 bit server if the 32 bit works perfectly.

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