Lotusnut >> Soft >> Visual Basic >> ListView1

Visual Basic 6.0

  • Option Explicit
  • Function dfLvwDB(ByVal ctrl As Variant, ByVal strPath As String) As Integer
  • ' 作  者(Writer) : lotusnut
  • ' 目  的(Aim) : リストビューに strPath内のフォルダー・ファイルを取り込む
  • ' 入  力(Input) : リストビューコントロール・フォルダーパス=Node.Key
  • ' 戻  値(Return) : フォルダー・ファイル数
  • ' 注  釈(Notes) : このプロシージャはツリービューコントロール専用です
  • ' 使用方法(Usage) : Count = dfLvwDB(Ctrl,path)
  • ' 履  歴(History) : Ver 0.0.0 2001/12/12 Original
  • 'システム関数のラップ
  • On Error GoTo ErrTrap
  • 'ファイルスクリプトの宣言
  • Dim FileSysObj As Scripting.FileSystemObject
  • Dim ScrFolder As Folder
  • Dim ScrFile As File
  • Dim ScrFiles As Files
  • 'ローカル変数の宣言
  • Dim itm As ListItem
  • Dim cvt As New clsVntTo
  • Dim lngcount As Long
  • Dim lngsize As Long
  • Dim lngresult As Long
  • Dim l As Long
  • '引数の検証&変換
  • strPath = cvt.dfVntToTrimStr(strPath)
  • If strPath = "" Then Exit Function
  • If Not TypeOf ctrl Is ListView Then
      Debug.Print "sfLvwSetColumリストビューコントロールが不正です" & _
         Err.Description
         Exit Function
    End If
  • '***** プロシージャ処理の実行 *****
  • If strPath = "Root" Or strPath = "Mycom" Then Exit Function
  • 'マウスアイコンを砂時計にし再描画を抑止
  • Screen.MousePointer = vbHourglass
  • lngresult = SendMessage(ctrl.hWnd, _
           WM_SETREDRAW, _
           0, ByVal CLng(0))
  • 'リストのクリア
  • ctrl.ListItems.Clear
  • 'フォルダー内のファイルを取得
  • Set FileSysObj = CreateObject("Scripting.FileSystemObject")
  • Set ScrFolder = FileSysObj.GetFolder(strPath)
  • Set ScrFiles = ScrFolder.Files
  • 'オブジェクト数を確認
  • lngcount = ScrFiles.count
    If lngcount = 0 Then
      Set itm = ctrl.ListItems.Add(, , "Not find File")
    Else
      For Each ScrFile In ScrFiles
        l = l + 1
        lngsize = Round(ScrFile.Size / 1024)
        If lngsize = 0 And ScrFile.Size <> 0 Then lngsize = 1

        
  •     'オブジェクトの設定    
  •     Set itm = ctrl.ListItems.Add(, , ScrFile.Name)
        itm.SubItems(1) = Format(lngsize, "###,###,##0") & " KB"
        itm.SubItems(2) = ScrFile.Type
        itm.SubItems(3) = Left(ScrFile.DateLastModified, 16)
        If ScrFile.Attributes And vbSystem Then itm.Ghosted = False
      Next
    End If
  • 'オブジェクトの開放
  • Set FileSysObj = Nothing
    Set ScrFolder = Nothing
    Set ScrFiles = Nothing

    'フォルダー&ファイルの総数を返す
    dfLvwDB = lngcount

    '表示の復活
    Screen.MousePointer = vbDefault
    lngresult = SendMessage(ctrl.hWnd, _
                 WM_SETREDRAW, _
                 1, ByVal CLng(0))
    Exit Function

    'エラートラップ
  • ErrTrap:
  •    Debug.Print "dfLvwDBプロシージャエラーです : " & Err.Description
       On Error GoTo 0

    End Function

  • 感想;

    ツリービューの次はリストビューを考えるというお決まりの?パターンで最近のVB-LIfeを楽しんでいるツリービューであんなに苦労したのだから、リストビューは一気にすんなりと、と思っていた目論みも一向に進まない。やはり展開の速さをいかに考えるかが最近のテーマになってしまった。人気のスクリプトを使ったファイル処理をまず考えたが、ロジックのせいなのか?ファイルスクリプトを利用したのが原因なのかいまいちわからない。(確かにScripting.FileSystemObject処理よりDir関数を利用したほうが早いようだが(すでに実験済み)、深い階層のフォルダーがある場合のエラーを考えると、安全策をとってしまいたくなる。)速さをとるか、安全?をとるか?・・・もう一つ考えている手を今度試してみる。・・・
    長い道のりはまだまだ続く・・・・・

メニューTOPへ