[VB.NET]资源管理器如何用.net2005实现

前端之家收集整理的这篇文章主要介绍了[VB.NET]资源管理器如何用.net2005实现前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
资源管理器如何用.net2005实现
http://www.cnblogs.com/wssmax/archive/2005/06/03/167342.html?Pending=true#Post

如何用.NET2005做成上述资源管理器???
__________________________________________________________________________
VBA是调用API函数来实现的,.NET2005怎么实现上述功能呢??
__________________________________________________________________________
大家真的都不知道吗???
__________________________________________________________________________
其实挺简单的,就是没有想到而已,用FolderBrowserDiag控件即可*——*
__________________________________________________________________________
FolderBrowserDialog是弹出窗口,没法用到窗体中啊?
你那个链接中的代码我没有看到效果,是个X
__________________________________________________________________________
TreeView + ListView
主要代码如下(其他诸如右键菜单、拖动等功能LZ自己实现吧,这部分我还没有写,呵呵):

VB.NET code
Imports System
Imports System.Windows.Forms
Imports System.IO
Imports System.Diagnostics
Imports WeifenLuo.WinFormsUI

Public Class frmFileSystem
Inherits DockContent

Private iFiles As Integer = 0
Private iDirectories As Integer = 0

Public Sub New()
InitializeComponent()
End Sub

Private Sub Form1_Load(ByVal sender As Object,ByVal e As System.EventArgs) Handles Me.Load
Dim d() As String = System.IO.Directory.GetLogicalDrives()
Dim en As System.Collections.IEnumerator = d.GetEnumerator

treeView1.BeginUpdate()
While en.MoveNext
Dim node As New System.Windows.Forms.TreeNode(en.Current.ToString)
treeView1.Nodes.Add(node)
End While
treeView1.EndUpdate()
End Sub

Private Sub AddDirectories(ByVal tnSubNode As TreeNode)
treeView1.BeginUpdate()
iDirectories = 0

Try
Dim diRoot As DirectoryInfo
If tnSubNode.SelectedImageIndex < 5 Then
diRoot = New DirectoryInfo(tnSubNode.FullPath + "/")
Else
diRoot = New DirectoryInfo(tnSubNode.FullPath)
End If
Dim dirs As DirectoryInfo() = diRoot.GetDirectories()

tnSubNode.Nodes.Clear()

For Each dir As DirectoryInfo In dirs
iDirectories += 1
Dim subNode As New TreeNode(dir.Name)
subNode.ImageIndex = 4
subNode.SelectedImageIndex = 5
tnSubNode.Nodes.Add(subNode)
Next
Catch
End Try

treeView1.EndUpdate()
End Sub

Private Sub AddFiles(ByVal strPath As String)
Me.ListViewEx1.BeginUpdate()

ListViewEx1.Items.Clear()
iFiles = 0
Try
Dim di As New DirectoryInfo(strPath + "/")
Dim theFiles As FileInfo() = di.GetFiles()
For Each theFile As FileInfo In theFiles
iFiles += 1

Dim lvItem As TreeListView.ExtendedListViewItem = Me.ListViewEx1.Items.Add(theFile.Name)
lvItem.ItemImage = GetItemBitmap.GetIcon(theFile.FullName)
lvItem.SubItems.Add(GetSizeString(theFile.Length))
lvItem.SubItems.Add(theFile.LastWriteTime.ToShortDateString + " " + theFile.LastWriteTime.ToShortTimeString)
Next
Catch Exc As Exception
statusBar1.Text = Exc.ToString()
End Try

ListViewEx1.EndUpdate()
End Sub

Private Sub treeView1_AfterSelect(ByVal sender As Object,ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles treeView1.AfterSelect
Call Me.AddDirectories(e.Node)
treeView1.SelectedNode.Expand()
Call Me.AddFiles(e.Node.FullPath.ToString())
statusBar1.Text = iDirectories.ToString() + "个文件夹," + iFiles.ToString() + "个文件"
End Sub

Private Sub ListViewEx1_DoubleClick(ByVal sender As Object,ByVal e As System.EventArgs) Handles ListViewEx1.DoubleClick
Dim item As TreeListView.ExtendedListViewItem = Me.ListViewEx1.SelectedItems(0)
Try
Dim sPath As String = treeView1.SelectedNode.FullPath
Dim sFileName As String = item.Text
Process.Start(sPath + "/" + sFileName)
Catch ex As Exception
MsgBox(ex.Message,MsgBoxStyle.Critical,"Error")
End Try
End Sub

Private Function GetSizeString(ByVal size As Long) As String
Dim gb As Double = 1024 * 1024 * 1024
Dim mb As Double = 1024 * 1024
Dim kb As Double = 1024

Dim sizekb As Long = CLng(Math.Ceiling(CDbl(size) / kb))
Select Case size
Case Is <= kb : Return String.Format("{0} KB",sizekb)
Case Is > gb : Return String.Format("{0:f2} GB",Math.Round(CDbl(size) / gb,2))
Case Is > mb : Return String.Format("{0:f2} MB",Math.Round(CDbl(size) / mb,2))
Case Else : Return String.Format("{0} KB",sizekb)
End Select
End Function

Public Class GetItemBitmap

Private Const SHGFI_ICON As Integer = 256
Private Const SHGFI_SMALLICON As Integer = 1
Private Const SHGFI_LARGEICON As Integer = 0

Private Structure SHFILEINFO
Public hIcon As IntPtr
Public iIcon As Integer
Public dwAttributes As UInteger
_
Public szDisplayName As String
_
Public szTypeName As String
End Structure

_
Private Shared Function SHGetFileInfo(ByVal pszPath As String,ByVal dwFileAttributes As UInteger,ByRef psfi As SHFILEINFO,ByVal cbFileInfo As Integer,ByVal uFlags As UInteger) As IntPtr
End Function

_ Private Shared Function DestroyIcon(ByVal handle As IntPtr) As Boolean End Function Public Shared Function GetIcon(ByVal filename As String) As Drawing.Bitmap Dim iconbitmap As Drawing.Bitmap Dim shinfo As New SHFILEINFO() Dim hImgSmall As IntPtr = SHGetFileInfo(filename,shinfo,System.Runtime.InteropServices.Marshal.SizeOf(shinfo),SHGFI_ICON Or SHGFI_SMALLICON) Dim icon As Drawing.Icon = System.Drawing.Icon.FromHandle(shinfo.hIcon) iconbitmap = icon.ToBitmap() DestroyIcon(shinfo.hIcon) Return iconbitmap End Function End Class End Class __________________________________________________________________________ Mark __________________________________________________________________________
原文链接:https://www.f2er.com/vb/263729.html

猜你在找的VB相关文章