找回密码
 加入
搜索
查看: 5950|回复: 7

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

  [复制链接]
发表于 2010-4-29 18:46:41 | 显示全部楼层 |阅读模式
本帖最后由 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

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 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。
 楼主| 发表于 2010-4-30 00:15:57 | 显示全部楼层
我试下看!
 楼主| 发表于 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
 楼主| 发表于 2010-4-30 18:26:05 | 显示全部楼层
加个判断就好了 我的是64BIT的系统 按键代码不一样
大绯狼 发表于 2010-4-30 17:21



今天论坛怎么这么慢,很久都打不开!现在只是判断的问题,我不知道做得合理不!
 楼主| 发表于 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
发表于 2014-4-6 17:34:48 | 显示全部楼层
在WIN7下不行。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-9-28 11:13 , Processed in 0.085954 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表