Chinaunix首页 | 论坛 | 博客
  • 博客访问: 188439
  • 博文数量: 106
  • 博客积分: 3810
  • 博客等级: 中校
  • 技术积分: 1007
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-18 13:35
文章分类

全部博文(106)

文章存档

2014年(17)

2011年(5)

2010年(75)

2009年(9)

我的朋友

分类:

2010-04-27 08:10:29

图形显示磁盘信息的实例

分类:VB.Net 

测试平台:VS2005 + .Net Framework 3.5sp1 + Windows2003/xp

代码如下:

 

 

' 导入命名空间。
Imports System.IO

Public Class LOB

    ' 声明各个私用变量。
    Private dirInfo As DirectoryInfo
    ' 此变量用来持有磁盘容量。
    Private totalSpace As Long
    ' 此变量用来持有磁盘可使用空间(剩余空间)。
    Private freeSpace As Long
    ' 此变量用来持有磁盘已使用空间。
    Private usedSpace As Long
    ' 此变量用来持有剩余空间之扇形区域之第二个边的角度。
    Private sweep As Single
    ' 此变量用来判断磁盘驱动器是否可以存取。
    Private isSpaceInfoAvailable As Boolean

    Private Sub LOB_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' 清除 Label 控件的显示内容。
        Me.driveReadyStatus.Text = ""

        ' 声明 DriveInfo 类对象,并使用 GetDrives 方法取得目前
        ' 系统中所有逻辑磁盘驱动器的 DriveInfo 类型数组。
        Dim drives As System.IO.DriveInfo() = System.IO.DriveInfo.GetDrives

        ' 将 drives 的内容赋给 ComboBox 控件当作选项显示。
        drivesOnPc.Items.AddRange(drives)
    End Sub

    Private Sub drivesOnPc_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles drivesOnPc.SelectedIndexChanged
        ' 调用用户自定义函数 LoadDriveInfo() 并将用户所选取的 ComboBox 项目(亦即 DriveInfo 对象)传递给它。
        LoadDriveInfo(drivesOnPc.SelectedItem)

        ' 强制重新绘制画面上的图形。
        Me.Invalidate()
    End Sub

    Private Sub LoadDriveInfo(ByVal myDriveInfo As DriveInfo)
        ' 将磁盘驱动器名称显示在文本框中。
        Me.driveName.Text = myDriveInfo.Name

        Try
            ' 判断 myDriveInfo 对象的磁盘代号文字长度是否大于 0 。
            If myDriveInfo.VolumeLabel.Length > 0 Then
                ' 将磁盘驱动器代号显示在文本框中。
                Me.driveVolumeLabel.Text = myDriveInfo.VolumeLabel
            Else
                Me.driveVolumeLabel.Text = "无"
            End If

            ' 将磁盘驱动器档案系统格式显示在文本框中。
            Me.driveFormat.Text = myDriveInfo.DriveFormat

            ' 将磁盘驱动器大小赋给变量 totalSpace。
            totalSpace = myDriveInfo.TotalSize

            ' 将磁盘驱动器剩余空间赋给变量 freeSpace。
            freeSpace = myDriveInfo.TotalFreeSpace

            ' 将已使用空间赋给变量 usedSpace。
            usedSpace = totalSpace - freeSpace

            ' 计算剩余空间之扇形区域之第二个边的角度。
            sweep = 360.0F * (freeSpace / totalSpace)

            isSpaceInfoAvailable = True
        Catch
            ' 将文字显示在文本框中。
            Me.driveVolumeLabel.Text = "无法访问"

            ' 将文字显示在文字方块中。
            Me.driveFormat.Text = "无法访问"

            isSpaceInfoAvailable = False
        End Try

        ' 将磁盘驱动器类型显示在文本框中。
        Me.driveType.Text = myDriveInfo.DriveType.ToString

        ' 将磁盘驱动器的根目录显示在文本框中。
        Me.driveRootDirectory.Text = myDriveInfo.RootDirectory.ToString

        ' 取得根目录的 DirectoryInfo 对象。
        dirInfo = myDriveInfo.RootDirectory '更多.net源码和教程,来自[乐博网 www.lob.cn]

        ' 判断磁盘驱动器是否已经就绪。
        If myDriveInfo.IsReady = True Then
            Me.driveReadyStatus.Text = "磁盘驱动器可访问"
        Else
            Me.driveReadyStatus.Text = "磁盘驱动器无法访问"
        End If

    End Sub

    Private Sub CH1_DemoForm004_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        ' 声明 Rectangle 结构变量,并指定 X 坐标参数、Y 坐标参数、宽度参数、高度参数,用来显示圆饼图的矩形大小。
        Dim rect As Rectangle = New Rectangle(410, 20, 200, 200)

        ' 声明 Rectangle 结构变量,并指定 X 坐标参数、Y 坐标参数、宽度参数、高度参数,用来当作圆饼图边界的矩形大小。
        Dim rect2 As Rectangle = New Rectangle(320, 10, 490, 320)

        ' 声明 Rectangle 结构变量,并指定 X 坐标参数、Y 坐标参数、宽度参数、高度参数,用来当作已使用空间的图例。
        Dim freeLegend As Rectangle = New Rectangle(325, 300, 20, 20)

        ' 声明 Rectangle 结构变量,并指定 X 坐标参数、Y 坐标参数、宽度参数、高度参数,用来当作未使用空间的图例。
        Dim usedLegend As Rectangle = New Rectangle(325, 275, 20, 20)

        ' 在画面上显示出矩形,并指定颜色参数与要显示的 Rectangle 参数。
        e.Graphics.DrawRectangle(Pens.Black, rect2)

        ' 绘制 "磁盘容量信息" 下方的水平线。
        e.Graphics.DrawLine(Pens.Black, 320, 260, 810, 260)

        ' 判断布尔变量。
        If isSpaceInfoAvailable = True Then

            ' 在画面上显示圆饼图,并指定颜色参数、Rectangle 参数、圆饼图的起点参数、以及圆饼图要涵盖的范围参数。
            e.Graphics.FillPie(Brushes.Magenta, rect, 0, sweep)
            e.Graphics.FillPie(Brushes.Blue, rect, sweep, 360 - sweep)

            ' 在画面上显示已使用及未使用图例。
            e.Graphics.FillRectangle(Brushes.Magenta, freeLegend)
            e.Graphics.FillRectangle(Brushes.Blue, usedLegend)

            ' 在画面上显示文字内容,并指定显示内容参数、字型参数、笔刷参数、以及位置参数。
            e.Graphics.DrawString("乐博网提示:磁盘容量为", New Font("Tahoma", 10, FontStyle.Regular), Brushes.Black, New PointF(360, 230))
            e.Graphics.DrawString("提示:已使用空间为, New Font("Tahoma", 10, FontStyle.Regular), Brushes.Black, New PointF(345, 275))
            e.Graphics.DrawString("
未使用空间:", New Font("Tahoma", 10, FontStyle.Regular), Brushes.Black, New PointF(345, 300))
            e.Graphics.DrawString(totalSpace.ToString("
N") + " 个字节", New Font("Tahoma", 10, FontStyle.Regular), Brushes.Black, New PointF(450, 230))
            e.Graphics.DrawString(usedSpace.ToString("
N") + " 个字节", New Font("Tahoma", 10, FontStyle.Regular), Brushes.Black, New PointF(450, 275))
            e.Graphics.DrawString(freeSpace.ToString("
N") + " 个字节", New Font("Tahoma", 10, FontStyle.Regular), Brushes.Black, New PointF(450, 300))

            ' 为了方便使用者检视磁盘空间信息,除了字节之外,额外显示出 MB 或 GB。
            ' 如果磁盘空间值小于 1073741824 个字节,就额外显示出 MB;否则,就额外显示出 GB。
            e.Graphics.DrawString(IIf(totalSpace < 1073741824, ConvertBytesToMB(totalSpace) + "
MB", ConvertBytesToGB(totalSpace) + " GB"), New Font("Tahoma", 10, FontStyle.Regular), Brushes.Black, New PointF(720, 230))
            e.Graphics.DrawString(IIf(usedSpace < 1073741824, ConvertBytesToMB(usedSpace) + "
MB", ConvertBytesToGB(usedSpace) + " GB"), New Font("Tahoma", 10, FontStyle.Regular), Brushes.Black, New PointF(720, 275))
            e.Graphics.DrawString(IIf(freeSpace < 1073741824, ConvertBytesToMB(freeSpace) + "
MB", ConvertBytesToGB(freeSpace) + " GB"), New Font("Tahoma", 10, FontStyle.Regular), Brushes.Black, New PointF(720, 300))
        End If
    End Sub

    ' 将 Bytes 值转换为 MB 值。
    Private Function ConvertBytesToMB(ByVal bytes As Int64) As String
        Dim mb As Long = bytes / 1048576
        Return mb.ToString("
N")
    End Function
    ' 将 Bytes 值转换为 GB 值。
    Private Function ConvertBytesToGB(ByVal bytes As Int64) As String
        Dim gb As Long = bytes / 1073741824
        Return gb.ToString("
N


阅读(353) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~