#include <GUIListView.au3>
#include <GuiMenu.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <ButtonConstants.au3>
#include <ListViewConstants.au3>
Opt('GUIOnEventMode', 1)
Global $hGui, $hListView, $hMenu, $aItem, $iCombo
Global $iListView
Global $sDefCombo1 = '111|222', $sDefCombo2 = 'AAA|ZZZ' ,$sDefCombo3 = '12345|00000'
$dir = @ScriptDir&"\test1.ini"
Global $num
Dim $hGui = GUICreate('')
GUISetOnEvent(-3, '_Exit')
$Save=GUICtrlCreateButton('保 存',200,300,80,30)
GUICtrlSetOnEvent($Save, "_save")
$Add=GUICtrlCreateButton('添 加',100,300,80,30)
GUICtrlSetOnEvent($Add, "_Add")
$Ref=GUICtrlCreateButton('刷 新',10,300,80,30)
GUICtrlSetOnEvent($Ref, "_Ref")
_Read()
Func _Read() ;读取ini内容,创建List
Dim $iCombo = GUICtrlCreateCombo('', -100, -100)
Global $iListView = GUICtrlCreateListView('序号|用户名称|密码|性别|修改密码|状态', 2, 2, 386, 258)
Dim $hListView = GUICtrlGetHandle(-1)
_GUICtrlListView_SetExtendedListViewStyle($iListView, $LVS_EX_GRIDLINES + $LVS_EX_FULLROWSELECT)
Global $zd = IniReadSectionNames($dir) ;获取字段数量——读取ini文件中的字段,并返回1维数组变量,$zd[0]是字段总数
If Not @error Then
$skipnum = 0 ;跳过的字段数
For $i = 1 to $zd[0] ;从1开始一直到$zd[0](总数),读取出每个字段名
If $zd[$i] <> 'set' and $zd[$i] <> 'admin' Then ;此处为不创建字段信息为set的内容
$num=$i-$skipnum
;MsgBox(262144,'正常,序号为',$num)
Else
$skipnum = $skipnum +1
;MsgBox(262144,'跳过的数量为',$skipnum)
EndIf
If $zd[$i] <> 'set' and $zd[$i] <> 'admin' Then ;此处为不创建字段信息为set的内容
;读取用户名
;$Lable=IniRead($dir,$zd[$i],'UserName','Error')
$Lable=$zd[$i]
;读取创建密码
$Password=IniRead($dir,$zd[$i],'PassWord','Error')
;读取性别
$UserMode=IniRead($dir,$zd[$i],'UserMode','Error')
If $UserMode = '1' Then
$Mode='男'
Else
$Mode='女'
EndIf
$PwMode=IniRead($dir,$zd[$i],'PwMode','Error')
If $PwMode = '1' Then;
$PW='允许'
Else
$PW='禁止'
EndIf
;自动创建
GUICtrlCreateListViewItem($num&'|'&$Lable&'|'&$Password&'|'&$Mode&'|'&$PW&'|保留', $iListView)
EndIf
Next
GUISetState()
Local $id1 = GUICtrlCreateDummy()
GUICtrlSetOnEvent(-1, '__UdpData')
Local $aAccelKeys[1][2] = [['{Enter}', $id1]]
GUISetAccelerators($aAccelKeys)
GUIRegisterMsg(0x004E, '_WM_NOTIFY') ;$WM_NOTIFY = 0x004E
Else
MsgBox(262144,'','配置文件读取错误',1)
Exit
EndIf
EndFunc
Func _Add() ;添加新用户
$num=$num+1
GUICtrlCreateListViewItem('|新用户|123456|男|允许|保留', $iListView)
EndFunc
Func _save() ;保存至INI文件
;删除 除admin set 的所有字段
$zd = IniReadSectionNames($dir) ;获取字段数量——读取ini文件中的字段,并返回1维数组变量,$zd[0]是字段总数
For $i = 1 to $zd[0] ;从1开始一直到$zd[0],读取出每个字段 的关键字和值
If $zd[$i] <> 'set' and $zd[$i] <> 'admin' Then ;此处为不创建字段信息为set的内容
IniDelete($dir,$zd[$i])
EndIf
Next
;MsgBox(262144,'','检查是否清理完成非admin和set')
;获取表格总行数
Local $lineAll = _GUICtrlListView_GetItemCount($iListView);取得数组的行数
For $ID = 0 To $lineAll - 1
$row = _GUICtrlListView_GetItemTextArray($iListview, $ID) ;获取全部文本和列数,从第一列开始
$UserName=$row[2]
$Password=$row[3]
$UserMode=$row[4]
$PwMode =$row[5]
$DEL =$row[6]
If $DEL = '保留' Then
;如果为保留,则写入ini
If $UserName <> '' Then IniWrite($dir, $UserName,'PassWord',$Password)
If $UserMode = '男' Then
IniWrite($dir, $UserName,'UserMode',1)
Else
IniWrite($dir, $UserName,'UserMode',0)
EndIf
If $PwMode = '允许' Then
IniWrite($dir, $UserName,'PwMode',1)
Else
IniWrite($dir, $UserName,'PwMode',0)
EndIf
EndIf
Next
;~ _Ref() ;刷新
EndFunc
Func _Ref() ;刷新
GUICtrlDelete($iListView)
_Read()
EndFunc
Func __UdpData()
;~
If Not IsArray($aItem) Then Return
Local $sRd = GUICtrlRead($aItem[2]), $sData = ''
_GUICtrlListView_SetItemText($hListView, $aItem[0], $sRd, $aItem[1])
GUICtrlSetPos($aItem[2], -1000, -100)
Dim $aItem = 0
EndFunc ;==>__UdpData
While 1
Sleep(1000)
WEnd
Func _WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg, $iwParam
Local $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
Switch DllStructGetData($tNMHDR, 'hWndFrom')
Case $hListView
Switch DllStructGetData($tNMHDR, 'Code')
Case -2 ;$NM_CLICK
Local $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
Local $index = DllStructGetData($tInfo, 'Index')
Local $SubItem = DllStructGetData($tInfo, 'SubItem')
If IsArray($aItem) Then
_GUICtrlListView_SetItemText($hListView, $aItem[0], GUICtrlRead($iCombo), $aItem[1])
GUICtrlSetPos($iCombo, -1000, -100)
Dim $aItem = 0
EndIf
;弹出菜单
If $index < 0 Then Return 'GUI_RUNDEFMSG'
If $SubItem = 3 Then
Local $context = GUICtrlCreateContextMenu(GUICtrlCreateDummy())
Dim $hMenu = GUICtrlGetHandle($context)
GUICtrlCreateMenuItem('男',$context)
GUICtrlCreateMenuItem('女',$context)
Local $iid = _GUICtrlMenu_TrackPopupMenu($hMenu, $hGui, -1, -1, 1, 1, 2)
If $iid Then _GUICtrlListView_SetItemText($hListView, $index, _GUICtrlMenu_GetItemText($hMenu, $iid, False), 3)
EndIf
If $SubItem = 4 Then
Local $context = GUICtrlCreateContextMenu(GUICtrlCreateDummy())
Dim $hMenu = GUICtrlGetHandle($context)
GUICtrlCreateMenuItem('允许',$context)
GUICtrlCreateMenuItem('禁止',$context)
Local $iid = _GUICtrlMenu_TrackPopupMenu($hMenu, $hGui, -1, -1, 1, 1, 2)
If $iid Then _GUICtrlListView_SetItemText($hListView, $index, _GUICtrlMenu_GetItemText($hMenu, $iid, False), 4)
EndIf
If $SubItem = 5 Then
Local $sName = _GUICtrlListView_GetItemText($hListView, $Index)
If MsgBox(48 + 4, '确认删除', '是否删除用户 "' & $sName & '" ?', 0, $hGUI) = 6 Then
_GUICtrlListView_DeleteItem($hListView, $Index)
IniDelete($dir, $sName)
EndIf
EndIf
;修改框
If Not StringRegExp($SubItem, "^(1|2)[ DISCUZ_CODE_0 ]quot;) Then Return 'GUI_RUNDEFMSG'
Local $aRect = _GUICtrlListView_GetSubItemRect($hListView, $index, $SubItem)
Dim $aItem[2] = [$index, $SubItem]
Local $sDef = $sDefCombo1
If $SubItem = 1 Then $sDef = $sDefCombo2
Local $sData = _GUICtrlListView_GetItemText($hListView, $index, $SubItem)
;GUICtrlSetPos($iCombo, $aRect[0] + 4, $aRect[1] + 32, $aRect[2] - $aRect[0])
GUICtrlSetPos($iCombo, $aRect[0] + 4, $aRect[1] + 4, $aRect[2] - $aRect[0])
GUICtrlSetData($iCombo, '|' & $sData & '|' & $sDef, $sData)
GUICtrlSetState($iCombo, 256)
If $SubItem = 2 Then $sDef = $sDefCombo3
Local $sData = _GUICtrlListView_GetItemText($hListView, $index, $SubItem)
;GUICtrlSetPos($iCombo, $aRect[0] + 4, $aRect[1] + 32, $aRect[2] - $aRect[0])
GUICtrlSetPos($iCombo, $aRect[0] + 4, $aRect[1] + 4, $aRect[2] - $aRect[0])
GUICtrlSetData($iCombo, '|' & $sData & '|' & $sDef, $sData)
GUICtrlSetState($iCombo, 256)
Case -5 ;$NM_CLICK
Case $NM_DBLCLK ; 双击
EndSwitch
EndSwitch
EndFunc ;==>_WM_NOTIFY
Func _Exit()
Exit
EndFunc ;==>_Exit