abberration Posted October 11, 2020 Share Posted October 11, 2020 Hi, everyone After playing with GuiRegisterMsg and WM_COMMAND for about an hour trying to solve a problem, I learned something and it brings up a new question. The script I'm working on has a search function and a list of files. I want to be able to double-click a file on the list and open the folder containing the file. To do this, I used GuiRegisterMsg for $LBN_DBLCLK and referenced a dummy file that I put at the very beginning of the script. This caused the Case for $idDummy to infinitely pop up the messagebox before touching the WM_COMMAND. I finally figured out I needed to declare the dummy in the child form, and all is working well and I'm able to go to the next step. Here's my question: to make this happen, on the child GUI, I have to declare dummy (and list box) globally. I have read it is best to declare all variables as local. I cannot think of any way of making this script work without declaring them globally. Is there something I am missing or is it sometimes absolutely necessary to declare them globally? Thanks in advance for any advice! Here is some condensed code: expandcollapse popup#include <GUIConstants.au3> #include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> #include <WindowsConstants.au3> #include <GuiListView.au3> $Form1 = GUICreate("Main Form", 355, 169, 0, 0) $Button1 = GUICtrlCreateButton("Click To Open List", 80, 64, 195, 25, $WS_GROUP) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 _OpenForm2() EndSwitch WEnd Func _OpenForm2() $Form2 = GUICreate("Child Form", 355, 169, 370, 0) Global $idDummy = GUICtrlCreateDummy() ; $idDummy and $List1 must be Global variables. Is there a way to make them Local? Global $List1 = GUICtrlCreateList("Double-Click Me", 16, 8, 321, 123) GUICtrlSetData($List1, "Or... Double-Click Me...") GUISetState(@SW_SHOW) GUIRegisterMsg($WM_COMMAND, "MyFunc") While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE GUIDelete($Form2) ExitLoop Case $idDummy MsgBox(0, "", GUICtrlRead($idDummy)) EndSwitch WEnd EndFunc Func MyFunc($hWnd, $iMsg, $wParam, $lParam) Local $hWndFrom, $iIDFrom, $iCode, $hWndEdit $hWndFrom = $lParam $iIDFrom = _WinAPI_LoWord($wParam) $iCode = _WinAPI_HiWord($wParam) If $iCode = $LBN_DBLCLK AND $iIDFrom = $List1 Then $sClicked = GUICtrlRead($List1) GUICtrlSendToDummy($idDummy, $sClicked) EndIf EndFunc Easy MP3 | Software Installer | Password Manager Link to comment Share on other sites More sharing options...
JockoDundee Posted October 11, 2020 Share Posted October 11, 2020 If it’s a child why is it not defined as one by referencing the parent in the GUICreate statement? Also, just on sight, doesn’t this show that $List1 is passed in $iIDFrom? If $iCode = $LBN_DBLCLK AND $iIDFrom = $List1 Then $sClicked = GUICtrlRead($List1) Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
pixelsearch Posted October 12, 2020 Share Posted October 12, 2020 5 hours ago, abberration said: Here is my question: I have to declare dummy (and list box) globally. I have read it is best to declare all variables as local [...] Is it sometimes absolutely necessary to declare them globally? Hi abberration I think you did it right : control id's and flags needed in registered messages have to be declared Global. As you can't pass them as Parameters in any registered message, then you have to declare them Global if you intend to use them inside the registered message code. * If you look at the Wiki Tutorial GUIRegisterMsg, you'll notice a sentence in the last example : "Whichever method we use, we need to declare the dummy control or the flag as a Global variable" * If you look at the help file, topic _GUICtrlListBox_Create (the only topic where $LBN_DBLCLK is found), you'll notice that the only Global variable declared in the example is : Global $g_hListBox It's Global because it's used within WM_COMMAND() code. Link to comment Share on other sites More sharing options...
argumentum Posted October 12, 2020 Share Posted October 12, 2020 7 hours ago, abberration said: Hi, everyone Hi. Just wanted to say that I like your avatar Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting. Link to comment Share on other sites More sharing options...
LarsJ Posted October 12, 2020 Share Posted October 12, 2020 It's not that hard: expandcollapse popup#include <GUIConstants.au3> #include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> #include <WindowsConstants.au3> #include <GuiListView.au3> $Form1 = GUICreate("Main Form", 355, 169, 0, 0) $Button1 = GUICtrlCreateButton("Click To Open List", 80, 64, 195, 25, $WS_GROUP) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 _OpenForm2() EndSwitch WEnd Func _OpenForm2() $Form2 = GUICreate("Child Form", 355, 169, 370, 0) Local $idDummy = GUICtrlCreateDummy() ; $idDummy and $List1 must be Global variables. Is there a way to make them Local? Local $List1 = GUICtrlCreateList("Double-Click Me", 16, 8, 321, 123) MyFunc(0, $idDummy, 0, $List1) ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< GUICtrlSetData($List1, "Or... Double-Click Me...") GUISetState(@SW_SHOW) GUIRegisterMsg($WM_COMMAND, "MyFunc") While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE GUIDelete($Form2) ExitLoop Case $idDummy MsgBox(0, "", GUICtrlRead($idDummy)) EndSwitch WEnd EndFunc Func MyFunc($hWnd, $iMsg, $wParam, $lParam) Local Static $idDummy, $List1 Local $hWndFrom, $iIDFrom, $iCode, $hWndEdit $hWndFrom = $lParam $iIDFrom = _WinAPI_LoWord($wParam) $iCode = _WinAPI_HiWord($wParam) If $iCode = $LBN_DBLCLK AND $iIDFrom = $List1 Then $sClicked = GUICtrlRead($List1) GUICtrlSendToDummy($idDummy, $sClicked) Return 0 EndIf If $hWnd = 0 And $wParam = 0 Then $idDummy = $iMsg $List1 = $lParam EndIf EndFunc dmob 1 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 Link to comment Share on other sites More sharing options...
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