Jump to content

Recommended Posts

Posted

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!

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

Posted

hello, autoit code tags make it much easier to see the code. :P

  Reveal hidden contents

IUIAutomation - Topic with framework and examples

Au3Record.exe

Posted

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.

  • Moderators
Posted (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.

M23

Edit: Take a look here and here

Edited by Melba23

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:

  Reveal hidden contents

 

Posted

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 Gude
How 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

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

Posted

  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.

Posted

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.

Posted

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

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.

Posted

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 Gude
How 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

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
  • Recently Browsing   0 members

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