jycel 发表于 2010-4-29 18:46:41

[已解决]listview中怎样随键盘光标改变而读取相数据

本帖最后由 jycel 于 2010-4-30 18:38 编辑

解决方法:7楼
在listview中我们可以通过鼠标点击一行,并获取相应数据,不知道通过键盘的光标来移动位置从而获取数据呢?

代码如下:#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#Include <GuiListView.au3>
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("listview测试", 288, 243, 401, 193)
$ListView1 = GUICtrlCreateListView("1|2|3|4", 8, 0, 273, 153)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 50)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 50)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 2, 50)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 3, 50)
GUICtrlCreateListViewItem("1|2|3|4", $ListView1)
GUICtrlCreateListViewItem("21|22|23|24", $ListView1)
GUICtrlCreateListViewItem("31|32|33|34", $ListView1)
GUICtrlCreateListViewItem("41|42|43|44", $ListView1)
GUICtrlCreateListViewItem("51|52|53|54", $ListView1)
$Input1 = GUICtrlCreateInput("", 8, 168, 121, 21)
$Input2 = GUICtrlCreateInput("", 160, 168, 121, 21)
$Input3 = GUICtrlCreateInput("", 8, 200, 121, 21)
$Input4 = GUICtrlCreateInput("", 160, 200, 121, 21)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit

        EndSwitch
WEnd
Func WM_NOTIFY($hWndGUI, $MsgID, $WParam, $LParam)
Dim $lyListView1
      Local $tagNMHDR, $Event, $hWndFrom, $IDFrom
      Local $tagNMHDR = DllStructCreate("int;int;int", $LParam)
      If @error Then Return $GUI_RUNDEFMSG
      $IDFrom = DllStructGetData($tagNMHDR, 2)
      $Event = DllStructGetData($tagNMHDR, 3)
      $tagNMHDR = 0
      Switch $IDFrom;选择产生事件的控件

                Case $ListView1

                        Switch $Event; 选择产生的事件

                                                                Case $NM_CLICK ; 左击
                                                                        $Index = _GUICtrlListView_GetSelectedIndices($ListView1)
                                        If Not StringLen($Index) Then; 这里用以判断是否选定了ListViewItem
                                                                                                Return
                                                                                Else
                                                                                        GUICtrlSetData($Input1,_GUICtrlListView_GetItemText($ListView1, Number($Index),0))
                                                                                        GUICtrlSetData($Input2,_GUICtrlListView_GetItemText($ListView1, Number($Index),1))
                                                                                        GUICtrlSetData($Input3,_GUICtrlListView_GetItemText($ListView1, Number($Index),2))
                                                                                        GUICtrlSetData($Input4,_GUICtrlListView_GetItemText($ListView1, Number($Index),3))

                                                                                EndIf
                                                                Case $NM_DBLCLK ; 双击
                                                       
                              Case $NM_RCLICK ; 右击

                        EndSwitch
      EndSwitch

      Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

pusofalse 发表于 2010-4-29 19:43:32

截取LVN_KEYDOWN通知,判断VKey-Code是否为38(Up-arrow)或40(Down-arrow)。
结构定义:hWnd hWndFrom;int IDFrom;int Code;word VKey对于3.3.5.3之前的版本,把word改成short。

jycel 发表于 2010-4-30 00:15:57

我试下看!

jycel 发表于 2010-4-30 16:44:04

我这样改的达到了效果,但是在第一个和最后一个再移动的话就得到的是空值,应该在判断上面再修改下
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#Include <GuiListView.au3>
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("listview测试", 288, 243, 401, 193)
$ListView1 = GUICtrlCreateListView("1|2|3|4", 8, 0, 273, 153)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 50)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 50)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 2, 50)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 3, 50)
GUICtrlCreateListViewItem("1|2|3|4", $ListView1)
GUICtrlCreateListViewItem("21|22|23|24", $ListView1)
GUICtrlCreateListViewItem("31|32|33|34", $ListView1)
GUICtrlCreateListViewItem("41|42|43|44", $ListView1)
GUICtrlCreateListViewItem("51|52|53|54", $ListView1)
$Input1 = GUICtrlCreateInput("", 8, 168, 121, 21)
$Input2 = GUICtrlCreateInput("", 160, 168, 121, 21)
$Input3 = GUICtrlCreateInput("", 8, 200, 121, 21)
$Input4 = GUICtrlCreateInput("", 160, 200, 121, 21)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit

        EndSwitch
WEnd

Func WM_NOTIFY($hWndGUI, $MsgID, $WParam, $LParam)
Dim $lyListView1
      Local $tagNMHDR, $Event, $hWndFrom, $IDFrom
      Local $tagNMHDR = DllStructCreate("int;int;int", $LParam)
      If @error Then Return $GUI_RUNDEFMSG
      $IDFrom = DllStructGetData($tagNMHDR, 2)
      $Event = DllStructGetData($tagNMHDR, 3)
      $tagNMHDR = 0
                ;=============
                $tNMHDR = DllStructCreate($tagNMHDR, $LParam)
                $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
                $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
                $iCode = DllStructGetData($tNMHDR, "Code")       
                ;=============               
      Switch $IDFrom;选择产生事件的控件

                Case $ListView1

                        Switch $Event; 选择产生的事件

                                                                Case $NM_CLICK ; 左击
                                                                        $Index = _GUICtrlListView_GetSelectedIndices($ListView1)
                                        If Not StringLen($Index) Then; 这里用以判断是否选定了ListViewItem
                                                                                                Return
                                                                                Else
                                                                                        GUICtrlSetData($Input1,_GUICtrlListView_GetItemText($ListView1, Number($Index),0))
                                                                                        GUICtrlSetData($Input2,_GUICtrlListView_GetItemText($ListView1, Number($Index),1))
                                                                                        GUICtrlSetData($Input3,_GUICtrlListView_GetItemText($ListView1, Number($Index),2))
                                                                                        GUICtrlSetData($Input4,_GUICtrlListView_GetItemText($ListView1, Number($Index),3))

                                                                                EndIf
                                                                Case $NM_DBLCLK ; 双击
                                                       
                                                Case $NM_RCLICK ; 右击
                                                Case $LVN_KEYDOWN
                                                                        $tInfo = DllStructCreate($tagNMLVKEYDOWN, $LParam);$ilParam)
                                                                        $psx=DllStructGetData($tInfo, "VKey")
                                                                        If $psx="38" Then;向上
                                                                        $Index = _GUICtrlListView_GetSelectedIndices($ListView1)
                                        If Not StringLen($Index) Then; 这里用以判断是否选定了ListViewItem
                                                                                                Return
                                                                                Else
                                                                                        GUICtrlSetData($Input1,_GUICtrlListView_GetItemText($ListView1, Number($Index)-1,0))
                                                                                        GUICtrlSetData($Input2,_GUICtrlListView_GetItemText($ListView1, Number($Index)-1,1))
                                                                                        GUICtrlSetData($Input3,_GUICtrlListView_GetItemText($ListView1, Number($Index)-1,2))
                                                                                        GUICtrlSetData($Input4,_GUICtrlListView_GetItemText($ListView1, Number($Index)-1,3))
                                                                                EndIf
                                                                        ElseIf $psx="40" Then;向下       
                                                                        $Index = _GUICtrlListView_GetSelectedIndices($ListView1)
                                        If Not StringLen($Index) Then; 这里用以判断是否选定了ListViewItem
                                                                                                Return
                                                                                Else
                                                                                        GUICtrlSetData($Input1,_GUICtrlListView_GetItemText($ListView1, Number($Index)+1,0))
                                                                                        GUICtrlSetData($Input2,_GUICtrlListView_GetItemText($ListView1, Number($Index)+1,1))
                                                                                        GUICtrlSetData($Input3,_GUICtrlListView_GetItemText($ListView1, Number($Index)+1,2))
                                                                                        GUICtrlSetData($Input4,_GUICtrlListView_GetItemText($ListView1, Number($Index)+1,3))
                                                                                EndIf
                                                                EndIf                                                                               
                        EndSwitch
      EndSwitch

      Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

大绯狼 发表于 2010-4-30 17:21:00

本帖最后由 大绯狼 于 2010-4-30 17:22 编辑

加个判断就好了 我的是64BIT的系统 按键代码不一样
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#Include <GuiListView.au3>
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("listview测试", 288, 243, 401, 193)
$ListView1 = GUICtrlCreateListView("1|2|3|4", 8, 0, 273, 153)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 50)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 50)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 2, 50)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 3, 50)
GUICtrlCreateListViewItem("1|2|3|4", $ListView1)
GUICtrlCreateListViewItem("21|22|23|24", $ListView1)
GUICtrlCreateListViewItem("31|32|33|34", $ListView1)
GUICtrlCreateListViewItem("41|42|43|44", $ListView1)
GUICtrlCreateListViewItem("51|52|53|54", $ListView1)
$Input1 = GUICtrlCreateInput("", 8, 168, 121, 21)
$Input2 = GUICtrlCreateInput("", 160, 168, 121, 21)
$Input3 = GUICtrlCreateInput("", 8, 200, 121, 21)
$Input4 = GUICtrlCreateInput("", 160, 200, 121, 21)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit

        EndSwitch
WEnd

Func WM_NOTIFY($hWndGUI, $MsgID, $WParam, $LParam)
        Dim $lyListView1
        Local $tagNMHDR, $Event, $hWndFrom, $IDFrom
        Local $tagNMHDR = DllStructCreate("int;int;int", $LParam)
        If @error Then Return $GUI_RUNDEFMSG
        $IDFrom = DllStructGetData($tagNMHDR, 2)
        $Event = DllStructGetData($tagNMHDR, 3)
        $tagNMHDR = 0
        ;=============
        $tNMHDR = DllStructCreate($tagNMHDR, $LParam)
        $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
        $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
        $iCode = DllStructGetData($tNMHDR, "Code")
        ;=============
        Switch $IDFrom;选择产生事件的控件

                Case $ListView1

                        Switch $Event; 选择产生的事件

                                Case $NM_CLICK ; 左击
                                        $Index = _GUICtrlListView_GetSelectedIndices($ListView1)
                                        If Not StringLen($Index) Then; 这里用以判断是否选定了ListViewItem
                                                Return
                                        Else
                                                GUICtrlSetData($Input1, _GUICtrlListView_GetItemText($ListView1, Number($Index), 0))
                                                GUICtrlSetData($Input2, _GUICtrlListView_GetItemText($ListView1, Number($Index), 1))
                                                GUICtrlSetData($Input3, _GUICtrlListView_GetItemText($ListView1, Number($Index), 2))
                                                GUICtrlSetData($Input4, _GUICtrlListView_GetItemText($ListView1, Number($Index), 3))

                                        EndIf
                                Case $NM_DBLCLK ; 双击

                                Case $NM_RCLICK ; 右击
                                Case $LVN_KEYDOWN
                                        $tInfo = DllStructCreate($tagNMLVKEYDOWN, $LParam);$ilParam)
                                        $psx = DllStructGetData($tInfo, "VKey")
;~                                         MsgBox(0,0,$psx)
                                        If $psx = "21495846"Then;向上
                                                $Index = _GUICtrlListView_GetSelectedIndices($ListView1)
                                                If Not StringLen($Index) Then; 这里用以判断是否选定了ListViewItem
                                                        Return
                                                Else
                                                        If Number(_GUICtrlListView_GetSelectedIndices($ListView1)) <> 0 Then
                                                                GUICtrlSetData($Input1, _GUICtrlListView_GetItemText($ListView1, Number($Index) - 1, 0))
                                                                GUICtrlSetData($Input2, _GUICtrlListView_GetItemText($ListView1, Number($Index) - 1, 1))
                                                                GUICtrlSetData($Input3, _GUICtrlListView_GetItemText($ListView1, Number($Index) - 1, 2))
                                                                GUICtrlSetData($Input4, _GUICtrlListView_GetItemText($ListView1, Number($Index) - 1, 3))
                                                        EndIf
                                                EndIf
                                        ElseIf $psx = "22020136"Then;向下
                                                $Index = _GUICtrlListView_GetSelectedIndices($ListView1)
                                                If Not StringLen($Index) Then; 这里用以判断是否选定了ListViewItem
                                                        Return
                                                Else
                                                        If Number(_GUICtrlListView_GetSelectedIndices($ListView1)) <> Number(_GUICtrlListView_GetItemCount($ListView1)) - 1 Then
                                                                GUICtrlSetData($Input1, _GUICtrlListView_GetItemText($ListView1, Number($Index) + 1, 0))
                                                                GUICtrlSetData($Input2, _GUICtrlListView_GetItemText($ListView1, Number($Index) + 1, 1))
                                                                GUICtrlSetData($Input3, _GUICtrlListView_GetItemText($ListView1, Number($Index) + 1, 2))
                                                                GUICtrlSetData($Input4, _GUICtrlListView_GetItemText($ListView1, Number($Index) + 1, 3))
                                                        EndIf
                                                EndIf
                                        EndIf
                        EndSwitch
        EndSwitch

        Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

jycel 发表于 2010-4-30 18:26:05

加个判断就好了 我的是64BIT的系统 按键代码不一样
大绯狼 发表于 2010-4-30 17:21 http://www.autoitx.com/images/common/back.gif


今天论坛怎么这么慢,很久都打不开!现在只是判断的问题,我不知道做得合理不!

jycel 发表于 2010-4-30 18:32:43


#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#Include <GuiListView.au3>
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("listview测试", 288, 243, 401, 193)
$ListView1 = GUICtrlCreateListView("1|2|3|4", 8, 0, 273, 153)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 50)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 50)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 2, 50)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 3, 50)
GUICtrlCreateListViewItem("1|2|3|4", $ListView1)
GUICtrlCreateListViewItem("21|22|23|24", $ListView1)
GUICtrlCreateListViewItem("31|32|33|34", $ListView1)
GUICtrlCreateListViewItem("41|42|43|44", $ListView1)
GUICtrlCreateListViewItem("51|52|53|54", $ListView1)
$Input1 = GUICtrlCreateInput("", 8, 168, 121, 21)
$Input2 = GUICtrlCreateInput("", 160, 168, 121, 21)
$Input3 = GUICtrlCreateInput("", 8, 200, 121, 21)
$Input4 = GUICtrlCreateInput("", 160, 200, 121, 21)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit

        EndSwitch
WEnd

Func WM_NOTIFY($hWndGUI, $MsgID, $WParam, $LParam)
Dim $lyListView1
      Local $tagNMHDR, $Event, $hWndFrom, $IDFrom
      Local $tagNMHDR = DllStructCreate("int;int;int", $LParam)
      If @error Then Return $GUI_RUNDEFMSG
      $IDFrom = DllStructGetData($tagNMHDR, 2)
      $Event = DllStructGetData($tagNMHDR, 3)
      $tagNMHDR = 0
                ;=============
                $tNMHDR = DllStructCreate($tagNMHDR, $LParam)
                $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
                $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
                $iCode = DllStructGetData($tNMHDR, "Code")       
                ;=============               
      Switch $IDFrom;选择产生事件的控件

                Case $ListView1

                        Switch $Event; 选择产生的事件

                                                                Case $NM_CLICK ; 左击
                                                                        $Index = _GUICtrlListView_GetSelectedIndices($ListView1)
                                        If Not StringLen($Index) Then; 这里用以判断是否选定了ListViewItem
                                                                                                Return
                                                                                Else
                                                                                        GUICtrlSetData($Input1,_GUICtrlListView_GetItemText($ListView1, Number($Index),0))
                                                                                        GUICtrlSetData($Input2,_GUICtrlListView_GetItemText($ListView1, Number($Index),1))
                                                                                        GUICtrlSetData($Input3,_GUICtrlListView_GetItemText($ListView1, Number($Index),2))
                                                                                        GUICtrlSetData($Input4,_GUICtrlListView_GetItemText($ListView1, Number($Index),3))

                                                                                EndIf
                                                                Case $NM_DBLCLK ; 双击
                                                       
                                                Case $NM_RCLICK ; 右击
                                                Case $LVN_KEYDOWN
                                                                        $tInfo = DllStructCreate($tagNMLVKEYDOWN, $LParam);$ilParam)
                                                                        $psx=DllStructGetData($tInfo, "VKey")
                                                                        If $psx="38" Then;向上
                                                                        $Index = _GUICtrlListView_GetSelectedIndices($ListView1)
                                        If Not StringLen($Index) Then; 这里用以判断是否选定了ListViewItem
                                                                                                Return
                                                                                        Else
                                                                                                If Number(_GUICtrlListView_GetSelectedIndices($ListView1)) <> 0 Then
                                                                                        GUICtrlSetData($Input1,_GUICtrlListView_GetItemText($ListView1, Number($Index)-1,0))
                                                                                        GUICtrlSetData($Input2,_GUICtrlListView_GetItemText($ListView1, Number($Index)-1,1))
                                                                                        GUICtrlSetData($Input3,_GUICtrlListView_GetItemText($ListView1, Number($Index)-1,2))
                                                                                        GUICtrlSetData($Input4,_GUICtrlListView_GetItemText($ListView1, Number($Index)-1,3))
                                                                                        EndIf
                                                                                EndIf
                                                                        ElseIf $psx="40" Then;向下       
                                                                        $Index = _GUICtrlListView_GetSelectedIndices($ListView1)
                                        If Not StringLen($Index) Then; 这里用以判断是否选定了ListViewItem
                                                                                                Return
                                                                                        Else
                                                                                                If Number(_GUICtrlListView_GetSelectedIndices($ListView1)) <> Number(_GUICtrlListView_GetItemCount($ListView1)) - 1 Then
                                                                                        GUICtrlSetData($Input1,_GUICtrlListView_GetItemText($ListView1, Number($Index)+1,0))
                                                                                        GUICtrlSetData($Input2,_GUICtrlListView_GetItemText($ListView1, Number($Index)+1,1))
                                                                                        GUICtrlSetData($Input3,_GUICtrlListView_GetItemText($ListView1, Number($Index)+1,2))
                                                                                        GUICtrlSetData($Input4,_GUICtrlListView_GetItemText($ListView1, Number($Index)+1,3))
                                                                                        EndIf
                                                                                EndIf
                                                                EndIf                                                                               
                        EndSwitch
      EndSwitch

      Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

damoo 发表于 2014-4-6 17:34:48

在WIN7下不行。
页: [1]
查看完整版本: [已解决]listview中怎样随键盘光标改变而读取相数据