Listview example

许多实用脚本和封装函数, 可以让您编写脚本更加便捷高效
garry
Posts: 1456
Joined: 22 Dec 2013, 12:50

Listview example

Post by garry » 18 Apr 2014, 13:30

Listview with 2 TABs and 2 Files / modify / ADD / Delete / Search

Code: Select all

MODIFIED=20140605
;- Listview   >>>   ADD_NEW  / MODIFY / DELETE / SEARCH

MainWindowTitle=ListView_Test1
transform,T,chr,09
delim = `,
first1:=0

gosub,test1    ;-- create 2 text-files for 2 ListViews  ( test )

Gui,2:default
Gui,2: Font,CDefault,Fixedsys
Gui,2: Margin, 10, 10

Tabnumber:=1
    gui,2:add, Tab2, x10 y10 w540 h250 gtabchange vTabnumber AltSubmit, Links|Regex

        gui,2:tab, Links
            gui,2:add, listview,x10 y40 w520 h400 grid cWhite backgroundteal hwndLV1 vLV1 gListViewEvents +altsubmit -multi, A|B
            gosub,fill1
            gosub,width1
            Gui,2:add,button, x10  y450 gPrintLV1,Print
            Gui,2:Add, Edit,  x100 y450 w250 gFind vSrch1,
            Gui,2:add,button, x400 y450 gAddNew1,ADD_NEW


        gui,2:tab, Regex
            gui,2:add, listview,x10 y40 w520 h400 grid cWhite backgroundteal hwndLV2 vLV2 gListViewEvents +altsubmit -multi, A|B
            gosub,fill2
            gosub,width1
            Gui,2:add,button, x10 y450 gPrintLV1,Print
            Gui,2:Add, Edit,  x100 y450 w250 gFind vSrch2,
            Gui,2:add,button, x400 y450 gAddNew1,ADD_NEW


gui,2: show,x10 y1 w600 h500,%MainWindowTitle%
gosub,tabchange
RETURN

2Guiclose:
exitapp

width1:
   T1=300
   T2=200
   LV_ModifyCol(1,T1)
   LV_ModifyCol(2,T2)
   ;LV_ModifyCol(2,"Integer")
return

;-------------------------------------------------------------------------------------
tabchange:
 GuiControlGet, Tabnumber
GuiControl,2:Focus,srch%tabnumber%
Return
;-------------------------------------------------------------------------------------





;---------------- SEARCH -------------------
Find:
Gui,2: Submit, Nohide
Gui,2:listview, LV%Tabnumber%
Fx=%A_scriptdir%\test%tabnumber%.txt
src:= % srch%Tabnumber%
if (SRC="")
   {
   goto,Fill%Tabnumber%
   return
   }
LV_Delete()
  loop,read,%fx%
  {
  LR=%A_loopReadLine%
  if SRC<>
     {
     if LR contains %src%
       {
       stringsplit,C,A_LoopReadLine,%delim%
       LV_Add("",C1,C2)
       }
     }
  else
   continue
   }
LV_Modify(LV_GetCount(), "Vis")
if (SRC="")
  goto,Fill%Tabnumber%
return




;------------------- LISTVIEW --------------
ListViewEvents:
Gui,2:default
Gui,2:listview, LV%Tabnumber%

    if(A_GuiEvent == "Normal")
         {
         LV_GetText(C1, A_EventInfo, 1)
         LV_GetText(C2, A_EventInfo, 2)
         }

    if(A_GuiEvent == "DoubleClick")
        {
        LV_GetText(C2, A_EventInfo, 2)
        stringmid,url1,c2,1,7
        stringmid,pth1,c2,2,2
        if ((url1="http://") or (pth1=":\"))
          run,%c2%
        return
        }


    if(A_GuiEvent == "RightClick")
        {
        LV_GetText(C1, A_EventInfo, 1)
        LV_GetText(C2, A_EventInfo, 2)
        gosub,Modify1
        return
        }

    if A_GuiEvent=K
       {
       GetKeyState,state,DEL        ;- << DELETE
       if state=D
         {
         RowNumber:=LV_GetNext()
         LV_Delete(RowNumber)
         gosub,Modify2
         return
        }
      return
      }
RETURN
;--------------------------------------------------------------



;----------------   MODIFY  ----------------
Modify1:
Gui,3: +AlwaysonTop
Gui,3: Font, s10, Verdana
gui, 3:listview, LV%Tabnumber%
    Gui,3:add, edit, w300 h30 vC1, %C1%
    Gui,3:add, edit, w300 h30 vC2, %C2%

    Gui,3: Add,Button,   x12 gACCEPT1 default, Accept
    Gui,3: Add,Button,   x+4 gCANCEL1, Cancel
Gui,3:show,center, LV_Modify
return

accept1:
Gui,2:default
Gui,3:submit,nohide
gui 3:listview, LV%Tabnumber%
RowNumber := LV_GetNext()
c1:= % c1
c2:= % c2
lv_modify(rownumber, "col1" , C1 )
lv_modify(rownumber, "col2" , C2 )
gosub,modify2
Gui,3:destroy
return

cancel1:
3Guiclose:
Gui,3:destroy
return
;-----------------------------------------------------------------


;----------------   ADD NEW  ----------------
ADDNEW1:
Gui,4: +AlwaysonTop
Gui,4: Font, s10, Verdana
Gui,4:listview, LV%Tabnumber%
    Gui,4:add, edit, w300 h30 vC1,
    Gui,4:add, edit, w300 h30 vC2,

    Gui,4: Add,Button,   x12 gACCEPT4 default, Accept
    Gui,4: Add,Button,   x+4 gCANCEL4, Cancel
Gui,4:show,center,Add_NEW
return

accept4:
Gui,2:default
Gui,4:submit,nohide
Gui,4:listview, LV%Tabnumber%
Fx=%A_scriptdir%\test%tabnumber%.txt
Fileappend,%c1%%delim%%c2%`r`n,%fx%
gosub,fill%tabnumber%
Gui,4:destroy
return

cancel4:
4Guiclose:
Gui,4:destroy
return
;-----------------------------------------------------------------



;------------------ FILL ----------------------------
Fill1:
gui,2:listview, listview%Tabnumber%
LV_Delete()
loop,read,%F1%
  {
  LR=%A_loopReadLine%
  if LR=
     continue
          C1 =
          C2 =
         stringsplit,C,LR,%delim%,
   LV_Add("", c1,c2)
  }
LV_ModifyCol(1, "Sort CaseLocale")   ; or "Sort CaseLocale"
LV_Modify(LV_GetCount(), "Vis")      ;scrolls down
return


Fill2:
gui, 2:listview, listview%Tabnumber%
LV_Delete()
loop,read,%F2%
  {
  LR=%A_loopReadLine%
  if LR=
     continue
          C1 =
          C2 =
         stringsplit,C,LR,%delim%,
   LV_Add("", c1,c2)
  }
LV_ModifyCol(1, "Sort CaseLocale")   ; or "Sort CaseLocale"
LV_Modify(LV_GetCount(), "Vis")      ;scrolls down
return


;---------------------------------------------------





;------------------- Modify Text -------------------------
Modify2:
Fx=%A_scriptdir%\test%tabnumber%.txt
ifexist,%fx%
   filedelete,%fx%
ControlGet,AA,List,,SysListView32%tabnumber%,%MainWindowTitle%        ;<< the correct name of listview
if aa<>
 {
 stringreplace,AA,AA,%t%,%delim%,all                                   ;<< replaces TAB with Delimiter
 stringreplace,AA,AA,`n,`r`n,all
 ;msgbox, 262208, ,%aa%
 fileappend,%AA%,%fx%
 aa=
 return
 }
return
;------------------------------------------------------------------





;------------------- PRINT-Listview -------------------------
PrintLv1:
FileTest=%a_scriptdir%\PrintListview55.txt
ifexist,%filetest%
   filedelete,%filetest%
ControlGet,AA,List,,SysListView32%tabnumber%,%MainWindowTitle%        ;<< the correct name of listview
if aa<>
 {
 stringreplace,AA,AA,%t%,%delim%,all                                   ;<< replaces TAB with Delimiter
 stringreplace,AA,AA,`n,`r`n,all
 ;msgbox, 262208, ,%aa%
 fileappend,%AA%,%filetest%
 aa=
 run,%filetest%
 return
 }
return
;------------------------------------------------------------------




;--- create a testfile ---------------
test1:
;delim = `,
F1=%A_scriptdir%\test1.txt
ifnotexist,%f1%
{
  e1=
  (Ltrim Join`r`n
  1-Youtube Monty Python%delim%http://www.youtube.com/MontyPython
  1-Li Xianglan wikipedia%delim%http://en.wikipedia.org/wiki/Li_Xianglan
  )
Fileappend,%e1%`r`n,%f1%
}

F2=%A_scriptdir%\test2.txt
ifnotexist,%f2%
{
  e2=
  (Ltrim Join`r`n
  2-REGEX ahk%delim%http://www.autohotkey.com/docs/misc/RegEx-QuickRef.htm
  2-REGEX regular%delim%http://www.regular-expressions.info/
  2-REGEX regenechsen%delim%http://www.regenechsen.de/phpwcms/index.php
  2-Regex 101%delim%http://regex101.com/
  )
Fileappend,%e2%`r`n,%f2%
}
e1=
e2=
return
;--- end create a testfile -----------

;================== END script ==========================================================


Listview / add / modify / delete-multiple /
some old examples :
http://www.autohotkey.com/board/topic/4 ... rlprogram/

Code: Select all

;-http://www.autohotkey.com/board/topic/4279-listview-select-column-and-start-emailurlprogram/
;--------- LISTVIEW EXAMPLE -------------------------------------------
;-- edit
;   delete multiple marked rows
;   add

;- example creates a subfolder _CSV with 2 text-files
;----------------------------------------------------------------------

modified=20140418
#NoEnv
SendMode Input
SetBatchLines, -1
SetWorkingDir, %A_ScriptDir%
d=`;                               ;- delimiter

gosub,create2Testfiles

Gui,1:Font,S13 CDefault,Lucida Console
Gui,1:Add, ListView,backgroundteal csilver grid r10 x110 y0 w480 +hscroll altsubmit gLW2, Name|URL1
 LV_ModifyCol()
Gui,1:Show,x0 y0 w600 h260,Listview_Example01                               Edit=F12            DELETE=Rightclick
Gui,1:add,Listbox,x5 y10 w100 h200 Sort vLB1 gLB
Loop,%R3C%\*.csv
    {
    FX=%A_LoopFileName%
    stringlen,L1,FX
    stringmid,FA,FX,1,L1-4
    GuiControl,,LB1,%FA%
    }
GuiControl, ChooseString, LB1, EX1                    ;- select EX1 file
;GuiControl, ChooseString, LB1, %fa%                  ;- select last-file
Gui,1:Font,  S10 CDefault , FixedSys
Gui,1:Add,Button,gAddUrl x5 y230 w120 h20,AddUrl
;=============================================================================
LB:
Gui, 1:Default
Gui,1:Submit,nohide
LV_Delete()
LV_ModifyCol(1,220)
LV_ModifyCol(2,220)
F2=%R3C%\%LB1%.csv

loop,read,%F2%
  {
  BX1=
  BX2=
  stringsplit,BX,A_LoopReadLine,%d%,
  LV_Add("",BX1,BX2)
  }
return
;==============================================================
AddUrl:
if LB1=
  {
  msgbox,select first a genre (EX1, EX2 ....)
  return
  }

GuiControl,2:Focus,Name
Gui,2:Font,  S10 CDefault , FixedSys
Gui,2:Add,Text, x1 y5 w80 h20, NAME
Gui,2:Add,Edit,   x80 y5 w500 h20 vA21, %A21%
Gui,2:Add,Text,   x1  y30 w80  h20, URL1
Gui,2:Add,Edit,   x80 y30 w500 h20 vA22, %A22%
Gui,2:Add, Button, x550 y80 w40 h25, OK
Gui,2:Show, x2 y385 w600 h110, URL-INPUT
return
;---------------------------------------
2GuiClose:
2GuiEscape:
Gui,2: Destroy
return

2ButtonOK:
Gui,2:submit
if A21=
  {
  Gui,2: Destroy
  return
  }
FILEAPPEND, %A21%%d%%A22%`r`n,%F2%
Gui,2: Destroy
GoSub, LB         ;update LV
return
;============================================================
LW2:
Gui, Submit,nohide
if A_GuiEvent = Normal
  {
  LV_GetText(C1,A_EventInfo,1)
  LV_GetText(C2,A_EventInfo,2)
  RN:=LV_GetNext("C")
  RF:=LV_GetNext("F")
  GC:=LV_GetCount()
  Goto,SK12
  }

;----------------------------
SK12:
if A_GuiEvent=K

{
GetKeyState,state,F12
if state=D
 {
if LB1=
   {
   msgbox,select first a genre (EX1 or EX2....)
   return
   }

GuiControl,3:Focus,Name
Gui,3:Font,  S10 CDefault , FixedSys
Gui,3:Add,Text, x1 y5 w80 h20, NAME
Gui,3:Add,Edit,  x80 y5 w500 h20 vA31, %C1%
Gui,3:Add,Text,  x1  y30 w80  h20, URL1
Gui,3:Add,Edit,   x80 y30 w500 h20 vA32, %C2%
Gui,3:Add, Button, x550 y80 w40 h25, OK
Gui,3:Show, x2 y385 w600 h100, URL-INPUT
return
;---------------------------------------
3GuiClose:
3GuiEscape:
Gui, 3:Destroy
return

3ButtonOK:
Gui,3:submit
if A31=
   {
   Gui,3: Destroy
   return
   }

FileRead, FileContent, %R3C%\%LB1%.csv
FileDelete, %R3C%\%LB1%.csv
StringReplace, FileContent, FileContent, %C1%%d%%C2%, %A31%%d%%A32%
FileAppend, %FileContent%, %R3C%\%LB1%.csv
GoSub, LB
Goto,3guiclose
 }
}


;============== RUN =========
if A_GuiEvent = DoubleClick
   {
   run,%C2%
   return
   }
;--------- DELETE multiple marked rows ---------------------
if A_GuiEvent = RightClick
{
C1 =
RF = 0
RFL =
Loop
   {
   RF:=LV_GetNext(RF)
   if RF=0
      break
   RFL = %RF%|%RFL%
   LV_GetText(C1_Temp, RF, 1)
   C1 = %C1%`n%C1_Temp%
  }

if C1 !=
 {
   MsgBox, 4, ,Want you really delete %C1% ?
   IfMsgBox,No
      Return
   Else
   {
      Loop, parse, RFL, |
       LV_Delete(A_LoopField)

     filedelete,%F2%
     Loop % LV_GetCount()
        {
        BX1=
        BX2=
        LV_GetText(BX1,A_INDEX,1)
        LV_GetText(BX2,A_INDEX,2)
        fileappend,%BX1%%d%%BX2%`r`n,%F2%
       }

   }
      C1=
      C2=
 }
return
}
return
;------------------------
GuiClose:
ExitApp


;-------------------------------------------------------
create2Testfiles:
R3C=%A_scriptDir%\_CSV
ifnotexist,%R3C%
   FileCreateDir,%R3C%

RM1=%A_scriptDir%\_CSV\EX1.csv
  ifnotexist,%RM1%
   {
   fileappend,Archive_Math%d%http://archives.math.utk.edu/popmath.html`r`n,%RM1%
   fileappend,Archive_org%d%http://www.archive.org`r`n,%RM1%
   fileappend,Archive_Video%d%https://www.youtube.com/channel/UCGp4u0WHLsK8OAxnvwiTyhA`r`n,%RM1%
   fileappend,Humour_Dilbert%d%http://dilbert.com`r`n,%RM1%
   fileappend,Youtube_Sunset%d%http://youtube.googleapis.com/v/XEvoFLbvXMc?start=1512&end=1677&autoplay=1&version=3`r`n,%RM1%
   }

RM2=%A_scriptDir%\_CSV\EX2.csv
  ifnotexist,%RM2%
   {
   fileappend,Autohotkey%d%http://ahkscript.org/boards/index.php`r`n,%RM2%
   fileappend,Puzzle games%d%http://www.chiark.greenend.org.uk/~sgtatham/puzzles/`r`n,%RM2%
   }
return
;=================== end script ============================================
KilliK
Posts: 169
Joined: 10 Mar 2016, 21:19

Re: Listview example

Post by KilliK » 08 Oct 2017, 02:39

thank you very much. it's exactly what I was looking for. it's perfect. thank you.
Post Reply

Return to “脚本函数”