AHK支持即时搜索的listview的示例

;-- a listview example with 3-columns
;-- ADDNEW -MODIFY -DELETE -SEARCH
;--------------------------------------------------


#NoEnv                        ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input                ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%
SetBatchLines -1
filename1:=%modified% . "ADD-MODIFY-DELETE          Edit=RightClick            DELETE=DEL"

;transform,S,chr,32          ;-- SPACE is the delimiter
;transform,S,chr,165         ;-- ANSI YEN  is the delimiter
S=;                          ;-- COMMA is the delimiter




;--- create a test-file ----
F1=testbasic.txt
ifnotexist,%f1%
{
e4x=
(Ltrim Join`r`n
016%s%CCC%s%1193
9017%s%BBB%s%92
9018%s%AAA%s%1191
)
Fileappend,%e4x%`r`n,%f1%
}
;-----------------------------

T1=80                  ;-- width column-1
T2=100                 ;-- width column-2
T3=70                  ;-- width column-3
tot:=(T1+T2+T3+50)     ;-- width listview
xpos=110
T1A:=(xpos+T1)
T2A:=(xpos+T1+T2)
T3A:=(xpos+T1+T2+T3)

Gui,1:Font,S13 CDefault,Lucida Console
Gui,1:Add, ListView,backgroundteal csilver grid r10 x%xpos% y0 w%tot% +hscroll altsubmit gLW2 vLV_1, A|B|C
;LV_ModifyCol()                               ;-- fit automatic size
LV_ModifyCol(1,T1)                            ;-- size
LV_ModifyCol(2,T2)
LV_ModifyCol(3,T3)

Gui,1:Add,Button,gAddUrl             x5   y230 w120 h20,Add
Gui,1:add,text,   x140 y230 w60 ,Search=
Gui,1:add,edit,   x210 y230 h20 w200 vWordToSearch gSearch
Gui,1:add,edit,   x450 y230 h20 w60 right readonly vIx
Gui,1:Add,Button, x530 y230 h20 w60 gWriteFile,WRITE
Gui,1:Show,x0 y0 w620 h260,%filename1%

gosub,lb
GuiControl,Focus,WordToSearch
return
;-------------- end gui show ------------------




;------------ fill listview columns -----------
LB:
Gui,1:default
LV_Delete()
I=0
loop,read,%F1%
  {
  LR:=A_LoopReadLine
  if LR=
     continue
  ;LR:=RegExReplace(LR,"\x20{2,}"," ")       ;-- allow only on space
  LR=%LR%                                    ;-- remove leading space
  stringsplit,C,LR,%s%                       ;-- split with delimiter
  I++
  LV_Add("",C1,C2,C3)
  }

LV_ModifyCol(1, "Sort")                        ;-- sort column-1
;LV_ModifyCol(2, "SortAsc")                    ;-- sort column-2
;LV_ModifyCol(1, "Logical SortAsc")
LV_ModifyCol(1,"integer")
LV_ModifyCol(3,"integer")
;LV_ModifyCol(2, "AutoHdr")
LV_Modify(LV_GetCount(), "Vis")              ;-- scrolls to last line
GuiControl,1:,ix,%i%
return
;==============================================================


;======== close exit ===
GuiClose:
ExitApp



;=================== search ========================
search:
gui,1:submit,nohide
I=0
LV_Delete()
loop,read,%F1%
  {
  LR:=A_LoopReadLine
  if LR=
     continue
  LR=%LR%                                    ;-- remove leading space
  stringsplit,C,LR,%s%                       ;-- split with delimiter
  If InStr(LR,WordToSearch)
      {
      i++
      LV_Add("",C1,C2,C3)
      }
  }
GuiControl,1:,ix,%i%
return



;============ write what you see to a file  =====================
;--- odlanir  http://www.autohotkey.com/forum/viewtopic.php?p=505196#505196
WriteFile:
clm=0
TempFile=%a_scriptdir%\LV_Print_content.txt
ifexist,%tempfile%
  filedelete,%tempfile%
  Loop % LV_GetCount("Column")
    clm++

loop, % LV_GetCount()
 {
   RowNum := A_Index
   loop, %clm%
      LV_GetText(col%A_Index% , RowNum, a_index)
   FileAppend, % col1 ";" col2 ";" col3 "`r`n", % TempFile
}
Run, % TempFile
return



;================= ADD-NEW ====================================
AddUrl:
Gui,2:Font,  S10 CDefault , FixedSys

Gui,2:Add,Text, x1 y5 w80 h20, C1
Gui,2:Add,Edit,   x80 y5 w500 h20 vA21,

Gui,2:Add,Text,   x1  y30 w80  h20, C2
Gui,2:Add,Edit,   x80 y30 w500 h20 vA22,

Gui,2:Add,Text,   x1  y60 w80  h20, C3
Gui,2:Add,Edit,   x80 y60 w500 h20 vA23,

Gui,2:Add, Button, x550 y90 w40 h25, OK
Gui,2:Show, x2 y385 w600 h120, ADD_NEW
return
;---------------------------------------
2GuiClose:
2GuiEscape:
Gui,2: Destroy
return


2ButtonOK:
Gui,2:submit
FILEAPPEND, %A21%%s%%A22%%s%%A23%`r`n,%F1%
Gui,2: Destroy
GoSub, LB         ;-- update LV
return
;=========== end ADD NEW ==========================================










;============= Listview event ===============================================
LW2:
Gui, Submit,nohide
Gui,1:ListView, LV_1
  RN:=LV_GetNext("C")
  RF:=LV_GetNext("F")
  GC:=LV_GetCount()


;------- which column  LV_1 is the name of Listview variable -----------------
if A_GuiEvent=ColClick
   {
   gosub,cc
   return
   }
;----------------------------------


;------- one-click ----------------
if A_GuiEvent = Normal
  {
  LV_GetText(C1,A_EventInfo,1)
  LV_GetText(C2,A_EventInfo,2)
  LV_GetText(C3,A_EventInfo,3)

  RN:=LV_GetNext("C")
  RF:=LV_GetNext("F")
  GC:=LV_GetCount()
  }


;------- Doubleclick shows only content from clicked column -----
if A_GuiEvent = DoubleClick
   {
   gosub,cc
   LV_GetText(C1,A_EventInfo,columnx)
   msgbox, 262208, ColumnInfo,You have DoubleClicked on`nRow= %A_EventInfo%`nColumn=%columnx%`nHeader=%thisheader%`nContent=%c1%
   return
   }




;------------ EDIT MODIFY LV --------

if A_GuiEvent = RightClick
 {
  LV_GetText(C1,A_EventInfo,1)
  LV_GetText(C2,A_EventInfo,2)
  LV_GetText(C3,A_EventInfo,3)

  RN:=LV_GetNext("C")
  RF:=LV_GetNext("F")
  GC:=LV_GetCount()


GuiControl,3:Focus,Name
Gui,3:Font,  S10 CDefault , FixedSys

Gui,3:Add,Text,  x1  y5 w80 h20, AA
Gui,3:Add,Edit,  x80 y5 w500 h20 vA31, %C1%

Gui,3:Add,Text,  x1  y30 w80  h20, BB
Gui,3:Add,Edit,  x80 y30 w500 h20 vA32, %C2%

Gui,3:Add,Text,  x1  y60 w80  h20, CC
Gui,3:Add,Edit,  x80 y60 w500 h20 vA33, %C3%

Gui,3:Add, Button, x550 y90 w40 h25, OK
Gui,3:Show, x2 y385 w600 h120, MODIFY
return
;----------


3ButtonOK:
Gui,3:submit
FileRead, FileContent, %F1%
FileDelete, %F1%
StringReplace, FileContent, FileContent, %C1%%s%%C2%%s%%C3% , %A31%%s%%A32%%s%%A33%
FileAppend, %FileContent%, %F1%
GoSub, LB

3GuiClose:
3GuiEscape:
Gui, 3:Destroy
return
 }
;------------ END EDIT MODIFY LV --------



;------------ DELETE Listview ROW -------------------
if A_GuiEvent=K
{
GetKeyState,state,DEL
if state=D
  {
   LV_GetText(C1, RF, 1)
   LV_GetText(C2, RF, 2)
   LV_GetText(C3, RF, 3)
   CT=%c1%%s%%c2%%s%%c3%
   msgbox, 262436,Delete,Want you really delete the row %rf%=`n%ct% ?
    IfMsgBox,No
       Return
      FileRead,AA,%F1%
      Filedelete,%F1%
      StringReplace,BB,AA,%ct%`r`n,,
      FileAppend,%BB%,%F1%
  gosub,lb
  return
  }
}
;============= end Listview event ===============================================






;-------------- Function Get Listviewx  Columnx  --------------------------------------------------------
   CC:
   ;---- user jaysp Fri Aug 20, 2010 3:42 pm  Get Listview columnx
   ;-------- http://www.autohotkey.com/forum/viewtopic.php?t=61655&highlight=clicked+column ---
   ;MouseGetPos,x,y,%Filename1%                   ;-- Get mouse position relative to the window

   MouseGetPos X, Y, WinID, Ctrl
   GuiControlGet, LV_1, Pos                      ;-- Get the x coordinate of the listview relative to the window
   scrollpos := DllCall("GetScrollPos", "UInt", ChildHWND, "Int", 0) ;--Get the position of the scrollbar
   highcoord := LV_1X - scrollpos                ;-- Get the relative position on the x axis

   ;-- Get the widths of all columns
   w_index=0
   Loop % LV_GetCount("Column")
      {
      SendMessage, 4125, A_Index - 1, 0, SysListView321 ;-- Gets the width of each cell
      w_index += 1
      columnWidth%w_index% := ErrorLevel
      }

   ;-- Get the relative position of each cell, see if x falls in the range and display the column
   Loop, %w_index%
      {
      highcoord := highcoord + columnWidth%A_Index%
      lowcoord  := highcoord - columnWidth%A_Index%
      If x between %lowcoord% and %highcoord%
          Columnx:=A_index
      LV_GetText( ThisHeader, 0, columnx )       ; Get column header.
      }
return
;========================= end script Listview basics ==========================================

给TA捐赠
共{{data.count}}人
人已捐赠
其他

Autohotkey微知识点全整理

2018-1-18 20:23:35

其他应用

autohotkey Dota辅助

2018-1-19 21:00:38

3 条回复 A文章作者 M管理员
  1. geer

    麻烦作者写完代码后能不能加哪怕一句描述一下代码是用来干什么的,在什么情况下使用。

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索