Advertisement
AZJIO

редактирование ListView

Aug 23rd, 2011
1,988
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 5.25 KB | None | 0 0
  1. #include <EditConstants.au3>
  2. #include <GUIConstantsEx.au3>
  3. #include <WindowsConstants.au3>
  4. #include <GuiListView.au3>
  5.  
  6. ; En
  7. $LngTitle = 'Double click the'
  8. $LngCol = 'Col'
  9.  
  10. ; Ru
  11. ; если русская локализация, то русский язык
  12. If @OSLang = 0419 Then
  13.     $LngTitle = 'Двойной клик на элементе'
  14.     $LngCol = 'колонка'
  15. EndIf
  16.  
  17. Opt("GUIOnEventMode", 1)
  18. Global $aElement[2], $hActive, $iInput
  19. Global $iListView, $hListView, $iExit, $iSaveChange
  20.  
  21. $hGUI = GUICreate($LngTitle, 450, 365)
  22. GUISetOnEvent(-3, '_Exit')
  23. $iInput = GUICtrlCreateInput("", 0, 0, 0, 0)
  24. GUICtrlSetState(-1, $GUI_HIDE)
  25. $iListView = GUICtrlCreateListView($LngCol & ' 1|' & $LngCol & ' 2', 5, 5, 220, 330, BitOR($GUI_SS_DEFAULT_LISTVIEW, $LVS_REPORT, $LVS_SHOWSELALWAYS))
  26. $hListView = GUICtrlGetHandle(-1)
  27. ; _GUICtrlListView_SetExtendedListViewStyle ($hListView, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)
  28. _ListView_Random_Fill($iListView)
  29.  
  30. $iListView2 = GUICtrlCreateListView($LngCol & ' 1|' & $LngCol & ' 2', 230, 5, 220, 330, BitOR($GUI_SS_DEFAULT_LISTVIEW, $LVS_REPORT, $LVS_SHOWSELALWAYS))
  31. $hListView2 = GUICtrlGetHandle(-1)
  32. _GUICtrlListView_SetExtendedListViewStyle ($hListView, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)
  33.  
  34. _ListView_Random_Fill($iListView2)
  35.  
  36. GUICtrlCreateButton('Button', 10, 340, 70, 25)
  37.  
  38. $iExit = GUICtrlCreateDummy()
  39. GUICtrlSetOnEvent(-1, "_Exit")
  40. $iSaveChange = GUICtrlCreateDummy()
  41. GUICtrlSetOnEvent(-1, "_SaveChange")
  42. Global $AccelKeys[2][2] = [["{ESC}", $iExit],["{ENTER}", $iSaveChange]]
  43. GUISetAccelerators($AccelKeys)
  44. GUISetState()
  45. GUIRegisterMsg(0x4E, "_WM_NOTIFY")
  46. GUIRegisterMsg($WM_COMMAND, "WM_COMMAND") ; для скрытия поля ввода при потере фокуса.
  47.  
  48. While 1
  49.     Sleep(100000)
  50. WEnd
  51.  
  52. Func _ListView_Random_Fill($iListView)
  53.     Local $item1, $item2
  54.     For $i = 1 To 20
  55.         $item1 = Random(10, 99, 1)
  56.         $item2 = ''
  57.         For $j = 1 To 9
  58.             $item2 &= Chr(Random(65, 90, 1)) ; 192, 255 - Ru
  59.         Next
  60.         GUICtrlCreateListViewItem($item1 & '|' & $item2, $iListView) ; создаём пункты
  61.     Next
  62. EndFunc
  63.  
  64. ; Выводит элемент Input на передний план
  65. Func _GUICtrlListView_EditItem($hWnd, $iIndex, $iSubItem)
  66.     ;funkey 19.02.2010
  67.     If $iIndex < 0 Then Return
  68.     Local $aPos, $aRect, $iSum = 0
  69.     Local $x, $y, $w, $h
  70.     For $i = 0 To $iSubItem - 1
  71.         $iSum += _GUICtrlListView_GetColumnWidth($hWnd, $i)
  72.     Next
  73.     $aRect = _GUICtrlListView_GetItemRect($hWnd, $iIndex)
  74.     $aPos = ControlGetPos($hGUI, "", $hWnd)
  75.     $x = $iSum + $aPos[0] + $aRect[0]
  76.     $y = $aPos[1] + $aRect[1]
  77.     $w = _GUICtrlListView_GetColumnWidth($hWnd, $iSubItem)
  78.     $h = $aRect[3] - $aRect[1]
  79.     GUICtrlSetPos($iInput, $x - 1, $y + 1, $w + 1, $h + 1)
  80.     GUICtrlSetData($iInput, _GUICtrlListView_GetItemText($hWnd, $iIndex, $iSubItem))
  81.     GUICtrlSetState($iInput, $GUI_SHOW)
  82.     GUICtrlSetState($iInput, $GUI_FOCUS)
  83.     $aElement[0] = $iIndex
  84.     $aElement[1] = $iSubItem
  85. EndFunc
  86.  
  87. ; Сохранить изменения редактирования пункта
  88. Func _SaveChange()
  89.     Local $sText = GUICtrlRead($iInput)
  90.     If StringInStr($sText, @CR) Or StringInStr($sText, @LF) Then
  91.         If StringLeft($sText, 1) <> '"' And StringInStr(StringMid($sText, 2, StringLen($sText) - 2), '"') Then $sText = StringReplace($sText, '"', "'")
  92.         $sText = '"' & StringReplace($sText, '"', '') & '"'
  93.     EndIf
  94.     _GUICtrlListView_BeginUpdate($hActive)
  95.     _GUICtrlListView_SetItemText($hActive, $aElement[0], $sText, $aElement[1])
  96.     GUICtrlSetState($iInput, $GUI_HIDE)
  97.     _GUICtrlListView_SetColumnWidth($hActive, $aElement[1], -2) ;$LVSCW_AUTOSIZE_USEHEADER
  98.     _GUICtrlListView_EndUpdate($hActive)
  99.     Return $sText ; возвращаем текст, если требуется его использовать после применения
  100. EndFunc
  101.  
  102. Func _WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
  103.     Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR
  104.     $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
  105.     $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
  106.     $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
  107.     $iCode = DllStructGetData($tNMHDR, "Code")
  108.     Switch $hWndFrom
  109.         Case $hListView, $hListView2
  110.             Switch $iCode
  111.                 Case $LVN_BEGINSCROLL ; прокрутка ListView
  112.                     If $hActive Then
  113.                         $hActive = 0
  114.                         GUICtrlSetState($iInput, $GUI_HIDE)
  115.                         GUICtrlSetData($iInput, '') ; Очищаем поле ввода
  116.                     EndIf
  117.                 Case $NM_DBLCLK ; двойной клик - редактируем пункт ListView
  118.                     Local $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
  119.                     $hActive = $hWndFrom
  120.                     _GUICtrlListView_EditItem($hActive, DllStructGetData($tInfo, "Index"), DllStructGetData($tInfo, "SubItem"))
  121.                     ; _GUICtrlListView_EnsureVisible($hActive, DllStructGetData($tInfo, "Index"), True)
  122.             EndSwitch
  123.     EndSwitch
  124.     Return $GUI_RUNDEFMSG
  125. EndFunc
  126.  
  127. Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
  128.     #forceref $hWnd, $iMsg
  129.     Local $iIDFrom, $iCode
  130.     $iIDFrom = BitAND($iwParam, 0xFFFF) ; младшее слово
  131.     $iCode = BitShift($iwParam, 16) ; старшее слово
  132.     Switch $iIDFrom
  133.         Case $iInput
  134.             Switch $iCode
  135.                 Case $EN_KILLFOCUS
  136.                     GUICtrlSetState($iInput, $GUI_HIDE)
  137.                     GUICtrlSetData($iInput, '') ; Очищаем поле ввода
  138.             EndSwitch
  139.     EndSwitch
  140.     Return $GUI_RUNDEFMSG
  141. EndFunc
  142.  
  143. Func _Exit()
  144.     Exit
  145. EndFunc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement