itljl 发表于 2010-7-19 15:15:40

[已解决]能不能不点击就排序listview?

本帖最后由 itljl 于 2010-7-20 12:57 编辑



我想排序时间这一列,经过翻了所有帮助,发现下面这种排序方式比较完美(有图标的话,图标会跟着排序)。但问题是这种排序方式必须要点击时间列才可以排序。
能不能不点击将代码写到循环外,事先就排序呢?

我测试过,将$bSet = 0
                        $nCurCol = $nCol
                        GUICtrlSendMsg($ListView1, $LVM_SETSELECTEDCOLUMN, GUICtrlGetState($ListView1), 0)
                        DllCall("user32.dll", "int", "InvalidateRect", "hwnd", GUICtrlGetHandle($ListView1), "int", 0, "int", 1)放到GUI代码写完之后,无法排序。有兄弟有方法吗?

下面是完整代码。

#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>


#include <GuiListView.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 378, 253, 192, 114)
$ListView1 = GUICtrlCreateListView("", 8, 16, 330, 214)
GUICtrlRegisterListViewSort(-1, "LVSort")
_GUICtrlListView_AddColumn($ListView1, "名字", 100)
_GUICtrlListView_AddColumn($ListView1, "时间", 100)

GUICtrlSendMsg($ListView1, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)

;~ ;定义排序数据
Global $nCurCol = -1
Global $nSortDir = 1
Global $bSet = 0
Global $nCol = -1
Global $B_DESCENDING


GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

GUICtrlCreateListViewItem("中国|2010/06/25", $ListView1)
GUICtrlCreateListViewItem("韩国|2011/06/25", $ListView1)
GUICtrlCreateListViewItem("日本|2012/06/25", $ListView1)
GUICtrlCreateListViewItem("那美克星|2013/06/25", $ListView1)
GUICtrlCreateListViewItem("火星|2014/06/25", $ListView1)

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $listview1
                        $bSet = 0
                        $nCurCol = $nCol
                        GUICtrlSendMsg($ListView1, $LVM_SETSELECTEDCOLUMN, GUICtrlGetState($ListView1), 0)
                        DllCall("user32.dll", "int", "InvalidateRect", "hwnd", GUICtrlGetHandle($ListView1), "int", 0, "int", 1)
        EndSwitch
WEnd

Func LVSort($hWnd, $nItem1, $nItem2, $nColumn)
        Local $nSort, $val1, $val2, $nResult

        ; Switch the sorting direction
        If $nColumn = $nCurCol Then
                If Not $bSet Then
                        $nSortDir = $nSortDir * - 1
                        $bSet = 1
                EndIf
        Else
                $nSortDir = 1
        EndIf
        $nCol = $nColumn

        $val1 = GetSubItemText($hWnd, $nItem1, $nColumn)
        $val2 = GetSubItemText($hWnd, $nItem2, $nColumn)

        ; If it is the 3rd colum (column starts with 0) then compare the dates
        If $nColumn = 2 Then
                $val1 = StringRight($val1, 4) & StringMid($val1, 4, 2) & StringLeft($val1, 2)
                $val2 = StringRight($val2, 4) & StringMid($val2, 4, 2) & StringLeft($val2, 2)
        EndIf

        $nResult = 0 ; No change of item1 and item2 positions

        If $val1 < $val2 Then
                $nResult = -1 ; Put item2 before item1
        ElseIf $val1 > $val2 Then
                $nResult = 1 ; Put item2 behind item1
        EndIf

        $nResult = $nResult * $nSortDir

        Return $nResult
EndFunc   ;==>LVSort


; Retrieve the text of a listview item in a specified column
Func GetSubItemText($nCtrlID, $nItemID, $nColumn)
        Local $stLvfi = DllStructCreate("uint;ptr;int;int;int")
        Local $nIndex, $stBuffer, $stLvi, $sItemText

        DllStructSetData($stLvfi, 1, $LVFI_PARAM)
        DllStructSetData($stLvfi, 3, $nItemID)

        $stBuffer = DllStructCreate("char")

        $nIndex = GUICtrlSendMsg($nCtrlID, $LVM_FINDITEM, -1, DllStructGetPtr($stLvfi));

        $stLvi = DllStructCreate("uint;int;int;uint;uint;ptr;int;int;int;int")

        DllStructSetData($stLvi, 1, $LVIF_TEXT)
        DllStructSetData($stLvi, 2, $nIndex)
        DllStructSetData($stLvi, 3, $nColumn)
        DllStructSetData($stLvi, 6, DllStructGetPtr($stBuffer))
        DllStructSetData($stLvi, 7, 260)

        GUICtrlSendMsg($nCtrlID, $LVM_GETITEMA, 0, DllStructGetPtr($stLvi));

        $sItemText = DllStructGetData($stBuffer, 1)

        $stLvi = 0
        $stLvfi = 0
        $stBuffer = 0

        Return $sItemText
EndFunc   ;==>GetSubItemText

afan 发表于 2010-7-19 17:28:22

本帖最后由 afan 于 2010-7-19 17:31 编辑

回复 6# itljl #include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 378, 253, 192, 114)
$ListView1 = GUICtrlCreateListView("", 8, 16, 330, 214)
_GUICtrlListView_AddColumn($ListView1, "名字", 100)
_GUICtrlListView_AddColumn($ListView1, "时间", 100)
GUICtrlSendMsg($ListView1, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
GUICtrlCreateListViewItem("日本|2012/06/25", $ListView1)
GUICtrlCreateListViewItem("中国|2010/06/25", $ListView1)
GUICtrlCreateListViewItem("韩国|2011/06/25", $ListView1)
GUICtrlCreateListViewItem("那美克星|2013/06/25", $ListView1)
GUICtrlCreateListViewItem("火星|2014/06/25", $ListView1)
_GUICtrlListView_RegisterSortCallBack($ListView1)
If MsgBox(36, "排序", '是否以名字进行排序') = 6 Then
        _GUICtrlListView_SortItems($ListView1, 0);以名字排序
Else
        _GUICtrlListView_SortItems($ListView1, 1);以时间排序
        _GUICtrlListView_SortItems($ListView1, 1);降序
EndIf
While 1
        Switch GUIGetMsg()
                Case $GUI_EVENT_CLOSE
                        ExitLoop
                Case $ListView1
                        _GUICtrlListView_SortItems($ListView1, GUICtrlGetState($ListView1))
        EndSwitch
WEnd
_GUICtrlListView_UnRegisterSortCallBack($ListView1)
GUIDelete()

rikthhpgf2005 发表于 2010-7-19 16:21:09

试了好久。。。不行。{:face (245):}

itljl 发表于 2010-7-19 16:23:27

回复 2# rikthhpgf2005


    谢谢偿试,我也是试了好久,也是不行的。

rikthhpgf2005 发表于 2010-7-19 16:43:28

看看老外写的,可以自动排!!;// #jotify.au3#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;//Author   : =|)arkSprout=
;//Purpose: Spotify Playlist/Album Manager
;//Local Functions:=
;//          ReadTextFile()
;//          AddToListBox($f_Artist = "", $f_Album = "", $f_Link = "", $f_Tag = "", $f_index = -1)
;//          RebuildList($listBox, $cboTags, $tagFilter)
;//          SaveFile()
;//          Trim($strIn)
;//          ShortSpotifyURI($strIn
;//          HTTPSpotifyLink($strIn)
;//Not My Functions:=
;//          MyGUICtrlCreateListViewItem($sText, $nCtrlID, $nIndex)
;//          GetSubItemText($nCtrlID, $nItemID, $nColumn)
;//          LVSort2($hWnd, $nItem1, $nItem2, $nColumn)
;// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
#include <GUIListBox.au3>
#include <ComboConstants.au3>
#include <GUIListView.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
;#include <Debug.au3>
;_DebugSetup ( "debug_jotify" )

;#AutoIt3Wrapper_icon=jotify.ico

Global $artistArray
Global $albumArray
Global $linksArray
Global $tagsArray
Global $arraySearchIndex
Global $file

;// List Box Globals //
Global $nCurCol = -1
Global $nSortDir = 1
Global $bSet = 0
Global $nCol = -1

_Main()
;==>ExitScrpt
      
Func _Main()
Local $returnAdd, $strOut, $arraySize, $strCombo
$nCurCol = -1
$nSortDir = 1
$bSet = 0
$nCol = -1
   
    #Region ### START Koda GUI section ### Form=
;// Form //
      $frm_jotify = GUICreate("jotify", 740, 445, 192, 124, BitOR($WS_CAPTION,$WS_POPUP,$WS_BORDER,$WS_CLIPSIBLINGS))
      
;// Menu //
      $masterMenu = GUICtrlCreateMenu("Ac&tions")
      $mnuImportLinkList = GUICtrlCreateMenuItem("* Import &Link List", $masterMenu)
      $mnuSeparator1 = GUICtrlCreateMenuItem("", $masterMenu)
      $mnuSortItems = GUICtrlCreateMenu("So&rt", $masterMenu)
            $submnuSortArtist = GUICtrlCreateMenuItem("* Sort by &Artist", $mnuSortItems)
            $submnuSortAlbum = GUICtrlCreateMenuItem("* Sort by Al&bum", $mnuSortItems)
            $submnuSortTags = GUICtrlCreateMenuItem("* Sort by &Tags", $mnuSortItems)
      
      $mnuSeparator2 = GUICtrlCreateMenuItem("", $masterMenu)
      $mnuClipHTTP_Link = GUICtrlCreateMenuItem("Copy Spotify UR&L To Clipboard", $masterMenu)
      $mnuClipSpotyLink = GUICtrlCreateMenuItem("Copy Spotify UR&I To Clipboard", $masterMenu)
      $mnuSeparator3 = GUICtrlCreateMenuItem("", $masterMenu)
      $mnuClose = GUICtrlCreateMenuItem("&Close", $masterMenu)

;// List Box //
      $listBox = GUICtrlCreateListView("Artist|Album|Spotify Link",112, 24, 610, 350)
      GUICtrlRegisterListViewSort(-1, "LVSort2"); Register the function "SortLV" for the sorting callback

;// Combo Box //
      $cboTags = GUICtrlCreateCombo("", 112,389, 225, 25)
      GUICtrlSetFont(-1, 10, 800, 0, "Arial")

;// Buttons //
      $cmdOpen = GUICtrlCreateButton("&Open Link", 16, 32, 81, 41, $BS_DEFPUSHBUTTON)
      GUICtrlSetFont(-1, 10, 800, 0, "Arial")
      $cmdEdit = GUICtrlCreateButton("&Edit", 16, 88, 81, 41, 0)
      GUICtrlSetFont(-1, 10, 800, 0, "Arial")
      $cmdAdd = GUICtrlCreateButton("&Add", 16, 144, 81, 41, 0)
      GUICtrlSetFont(-1, 10, 800, 0, "Arial")
      $cmdDelete = GUICtrlCreateButton("&Delete", 16, 200, 81, 41, 0)
      GUICtrlSetFont(-1, 10, 800, 0, "Arial")
      GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

;// Load in text file //
    ReadTextFile()
   
;// Fill list box //
    For $x = 1 to UBound($artistArray) -1
      MyGUICtrlCreateListViewItem( $artistArray[$x] & "|" & $albumArray[$x] & "|" & $linksArray[$x], $listBox, -1)
    Next
   
;// Fill ComboBox //
    GUICtrlSetData($cboTags, "All" & BuildTagList($tagsArray), "All")

;// Main Loop | Wait for button press //
Do
    $msg = GUIGetMsg()
;If $msg <> 0 Then _DebugOut($nMsg,1);\\ Track Button Presses
    Select
      Case $msg = $cboTags;// Filter listBox based on Tag in ComboBox
            RebuildList($listBox, $cboTags, GUICtrlRead($cboTags))
      
      Case $msg = $cmdOpen
            $arraySearchIndex = _ArraySearch($linksArray, GetSubItemText($listBox,GUICtrlRead($listBox),2))
            If $arraySearchIndex > 0 Then
                ShellExecute("spotify:" & $linksArray[$arraySearchIndex])
            EndIf
            
      Case $msg = $cmdEdit;// Edit Selection
            $arraySearchIndex = _ArraySearch($linksArray, GetSubItemText($listBox,GUICtrlRead($listBox),2))
            If $arraySearchIndex > 0 Then
                $returnAdd = AddToListBox($artistArray[$arraySearchIndex],$albumArray[$arraySearchIndex], _
                  $linksArray[$arraySearchIndex], $tagsArray[$arraySearchIndex], $arraySearchIndex)
                If $returnAdd <> "" Then
                  GUICtrlSetData($listBox, $returnAdd)
                  RebuildList($listBox, $cboTags, "All")
                EndIf
            EndIf               
               
      Case $msg = $cmdAdd;// Add to list box
            If AddToListBox() = True Then
                RebuildList($listBox, $cboTags, "All")
            EndIf
            
      Case $msg = $cmdDelete;// Delete Selection
            $arraySearchIndex = _ArraySearch($linksArray, GetSubItemText($listBox,GUICtrlRead($listBox),2))
            If $arraySearchIndex > 0 Then
                If MsgBox(292, "jotify ~ Question", "Are you sure you wish to delete:" & @CRLF & @CRLF & _
                        $artistArray[$arraySearchIndex] & " - " & $albumArray[$arraySearchIndex] & "?") = 6 Then
                  _ArrayDelete($artistArray,$arraySearchIndex)
                  _ArrayDelete($albumArray,$arraySearchIndex)
                  _ArrayDelete($linksArray,$arraySearchIndex)
                  _ArrayDelete($tagsArray, $arraySearchIndex)
                  RebuildList($listBox, $cboTags, "All")
                EndIf
            EndIf
      Case $msg = $listBox
            ContinueCase

;// Menu //
      Case $msg = $mnuClipHTTP_Link;// Copy HTTTP (hyperlink) version of Spotify Link to the Clipboard
            $arraySearchIndex = _ArraySearch($linksArray, GetSubItemText($listBox,GUICtrlRead($listBox),2))
            If $arraySearchIndex > 0 Then
                ClipPut(HTTPSpotifyLink($linksArray[$arraySearchIndex]))
            EndIf
            
      Case $msg = $mnuClipSpotyLink;// Copy Spotify Link {URI) to the Clipboard
            $arraySearchIndex = _ArraySearch($linksArray, GetSubItemText($listBox,GUICtrlRead($listBox),2))
            If $arraySearchIndex > 0 Then
                ClipPut("Spotify:" & ShortSpotifyURI($linksArray[$arraySearchIndex]))
            EndIf
            
      Case $msg = $mnuClose;// Close Form Requested
            #cs ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
                This is a Bug I Can't Fix
                If ListBox has a selection value of 14 then is called
                $mnuClose - has a value of 14 ???
            #ce ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
            If GUICtrlRead($listBox) = 14 Then ContinueCase
            ExitLoop
    EndSelect
Until $msg = $GUI_EVENT_CLOSE

GUIDelete()
SaveFile();// Save Arrays
EndFunc;==>_Main

Func ReadTextFile()
Dim $file
Dim $iMsgBoxAnswer
Dim $loopCount = 1
Dim $splitArray
   
    If Not FileExists("jotify_database.jtb") Then
;// File Missing | Create New? //
      $iMsgBoxAnswer = MsgBox(292, "jotify ~ Question","Database File Not Found!" & @CRLF & @CRLF & "Do you wish to create a new one?")
      Select
         Case $iMsgBoxAnswer = 6;// Yes
                $file = FileOpen("jotify_database.jtb", 10);// Create
         Case Else
                Exit
      EndSelect
      
    Else
;// File Exists //
      $file = FileOpen("jotify_database.jtb", 0)

      While 1
            $line = FileReadLine($file)
            If @error = -1 Then ExitLoop
            $splitArray = StringSplit($line,"|")
            
    ;// Add to arrays //
            _ArrayAdd($artistArray,Trim($splitArray))
            _ArrayAdd($albumArray, Trim($splitArray))
            _ArrayAdd($linksArray, Trim($splitArray))
            _ArrayAdd($tagsArray, Trim($splitArray))
      Wend

      FileClose($file)
    EndIf

EndFunc;==>ReadTextFile

Func AddToListBox($f_Artist = "", $f_Album = "", $f_Link = "", $f_Tag = "", $f_index = -1)
Dim $blComplete
Dim $artist
Dim $album
Dim $link
Dim $tag

$blComplete = False

#Region ### START Koda GUI section ### Form=
    $frmInput = GUICreate("Spotify Data Input", 435, 210, 286, 224, BitOR($WS_EX_TOPMOST,$WS_CAPTION,$WS_POPUP,$WS_BORDER,$WS_CLIPSIBLINGS))
    $artistInput = GUICtrlCreateInput($f_Artist, 157, 32, 250, 26)
    GUICtrlSetFont(-1, 12, 400, 0, "Arial")
    $Label1 = GUICtrlCreateLabel("Artist", 37, 32, 44, 23)
    GUICtrlSetFont(-1, 12, 800, 0, "Arial")
    GUICtrlSetColor(-1, 0x008000)
    $InputAlbum = GUICtrlCreateInput($f_Album, 157, 65, 250, 26)
    GUICtrlSetFont(-1, 12, 400, 0, "Arial")
    $Label2 = GUICtrlCreateLabel("Album", 37, 65, 53, 23)
    GUICtrlSetFont(-1, 12, 800, 0, "Arial")
    GUICtrlSetColor(-1, 0x008000)
    $inputLink = GUICtrlCreateInput($f_Link, 157, 98, 250, 26)
    GUICtrlSetFont(-1, 12, 400, 0, "Arial")
    $Label3 = GUICtrlCreateLabel("Spotify Link", 35, 98, 95, 23)
    GUICtrlSetFont(-1, 12, 800, 0, "Arial")
    GUICtrlSetColor(-1, 0x008000)
    $inputTags = GUICtrlCreateInput($f_Tag, 157, 131, 250, 26)
    GUICtrlSetFont(-1, 12, 400, 0, "Arial")
    GUICtrlSetTip(-1,'Seperate by commas for mutible tags ~ e.g. Dance, 1980s, Electro')
    $Label4 = GUICtrlCreateLabel("Tag", 35, 131, 95, 23)
    GUICtrlSetFont(-1, 12, 800, 0, "Arial")
    GUICtrlSetColor(-1, 0x008000)
    $cmdCancel = GUICtrlCreateButton("&Cancel", 101, 164, 97, 33, $BS_DEFPUSHBUTTON )
    GUICtrlSetFont(-1, 10, 800, 0, "Arial")
    $cmdDone = GUICtrlCreateButton("&Done", 245, 164, 97, 33, 0)
    GUICtrlSetFont(-1, 10, 800, 0, "Arial")
    GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsgInput = GUIGetMsg()
    Switch $nMsgInput
      Case $cmdCancel
            $blComplete = False
            ExitLoop
      Case $cmdDone
            $blComplete = True
            ExitLoop
      Case $inputLink
            GUICtrlSetData($inputLink, ShortSpotifyURI(GUICtrlRead($inputLink)))
    EndSwitch
WEnd

;// Store values before form closes //
$artist = GUICtrlRead($artistInput)
$album = GUICtrlRead($InputAlbum)
$link = GUICtrlRead($inputLink)
$tag = GUICtrlRead($inputTags)

;// Close Form //
GUIDelete()

;// Return value | insert into array //
If $blComplete = True Then
    If $f_index = -1 Then
      _ArrayAdd($artistArray, $artist);// Add
      _ArrayAdd($albumArray, $album)
      _ArrayAdd($linksArray, $link)
      _ArrayAdd($tagsArray, $tag)
    Else
      $artistArray[$f_index] = $artist;// Insert
      $albumArray[$f_index] = $album
      $linksArray[$f_index] = $link
      $tagsArray[$f_index] = $tag
    EndIf
EndIf

Return $blComplete
EndFunc;==>AddToListBox

;// Delete and rebuild listbox with selected items //
Func RebuildList($listBox, $cboTags, $tagFilter)
Dim $strCombo

    GUICtrlDelete($listBox)
    GUICtrlDelete($cboTags)

;// Make List Box //
    $listBox = GUICtrlCreateListView("Artist|Album|Spotify Link",112, 24, 610, 350, -1)
    GUICtrlRegisterListViewSort(-1, "LVSort2");// Register the function "SortLV" for the sorting callback
   
;// Fill List Box | filtered by Tag //
    For $x = 1 to UBound($artistArray) -1
      If $tagFilter = "All" Or StringInStr($tagsArray[$x], $tagFilter, 0) > 0 Then
            MyGUICtrlCreateListViewItem( $artistArray[$x] & "|" & $albumArray[$x] & "|" & $linksArray[$x], $listBox, -1)
      EndIf
    Next

;// Combo Box //
    $cboTags = GUICtrlCreateCombo("", 112,389, 225, 25)
    GUICtrlSetFont(-1, 10, 800, 0, "Arial")
    GUICtrlSetData(-1, "All" & BuildTagList($tagsArray), $tagFilter)
EndFunc;==>RebuildList

;// Write back array contents //
Func SaveFile()
Dim $x
    $file = FileOpen("jotify_database.jtb", 10);// Overwrite
   
    For $x = 1 to UBound($artistArray)-1
      If StringLen(Trim($artistArray[$x])) > 0 Then
            FileWriteLine($file, $artistArray[$x] & "|" & $albumArray[$x] & "|" & $linksArray[$x] & "|" & $tagsArray[$x] & @CRLF)
      EndIf
    Next
    FileClose($file)

EndFunc;==>SaveFile

;// Build Unique List of all Tags | Buggy as hell not sorting Unique yet! //
Func BuildTagList($arrayIn)
Dim $returnString
$tagsUniqueArray = _ArrayUnique($arrayIn)

For $x = 1 To UBound($tagsUniqueArray) - 1
    $splitArray = StringSplit($tagsUniqueArray[$x],",")
    For $y = 1 To UBound($splitArray) - 1
      $returnString = $returnString & Trim($splitArray[$y]) & "|"
    Next
Next

Return $returnString
EndFunc;==>BuildTagList

;// Strip Leading and Trailing White Space //
Func Trim($strIn)
    return StringStripWS(StringStripWS($strIn, 1), 2)
EndFunc;==>Trim

Func ShortSpotifyURI($strIn)
;// Shorten URI //
    $strIn = StringReplace($strIn,"http://open.spotify.com/","")
    $strIn = StringReplace($strIn,"/",":")
    $strIn = StringReplace($strIn,"spotify:","")
    Return $strIn
EndFunc;==>ShortSpotifyURI

Func HTTPSpotifyLink($strIn)
;// Create HTTP hyperlink //
    $strIn = StringReplace($strIn,"http://open.spotify.com/","")
    $strIn = StringReplace($strIn,"spotify:","")
    $strIn = StringReplace($strIn,":","/")
    $strIn = "http://open.spotify.com/" & $strIn
    Return $strIn
EndFunc;==>HTTPSpotifyLink

;// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GUI CONTROLS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
;// Create and insert items directly into the listview //
Func MyGUICtrlCreateListViewItem($sText, $nCtrlID, $nIndex)
    Local $stLvItem = DllStructCreate("uint;int;int;uint;uint;ptr;int;int;int;int;")
    Local $stText = DllStructCreate("char")
    Local $arText = StringSplit($sText, "|")
   
    If $nIndex = -1 Then $nIndex = GUICtrlSendMsg($nCtrlID, $LVM_GETITEMCOUNT, 0, 0)
   
    DllStructSetData($stText, 1, $arText);// Save the item text in the struct
   
    DllStructSetData($stLvItem, 1, BitOR($LVIF_TEXT, $LVIF_PARAM))
    DllStructSetData($stLvItem, 2, $nIndex)
    DllStructSetData($stLvItem, 6, DllStructGetPtr($stText))
;// Set the lParam of the struct to the line index - unique within the listview //
    DllStructSetData($stLvItem, 9, $nIndex)
   
    $nIndex = GUICtrlSendMsg($nCtrlID, $LVM_INSERTITEMA, 0, DllStructGetPtr($stLvItem))

    If $nIndex > -1 Then
;// Insert now the rest of the column text //
      For $i = 2 To $arText
            DllStructSetData($stText, 1, $arText[$i])
            DllStructSetData($stLvItem, 3, $i - 1);// Store the subitem index
            
            GUICtrlSendMsg($nCtrlID, $LVM_SETITEMTEXTA, $nIndex, DllStructGetPtr($stLvItem))
      Next
    EndIf

    $stText = 0
    $stLvItem = 0

;// Change the column width to fit the item text //
    For $i = 0 To 2
      GUICtrlSendMsg($nCtrlID, $LVM_SETCOLUMNWIDTH, $i, -1)
      GUICtrlSendMsg($nCtrlID, $LVM_SETCOLUMNWIDTH, $i, -2)
    Next
EndFunc ;==>MyGUICtrlCreateListViewItem

;// Retrieve the text of a listview item in a specified column //
Func GetSubItemText($nCtrlID, $nItemID, $nColumn)
    Local $stLvfi = DllStructCreate("uint;ptr;int;int;int")
    Local $nIndex, $stBuffer, $stLvi, $sItemText
   
    DllStructSetData($stLvfi, 1, $LVFI_PARAM)
    DllStructSetData($stLvfi, 3, $nItemID)

    $stBuffer = DllStructCreate("char")
   
    $nIndex = GUICtrlSendMsg($nCtrlID, $LVM_FINDITEM, -1, DllStructGetPtr($stLvfi));
   
    $stLvi = DllStructCreate("uint;int;int;uint;uint;ptr;int;int;int;int")
   
    DllStructSetData($stLvi, 1, $LVIF_TEXT)
    DllStructSetData($stLvi, 2, $nIndex)
    DllStructSetData($stLvi, 3, $nColumn)
    DllStructSetData($stLvi, 6, DllStructGetPtr($stBuffer))
    DllStructSetData($stLvi, 7, 260)

    GUICtrlSendMsg($nCtrlID, $LVM_GETITEMA, 0, DllStructGetPtr($stLvi));

    $sItemText = DllStructGetData($stBuffer, 1)

    $stLvi = 0
    $stLvfi = 0
    $stBuffer = 0
   
    Return $sItemText
EndFunc ;==>GetSubItemText

;// Our sorting callback funtion //
Func LVSort2($hWnd, $nItem1, $nItem2, $nColumn)
    Local $nSort, $val1, $val2, $nResult
   
;// Switch the sorting direction //
    If $nColumn = $nCurCol Then
      If Not $bSet Then
            $nSortDir = $nSortDir * - 1
            $bSet = 1
      EndIf
    Else
      $nSortDir = 1
    EndIf
    $nCol = $nColumn
   
    $val1 = GetSubItemText($hWnd, $nItem1, $nColumn)
    $val2 = GetSubItemText($hWnd, $nItem2, $nColumn)

;// If it is the 3rd colum (column starts with 0) then compare the dates //
    If $nColumn = 2 Then
      $val1 = StringRight($val1, 4) & StringMid($val1, 4, 2) & StringLeft($val1, 2)
      $val2 = StringRight($val2, 4) & StringMid($val2, 4, 2) & StringLeft($val2, 2)
    EndIf
   
    $nResult = 0;// No change of item1 and item2 positions
   
    If $val1 < $val2 Then
      $nResult = -1;// Put item2 before item1
    ElseIf $val1 > $val2 Then
      $nResult = 1;// Put item2 behind item1
    EndIf

    $nResult = $nResult * $nSortDir
   
    Return $nResult
EndFunc ;==>LVSort2

3mile 发表于 2010-7-19 16:59:34

只是排序吗?#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>


#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 378, 253, 192, 114)
$ListView1 = GUICtrlCreateListView("", 8, 16, 330, 214)
_GUICtrlListView_AddColumn($ListView1, "名字", 100)
_GUICtrlListView_AddColumn($ListView1, "时间", 100)

GUICtrlSendMsg($ListView1, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
GUICtrlCreateListViewItem("日本|2012/06/25", $ListView1)
GUICtrlCreateListViewItem("中国|2010/06/25", $ListView1)
GUICtrlCreateListViewItem("韩国|2011/06/25", $ListView1)
GUICtrlCreateListViewItem("那美克星|2013/06/25", $ListView1)
GUICtrlCreateListViewItem("火星|2014/06/25", $ListView1)
If MsgBox(36, "排序", '是否以名字进行排序') = 6 Then
        _GUICtrlListView_RegisterSortCallBack($ListView1)
        _GUICtrlListView_SortItems($ListView1, 0);以名字排序
        _GUICtrlListView_UnRegisterSortCallBack($ListView1)
Else
        _GUICtrlListView_RegisterSortCallBack($ListView1)
        _GUICtrlListView_SortItems($ListView1, 1);以时间排序
        _GUICtrlListView_UnRegisterSortCallBack($ListView1)
EndIf
While 1
        Switch GUIGetMsg()
                Case $GUI_EVENT_CLOSE
                        ExitLoop
                Case $ListView1

        EndSwitch
WEnd
GUIDelete()

itljl 发表于 2010-7-19 17:08:10

只是排序吗?
3mile 发表于 2010-7-19 16:59 http://www.autoitx.com/images/common/back.gif


    谢谢,不错,只能升序排列,不能降序?

itljl 发表于 2010-7-19 18:11:52

回复 7# afan


    谢谢,我刚才失败是因为。
每次都用了。
_GUICtrlListView_RegisterSortCallBack($ListView1)
谢谢。

pusofalse 发表于 2011-3-5 18:37:53

给ListView加上LVS_SORTASCENDING或LVS_SORTDESCENDING风格,自动排序。

qq82015930 发表于 2011-3-5 21:41:26

值得学习.{:face (332):}

liyi-softs 发表于 2011-9-24 15:50:55

本帖最后由 liyi-softs 于 2011-9-24 15:52 编辑

afan 发表于 2010-7-19 17:28 http://www.autoitx.com/images/common/back.gif




一千条以上数据后.很慢......

apibits 发表于 2012-6-2 14:20:10

这个看过之后也学了不少东西~

CHEARV 发表于 2013-10-10 22:58:47

回复itljl
afan 发表于 2010-7-19 17:28 http://www.autoitx.com/images/common/back.gif


   代码非常简洁,不过,如果数据过多,排序就特别慢了。
页: [1]
查看完整版本: [已解决]能不能不点击就排序listview?