关于LISTVIEW数据更新问题(附上源码,大家别让它卡壳了= =!)
本帖最后由 cutyourchicken 于 2010-8-27 15:21 编辑{:face (319):}各位兄弟,请问你们是如何将一个数组的数据更新到LISTVIEW的ITEM控件的呢?数组数据每隔一秒更新,同时也要更新到ITEM上面。我不想用DELETEITEM,请问兄弟们有什么方法吗?谢谢!假如我的数据例子是:[电脑名称,IP地址,MAC地址,上传速度,下载速度],每台电脑开机就会连上服务端,并发送如上数据。我用stringsplit函数处理成数组。然后用类似ADDITEM的函数添加到ITEM里面,但是到下一秒有新的数据,ITEM更新就开始出问题了。不断添加相同的ITEM,SUBITEM的数据却可以更新,后来我添加判断语句,有相同的ITEM就只更新SUBITEM,当客户端在4台以下更新数据正常,但是当有4台电脑以上连上服务端的时候就还是出现自动添加相同ITEM的情况 。如果兄弟们想看看源代码我会上传。希望兄弟们能帮帮忙修改FOR 中的IF判断可以让数据更新正常{:face (207):}
附上代码
本帖最后由 cutyourchicken 于 2010-8-14 09:31 编辑客户端的:#include <String.au3>
#include <GUIConstantsEx.au3>
If Not FileExists(@ScriptDir&"\"&"Client.ini") Then
MsgBox(0+16,"错误:","配置文件丢失!",20,"")
Exit
EndIf
$SVRIP= IniRead(@ScriptDir&"\"&"Client.ini","Config","Server","")
$Port= IniRead(@ScriptDir&"\"&"Client.ini","Config","Port","")
$_IfTable=_GetIfTable()
Global $_Down=$_IfTable
Global $_UpDate=$_IfTable
TCPStartup()
While 1
Sleep(1000)
_Strat_Time()
WEnd
Func _Strat_Time()
$_IfTable=_GetIfTable()
$r_up= "+"&@ComputerName&","&@IPAddress1&","&"在线"&","&Round(($_IfTable-$_Down)/1024,2)&"KB/S"&","&Round(($_IfTable-$_UpDate)/1024,2)&"KB/S"
$go1=StringToBinary($r_up,2)
$socket= TCPConnect($SVRIP,$Port)
If $socket = -1 Then
MsgBox(0+16,"错误:","通讯失败!")
Exit
EndIf
TCPSend($socket,$go1)
Global $_Down=$_IfTable
Global $_UpDate=$_IfTable
EndFunc
Func _GetIfTable()
$_Return=DllCall("Iphlpapi.dll","long","GetIfTable","ptr",0,"ulong*",0,"int",0)
$_aBuffer=DllStructCreate("byte[" & $_Return & "]")
$_Return=DllCall("Iphlpapi.dll","long","GetIfTable","ptr",DllStructGetPtr($_aBuffer),"ulong*",DllStructGetSize($_aBuffer),"int",0)
$_Number=DllStructCreate("dword",DllStructGetPtr($_aBuffer))
$_Number=DllStructGetData($_Number,1)
$_MIB_IFROW="dword" & _StringRepeat(";wchar;dword;byte;dword;byte",$_Number)
$_MIB_IFTABLE=DllStructCreate($_MIB_IFROW,DllStructGetPtr($_aBuffer))
Dim $_Table
For $i=2 To $_Number * 5 Step 5
$_Table+=DllStructGetData($_MIB_IFTABLE,$i+3,4)
$_Table+=DllStructGetData($_MIB_IFTABLE,$i+3,10)
Next
Return $_Table
EndFunc服务端的:#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListView.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#Include <file.au3>
#Include <Array.au3>
Dim $DealData
Dim$HWD
Dim $DealCom
Dim $DataRip
$Form1_1 = GUICreate("服务端v1.0", 773, 574, 223, 123)
$ListView1 = GUICtrlCreateListView(" 电脑|IP地址|状态|下载速度|上传速度", 18, 8, 737, 497, -1,BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES, $LVS_EX_DOUBLEBUFFER))
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 100)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 200)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 2, 50)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 3, 200)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 4, 200)
_GUICtrlListView_JustifyColumn(GUICtrlGetHandle($ListView1), 1, 2)
_GUICtrlListView_JustifyColumn(GUICtrlGetHandle($ListView1), 2, 2)
_GUICtrlListView_JustifyColumn(GUICtrlGetHandle($ListView1), 3, 2)
$Checkbox1 = GUICtrlCreateCheckbox("开机自动运行", 48, 528, 145, 25)
GUICtrlSetState(-1, $GUI_CHECKED)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
$Checkbox2 = GUICtrlCreateCheckbox("收到消息弹出", 265, 528, 145, 25)
GUICtrlSetState(-1, $GUI_CHECKED)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
$Button1 = GUICtrlCreateButton("设置", 456, 520, 105, 41, -1, BitOR($WS_EX_CLIENTEDGE,$WS_EX_STATICEDGE))
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x008000)
$Button2 = GUICtrlCreateButton("关于", 601, 520, 105, 41, -1, BitOR($WS_EX_CLIENTEDGE,$WS_EX_STATICEDGE))
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x008000)
$zhucaidan = GUICtrlCreateContextMenu($ListView1)
$yuanchengkongzhi = GUICtrlCreateMenuItem(" 远程控制 ", $zhucaidan)
GUICtrlCreateMenuItem("", $zhucaidan)
$yuanchengzhuomian = GUICtrlCreateMenuItem(" 远程桌面 ", $zhucaidan)
GUICtrlCreateMenuItem("", $zhucaidan)
$wenjianchuanshu = GUICtrlCreateMenuItem(" 文件传输 ", $zhucaidan)
GUICtrlCreateMenuItem("", $zhucaidan)
$jinchengchakan= GUICtrlCreateMenuItem(" 进程查看 ", $zhucaidan)
GUICtrlCreateMenuItem("", $zhucaidan)
$jieshujincheng = GUICtrlCreateMenuItem(" 结束进程 ", $zhucaidan)
GUICtrlCreateMenuItem("", $zhucaidan)
$yunxingchengxu = GUICtrlCreateMenuItem(" 运行程序 ", $zhucaidan)
GUICtrlCreateMenuItem("", $zhucaidan)
$fasongxiaoxi = GUICtrlCreateMenuItem(" 发送消息 ", $zhucaidan)
GUISetState(@SW_SHOW)
IfNot FileExists(@ScriptDir&"\"&"Config.ini") Then
MsgBox(0+16,"错误:","配置文件丢失!","30",$Form1_1)
Exit
EndIf
$serverip = IniRead(@ScriptDir&"\"&"Config.ini","Feature","ServerIP","")
$ComPort = IniRead(@ScriptDir&"\"&"Config.ini","Feature","ComPort","")
TCPStartup()
$createskt= TCPListen($serverip,$ComPort)
If $createskt =-1Then
MsgBox(0+16,"错误:","通讯服务启动失败!",30,$Form1_1)
Exit
EndIf
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
Case $Button2
EndSwitch
$accskt= TCPAccept($createskt)
$RecData = TCPRecv($accskt,5000000,1)
If $RecData <> "" Then
$DealData=BinaryToString($RecData,2)
$DealCom=StringSplit($DealData,"+",1)
#cs
_FileWriteLog(@ScriptDir&"\"&"datatransport.log","$DealCom="&$DealCom,1)
_FileWriteLog(@ScriptDir&"\"&"datatransport.log","$DealCom="&$DealCom,1)
_FileWriteLog(@ScriptDir&"\"&"datatransport.log","$DealCom="&$DealCom,1)
#ce
$count= 0
For $Num = 1To $DealCom
$DataRip=StringSplit($DealCom[$Num+1],",",1)
#cs
_FileWriteLog(@ScriptDir&"\"&"datatransport.log","$DealCom[$Num+1]="&$DealCom[$Num+1],1)
#ce
$get=_GUICtrlListView_GetItemText($ListView1, $Num-1)
$getpre = _GUICtrlListView_GetItemText($ListView1, $count+1)
If$get = $DataRip Then
_FileWriteLog(@ScriptDir&"\"&"datatransport.log","1if,OK!",1)
_FileWriteLog(@ScriptDir&"\"&"datatransport.log","$count="&$count,1)
_FileWriteLog(@ScriptDir&"\"&"datatransport.log","$getpre="&$getpre,1)
_FileWriteLog(@ScriptDir&"\"&"datatransport.log","$DataRip"&$DataRip,1)
_FileWriteLog(@ScriptDir&"\"&"datatransport.log","$get="&$get,1)
_GUICtrlListView_SetItemText($ListView1,$count,$DataRip,1)
_GUICtrlListView_SetItemText($ListView1,$count,$DataRip,2)
_GUICtrlListView_SetItemText($ListView1,$count,$DataRip,3)
_GUICtrlListView_SetItemText($ListView1,$count,$DataRip,4)
ElseIf $get ="" And $getpre <> $DataRip Then
_FileWriteLog(@ScriptDir&"\"&"datatransport.log","2if,OK!",1)
_GUICtrlListView_AddItem($ListView1,$DataRip,"","")
_FileWriteLog(@ScriptDir&"\"&"datatransport.log","2ADD,OK!",1)
_GUICtrlListView_AddSubItem($ListView1,$count,$DataRip,1,"")
_GUICtrlListView_AddSubItem($ListView1,$count,$DataRip,2,"")
_GUICtrlListView_AddSubItem($ListView1,$count,$DataRip,3,"")
_GUICtrlListView_AddSubItem($ListView1,$count,$DataRip,4,"")
Else
If $get<>"" And $get <> $DataRipThen
$count+=1
_FileWriteLog(@ScriptDir&"\"&"datatransport.log","3if,OK!",1)
If $get <> $DataRipThen
_GUICtrlListView_AddItem($ListView1,$DataRip,"","")
_FileWriteLog(@ScriptDir&"\"&"datatransport.log","3ADD,OK!",1)
$count+=1
EndIf
_GUICtrlListView_AddSubItem($ListView1,$count,$DataRip,1,"")
_GUICtrlListView_AddSubItem($ListView1,$count,$DataRip,2,"")
_GUICtrlListView_AddSubItem($ListView1,$count,$DataRip,3,"")
_GUICtrlListView_AddSubItem($ListView1,$count,$DataRip,4,"")
ExitLoop
EndIf
EndIf
$count+=1
If $Num = $DealCom-1 Then ExitLoop
Next
EndIf
WEnd 本帖最后由 水木子 于 2010-8-14 08:28 编辑
用这个函数啊!
GUICtrlCreateListViewItem#include <GuiListView.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ListViewConstants.au3>
Opt("TrayMenuMode", 1)
$Path = @ScriptDir & "\config.ini"
GUICreate("ListView", 300, 300)
$ListView1 = GUICtrlCreateListView("账号|密码|", 10, 10, 280, 170)
_GUICtrlListView_SetColumnWidth($ListView1, 0, 130)
_GUICtrlListView_SetColumnWidth($ListView1, 1, 130)
$But1 = GUICtrlCreateButton("添加", 210, 208, 70, 25, $WS_GROUP)
$But2 = GUICtrlCreateButton("删除", 210, 248, 70, 25, $WS_GROUP)
$Name = GUICtrlCreateInput("", 68, 210, 140, 21)
$Pass = GUICtrlCreateInput("", 68, 250, 140, 21)
$Number = GUICtrlCreateGroup("", 8, 185, 284, 105)
GUICtrlCreateLabel("账号:", 20, 210, 48, 24)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
GUICtrlCreateLabel("密码:", 20, 250, 48, 24)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
Read()
GUISetState(@SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case - 3
Exit
Case $But1
Add(GUICtrlRead($Name), GUICtrlRead($Pass))
Case $But2
Del()
EndSwitch
WEnd
Func Read()
_GUICtrlListView_DeleteAllItems($ListView1)
$read = IniReadSection($Path, "config")
If Not @error Then
For $i = 1 To $read
GUICtrlCreateListViewItem($read[$i] & '|' & $read[$i], $ListView1)
Next
GUICtrlSetData($Number, '共有 ' & _GUICtrlListView_GetItemCount($ListView1) & ' 行数据')
EndIf
EndFunc ;==>Read
Func Add($Name, $Pass)
If $Name <> "" And $Pass <> "" Then
IniWrite($Path, "config", $Name, $Pass)
GUICtrlCreateListViewItem($Name & '|' & $Pass, $ListView1)
MsgBox(64, "提示", " 保存成功 " & @CRLF & @CRLF & " 账号: " & $Name & @CRLF & @CRLF & " 密码: " & $Pass)
GUICtrlSetData($Name, "")
GUICtrlSetData($Pass, "")
GUICtrlSetState($Name, $GUI_FOCUS)
GUICtrlSetData($Number, '共有 ' & _GUICtrlListView_GetItemCount($ListView1) & ' 行数据')
EndIf
EndFunc ;==>Add
Func Del()
$sItem = _GUICtrlListView_GetSelectedIndices($ListView1)
$sText = _GUICtrlListView_GetItemTextString($ListView1, Number($sItem))
If Not StringLen($sItem) Then
MsgBox(48, '提示', '请选中需要删除的项目!')
Return
EndIf
$Split = StringSplit($sText, "|")
If MsgBox(32 + 1, "提示", "请确认是否删除以下数据" & @CRLF & @CRLF & "账号:" & $Split & @CRLF & @CRLF & "密码:" & $Split) = 1 Then
_GUICtrlListView_DeleteItemsSelected($ListView1) ;删除选定项目
IniDelete($Path, 'config', $Split)
GUICtrlSetData($Number, '共有 ' & _GUICtrlListView_GetItemCount($ListView1) & ' 行数据')
EndIf
EndFunc ;==>Del 恩,是用这个函数的,效果一样 additem也试过了 不知道水木子兄弟是否做过这样的GUI呢? 如果可以,想麻烦水木子兄弟,写个例子出来{:face (52):} 回复 3# 水木子
可以创建ITEM,但是更新出问题了 回复 7# cutyourchicken
3楼有例子! 回复 9# 水木子
我看过你的例子了,里面用到DELETEITEM,那样效率不高,郁闷!它更新ITEM时候不断添加相同的0索引ITEM,SUBITEM的数据却可以更新,后来我添加判断语句,有相同的ITEM就只更新SUBITEM,当客户端在4台以下更新数据正常,但是当有4台电脑以上连上服务端的时候就还是出现自动添加相同ITEM的情况 。 看来我下午再把源代码放上来好了,希望水木子兄弟和各位高手看看哪里出问题了。 需要判断 如果存在相同的项目,则不需要创建项目,而只需要对项目文本更新。 回复 12# 水木子
是的,我也曾加了一个判断语句,但等有4台以上的电脑连接并发送数据的时候,就又自动添加相同的0索引ITEM了,下午我回去把源代码放上来,麻烦你看看好吗? 我是测试了很多次,从数据发送到传输到处理到处理成数组都一点点确认过了,也查了UDF解决不了,才敢上来提问的,可能我自己钻牛角尖了。顺便问问如何提升阅读权限呢? 顶上去 = =