LurchMan Posted January 10, 2013 Posted January 10, 2013 Hello Everyone - I'm trying to get a context menu to show up on a list view control. Every thing appears to be working with it except it wont build the context menu. Can someone help me? This is a stripped down version of what I'm working with but it has everything related to the context menu and list view control. Any help is appreciated! Thank you! expandcollapse popup#Include <GuiListView.au3> #Include <winapi.au3> #include <WindowsConstants.au3> #include <GUIConstantsEx.au3> #include <ListViewConstants.au3> Opt("GUIOnEventMode", 1) Opt("GUIEventOptions", 1) Global $sigs_yes, $sigs_no, $context Global $bMenuActive = False $Form1 = GUICreate("Example", 466, 580, 0, 0) GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit") $hListView = _GUICtrlListView_Create ($Form1, "", 8, 16, 138, 382) _GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES)) _GUICtrlListView_AddColumn($hListView, "Col1", 85) _GUICtrlListView_AddColumn($hListView, "Col2", 85) _GUICtrlListView_AddItem ($hListView, "Testing") GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY") Global $B_DESCENDING[_GUICtrlListView_GetColumnCount($hListView)] GUISetState(@SW_SHOW) While 1 Sleep (100) WEnd Func _Exit () Exit EndFunc Func _BuildMenu ($hcontrol, $text) If $bMenuActive = True Then GUICtrlDelete ( $context ) $bMenuActive = True ConsoleWrite($text & @LF) Switch $hcontrol Case $hListView $ID = _WinAPI_GetDlgCtrlID($hcontrol) $context = GUICtrlCreateContextMenu ($ID) ConsoleWrite($context & @LF) $sigs = GUICtrlCreateMenu("Signatures", $context) $sigs_yes = GUICtrlCreateMenuItem ("Yes", $sigs) GUICtrlSetOnEvent (-1, "_contextmenu") $sigs_no = GUICtrlCreateMenuItem ("No", $sigs) GUICtrlSetOnEvent (-1, "_contextmenu") EndSwitch EndFunc Func _contextmenu () Switch @GUI_CtrlId Case $sigs_yes $iIndex = _GUICtrlListView_GetSelectedIndices ($hListView) _GUICtrlListView_SetItemText($hListView, $iIndex, "Yes", 1) Case $sigs_no $iIndex = _GUICtrlListView_GetSelectedIndices ($hListView) _GUICtrlListView_SetItemText($hListView, $iIndex, "No", 1) EndSwitch GUICtrlDelete ( $context ) $bMenuActive = False EndFunc Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam) #forceref $hWnd, $iMsg, $iwParam Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo $hWndListView1 = $hListView $tNMHDR = DllStructCreate($tagNMHDR, $ilParam) $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom")) $iIDFrom = DllStructGetData($tNMHDR, "IDFrom") $iCode = DllStructGetData($tNMHDR, "Code") Switch $hWndFrom Case $hWndListView1 Switch $iCode Case $NM_RCLICK $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam) $selectedtext = _GUICtrlListView_GetItemText ($hWndListView1, DllStructGetData($tInfo, "Index")) $contextMenu_LV = $hWndListView1 _BuildMenu ($hWndListView1, $selectedtext) EndSwitch EndSwitch Return $GUI_RUNDEFMSG EndFunc ;==>WM_NOTIFY Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.
careca Posted January 10, 2013 Posted January 10, 2013 hello, autoit code tags make it much easier to see the code. Reveal hidden contents Renamer - Rename files and folders, remove portions of text from the filename etc. GPO Tool - Export/Import Group policy settings. MirrorDir - Synchronize/Backup/Mirror Folders BeatsPlayer - Music player. Params Tool - Right click an exe to see it's parameters or execute them. String Trigger - Triggers pasting text or applications or internet links on specific strings. Inconspicuous - Hide files in plain sight, not fully encrypted. Regedit Control - Registry browsing history, quickly jump into any saved key. Time4Shutdown - Write the time for shutdown in minutes. Power Profiles Tool - Set a profile as active, delete, duplicate, export and import. Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes. NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s. IUIAutomation - Topic with framework and examples Au3Record.exe
LarsJ Posted January 10, 2013 Posted January 10, 2013 Replace this line $hListView = _GUICtrlListView_Create ($Form1, "", 8, 16, 138, 382) with these two lines and it will work $idListView = GUICtrlCreateListView("", 8, 16, 138, 382) $hListView = ControlGetHandle( $Form1, "", $idListView ) _WinAPI_GetDlgCtrlID() only returns an AutoIt controlID (as GUICtrlCreateContextMenu() needs) if the listview is created as a native control. Now you can skip _WinAPI_GetDlgCtrlID() and use $idListView directly. Lars. Controls, File Explorer, ROT objects, UI Automation, Windows Message MonitorCompiled code: Accessing AutoIt variables, DotNet.au3 UDF, Using C# and VB codeShell menus: The Context menu, The Favorites menu. Shell related: Control Panel, System Image ListsGraphics related: Rubik's Cube, OpenGL without external libraries, Navigating in an image, Non-rectangular selectionsListView controls: Colors and fonts, Multi-line header, Multi-line items, Checkboxes and icons, Incremental searchListView controls: Virtual ListViews, Editing cells, Data display functions
Moderators Melba23 Posted January 10, 2013 Moderators Posted January 10, 2013 (edited) LurchMan,I am pretty sure that ListView (and certainly TreeView) items do not return a ControlID when queried by _WinAPI_GetDlgCtrlID - you are certainly getting 0 returned in your script. I seem to remember rover producing some of his usual esoteric code a while back - I will see if I can find it for you.M23Edit: Take a look here and here Edited January 10, 2013 by Melba23 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: Reveal hidden contents ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
BrewManNH Posted January 10, 2013 Posted January 10, 2013 I don't believe a normal context menu can be created on a UDF created listview/listview item. GUICtrlContextMenuCreate requires an AutoIt control ID, a UDF created listview and the items in them don't have control IDs just handles. though old, should give you some ideas to work with. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! Reveal hidden contents I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator
LurchMan Posted January 10, 2013 Author Posted January 10, 2013 (edited) On 1/10/2013 at 6:24 PM, 'LarsJ said: Replace this line$hListView = _GUICtrlListView_Create ($Form1, "", 8, 16, 138, 382)with these two lines and it will work$idListView = GUICtrlCreateListView("", 8, 16, 138, 382)$hListView = ControlGetHandle( $Form1, "", $idListView )_WinAPI_GetDlgCtrlID() only returns an AutoIt controlID (as GUICtrlCreateContextMenu() needs) if the listview is created as a native control.Now you can skip _WinAPI_GetDlgCtrlID() and use $idListView directly.Lars.Thank you for the reply. Unfortunatly I know this is a solution however I need to use the UDF version of the Listview for another section of this script that inserts colored boxes next to some of these items. I was unable to get this function to work on windows 7 using the native Listview functions. I apologize, I should have stated this earlier.Edit: Thank you everyone for your replies. I'm going to attempt to use BrewManNH's idea that he was able to show me. Edited January 10, 2013 by LurchMan Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.
LurchMan Posted January 10, 2013 Author Posted January 10, 2013 On 1/10/2013 at 6:26 PM, 'BrewManNH said: I don't believe a normal context menu can be created on a UDF created listview/listview item. GUICtrlContextMenuCreate requires an AutoIt control ID, a UDF created listview and the items in them don't have control IDs just handles. though old, should give you some ideas to work with.Thank you so much for this information!! This was able to give me exactly what I needed! Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.
LarsJ Posted January 10, 2013 Posted January 10, 2013 But as long as you have the handle you can use all the functions in the UDF. Or am I wrong? Are there any differences in these two scenarios. First: $idListView = GUICtrlCreateListView( "", 8, 16, 138, 382 ) $hListView = ControlGetHandle( $hGui, "", $idListView ) Do all the rest with functions from the UDF Second: $hListView = _GUICtrlListView_Create( $hGui, "", 8, 16, 138, 382 ) Do all the rest with functions from the UDF Except that in the first scenario you have a ControlId, and it can often be useful. I would like to know that. Regards Lars. Controls, File Explorer, ROT objects, UI Automation, Windows Message MonitorCompiled code: Accessing AutoIt variables, DotNet.au3 UDF, Using C# and VB codeShell menus: The Context menu, The Favorites menu. Shell related: Control Panel, System Image ListsGraphics related: Rubik's Cube, OpenGL without external libraries, Navigating in an image, Non-rectangular selectionsListView controls: Colors and fonts, Multi-line header, Multi-line items, Checkboxes and icons, Incremental searchListView controls: Virtual ListViews, Editing cells, Data display functions
LurchMan Posted January 10, 2013 Author Posted January 10, 2013 On 1/10/2013 at 7:52 PM, 'LarsJ said: But as long as you have the handle you can use all the functions in the UDF. Or am I wrong?Are there any differences in these two scenarios.First:$idListView = GUICtrlCreateListView( "", 8, 16, 138, 382 )$hListView = ControlGetHandle( $hGui, "", $idListView )Do all the rest with functions from the UDFSecond:$hListView = _GUICtrlListView_Create( $hGui, "", 8, 16, 138, 382 )Do all the rest with functions from the UDFExcept that in the first scenario you have a ControlId, and it can often be useful.I would like to know that.Regards Lars.There are major differences between the native functions and the UDF functions. If I was to explain it I would probably be wrong but I do know there are major differences between the two and what you can do with them. I generally try to only use the UDF functions or only use the native functions as I have experienced some unwanted behavior when mixing the two. Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.
BrewManNH Posted January 10, 2013 Posted January 10, 2013 It will depend on what you're doing with the controls as to whether it's easy to mix them or not. Most of the time you shouldn't have a problem mixing the two, you just have to make sure you don't treat the UDF controls as if they're native controls. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! Reveal hidden contents I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now