Jump to content

WM_COMMAND, child GUI and Dummy question


Recommended Posts

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:

#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

 

Link to comment
Share on other sites

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

It's not that hard:

#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

 

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