Что нового

WM_NOTIFY Как отследить события в разных окнах

pvnn

Осваивающий
Сообщения
305
Репутация
32
Здравствуйте!
Необходимо отследить в WM_NOTIFY событие "Нажатие правой кнопки мыши на ListView" в form2 и form3
В WM_NOTIFY можно добавить анализ окон? или как мне лучше поступить?

Код:
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>

Global $hListView1
Global $hListView2

$Form1 = GUICreate("Form1", 245, 126, -1, -1)
$Button1 = GUICtrlCreateButton("Button1", 16, 16, 75, 25)
$Button2 = GUICtrlCreateButton("Button2", 152, 16, 75, 25)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            form2()
        Case $Button2
            form3()
    EndSwitch
WEnd

Func form2()
$Form2 = GUICreate("", 421, 215, -1, -1)
$hListView1 = GUICtrlCreateListView("", 8, 8, 402, 166, BitOR($LVS_REPORT,$LVS_SHOWSELALWAYS))
GUISetState(@SW_SHOW)
GUIRegisterMsg($WM_NOTIFY, 'WM_NOTIFY')
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            GUIDelete($Form2)
            Return
    EndSwitch
WEnd
EndFunc


Func form3()
$Form3 = GUICreate("", 421, 215, -1, -1)
$hListView2 = GUICtrlCreateListView("", 8, 8, 402, 166, BitOR($LVS_REPORT,$LVS_SHOWSELALWAYS))
GUISetState(@SW_SHOW)
GUIRegisterMsg($WM_NOTIFY, 'WM_NOTIFY')
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            GUIDelete($Form3)
            Return
    EndSwitch
WEnd
EndFunc


Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $tNMHDR, $hWndFrom, $iIDFrom, $iCode
    #forceref $hWnd, $iMsg, $iwParam, $ilParam

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")

    ; $hListView1 в $Form2
    Local $hWndListView1 = $hListView1
    If Not IsHWnd($hWndListView1) Then $hWndListView1 = GUICtrlGetHandle($hListView1)

    ; $hListView1 в $Form3
    $hWndListView2=$hListView2
    If Not IsHWnd($hListView2) Then $hWndListView2 = GUICtrlGetHandle($hListView2)

    Switch $hWndFrom
        Case $hWndListView1
            Switch $iCode
                Case $NM_RCLICK
                    ConsoleWrite('$NM_RCLICK_ListView_1'&@CRLF)
            EndSwitch

        Case $hWndListView2
            Switch $iCode
                Case $NM_RCLICK
                    ConsoleWrite('$NM_RCLICK_ListView_2'&@CRLF)
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc
 

IMStrelcov

CTPEJIbLLOB
Сообщения
258
Репутация
66
Код:
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>

Global $hListView1
Global $hListView2
Global $hWndListView1
Global $hWndListView2

$Form1 = GUICreate("Form1", 245, 126, -1, -1)
$Button1 = GUICtrlCreateButton("Button1", 16, 16, 75, 25)
$Button2 = GUICtrlCreateButton("Button2", 152, 16, 75, 25)
GUISetState(@SW_SHOW)

While 1
   Switch GUIGetMsg()
   Case $GUI_EVENT_CLOSE
      Exit
   Case $Button1
      form2()
   Case $Button2
      form3()
   EndSwitch
WEnd

Func form2()
   $Form2 = GUICreate("", 421, 215, -1, -1)
   $hListView1 = GUICtrlCreateListView("", 8, 8, 402, 166, BitOR($LVS_REPORT,$LVS_SHOWSELALWAYS))
   $hWndListView1 = GUICtrlGetHandle($hListView1)
   GUISetState(@SW_SHOW)
   GUIRegisterMsg($WM_NOTIFY, 'WM_NOTIFY')
   While 1
      Switch GUIGetMsg()
      Case $GUI_EVENT_CLOSE
         GUIDelete($Form2)
         ExitLoop
      EndSwitch
   WEnd
EndFunc

Func form3()
   $Form3 = GUICreate("", 421, 215, -1, -1)
   $hListView2 = GUICtrlCreateListView("", 8, 8, 402, 166, BitOR($LVS_REPORT,$LVS_SHOWSELALWAYS))
   $hWndListView2 = GUICtrlGetHandle($hListView2)
   GUISetState(@SW_SHOW)
   GUIRegisterMsg($WM_NOTIFY, 'WM_NOTIFY')
   While 1
      Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
         GUIDelete($Form3)
         ExitLoop
      EndSwitch
   WEnd
EndFunc

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
   Local $tNMHDR, $hWndFrom, $iIDFrom, $iCode
   #forceref $hWnd, $iMsg, $iwParam, $ilParam
   $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
   $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
   $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
   $iCode = DllStructGetData($tNMHDR, "Code")
   Switch $iCode
   Case $NM_RCLICK
      Switch $hWndFrom
      Case $hWndListView1
         ConsoleWrite('$NM_RCLICK_ListView_1'&@CRLF)
      Case $hWndListView2
         ConsoleWrite('$NM_RCLICK_ListView_2'&@CRLF)
      EndSwitch
   EndSwitch
   Return $GUI_RUNDEFMSG
EndFunc
 
Последнее редактирование:
Автор
P

pvnn

Осваивающий
Сообщения
305
Репутация
32
Спасибо! Помогли разобраться
В функции form2(), переменная
Код:
$hListView1 = GUICtrlCreateListView()
возвращает идентификатор элемента (controlID) = 5
В функции form3(), переменная
Код:
$hListView2 = GUICtrlCreateListView()
возвращает идентификатор элемента (controlID) = 5
То есть controlID у обоих элементов в разных формах одинаковые.
Поэтому в WM_NOTIFY и получаются одинаковые указатели на элементы $hListView1 и $hListView2
Код:
$hWndListView1 = GUICtrlGetHandle($hListView1)
$hWndListView2 = GUICtrlGetHandle($hListView2)

Решение. Получать указатели на элементы необходимо в форме их создания.





Код:
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>

Global $hListView1
Global $hListView2
Global $hWndListView1
Global $hWndListView2


$Form1 = GUICreate("Form1", 245, 126, -1, -1)
$Button1 = GUICtrlCreateButton("Button1", 16, 16, 75, 25)
$Button2 = GUICtrlCreateButton("Button2", 152, 16, 75, 25)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            form2()
        Case $Button2
            form3()
    EndSwitch
WEnd

Func form2()
$Form2 = GUICreate("", 421, 215, -1, -1)
$hListView1 = GUICtrlCreateListView("", 8, 8, 402, 166, BitOR($LVS_REPORT,$LVS_SHOWSELALWAYS))
$hWndListView1 = GUICtrlGetHandle($hListView1)
GUISetState(@SW_SHOW)
GUIRegisterMsg($WM_NOTIFY, 'WM_NOTIFY')
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            GUIDelete($Form2)
            Return
    EndSwitch
WEnd
EndFunc


Func form3()
$Form3 = GUICreate("", 421, 215, -1, -1)
$hListView2 = GUICtrlCreateListView("", 8, 8, 402, 166, BitOR($LVS_REPORT,$LVS_SHOWSELALWAYS))
$hWndListView2 = GUICtrlGetHandle($hListView2)
GUISetState(@SW_SHOW)
GUIRegisterMsg($WM_NOTIFY, 'WM_NOTIFY')
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            GUIDelete($Form3)
            Return
    EndSwitch
WEnd
EndFunc


Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $tNMHDR, $hWndFrom, $iIDFrom, $iCode
    #forceref $hWnd, $iMsg, $iwParam, $ilParam

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")

    ; $Form2
;~     Local $hWndListView1 = $hListView1
;~     If Not IsHWnd($hWndListView1) Then $hWndListView1 = GUICtrlGetHandle($hListView1)
;~     ConsoleWrite('1 - '&$hWndListView1&@CRLF)

    ; $Form3
;~     Local $hWndListView2=$hListView2
;~     If Not IsHWnd($hListView2) Then $hWndListView2 = GUICtrlGetHandle($hListView2)
;~     ConsoleWrite('2 - '&$hWndListView2&@CRLF)

    Switch $hWndFrom
        Case $hWndListView1
            Switch $iCode
                Case $NM_RCLICK
                    ConsoleWrite('$NM_RCLICK_ListView_1'&@CRLF)
            EndSwitch

        Case $hWndListView2
            Switch $iCode
                Case $NM_RCLICK
                    ConsoleWrite('$NM_RCLICK_ListView_2'&@CRLF)
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc
 
Верх