在VB开发中,通过简捷的代码就能快速地调用这个系统对话框,并且,通过设置不 同的调用参数,能够直接选择对话框出现的形式。下面就详细介绍: '声明API函数 '第2个参数表示调用对话框的具体形式 Private Declare Function GetWindowsDirectory Lib "kernel32" _ Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, _ ByVal nSize As Long) As Long '调用通用软盘格式化对话框 Private Sub cmdFormat(ByVal mystyle As Integer) Dim rtn As String Dim Buffer As String Dim WinPath As String Buffer = String$(255, 0) rtn = GetWindowsDirectory(Buffer, Len(Buffer)) WinPath = Left(Trim(Buffer), rtn) rtn = Shell(WinPath + "\rundll32.exe shell32.dll,SHFormatDrive", mystyle ) End Sub '调用例程 Private Sub Command2_Click() Call cmdFormat(1) End Sub
Private Declare Function SHFormatDrive Lib "shell32" (ByVal hwnd As Long, ByVal Drive As Long, ByVal fmtID As Long, ByVal options As Long) As LongPrivate Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long'在 Form 中加入一个 CommandButton ,再加入一个 DriveListBoxPrivate Sub Command1_Click() Dim DriveLetter$, DriveNumber&, DriveType& Dim RetVal&, RetFromMsg% DriveLetter = UCase(Drive1.Drive) '磁盘代号 ( A / B / C / D..... ) DriveNumber = (Asc(DriveLetter) - 65) '磁盘序号,从 0 开始:A=0,B=1.... DriveType = GetDriveType(DriveLetter) '磁盘型态 ( 软盘 / 硬盘 / 光盘 ... )If DriveType = 2 Then '软盘 RetVal = SHFormatDrive(Me.hwnd, DriveNumber, 0&, 0&) Else '非软盘 RetFromMsg = MsgBox("这一张磁盘不是软盘,可能是硬盘!" & vbCrLf & _ "您还要继续格式 (Format) 吗?", 276, "格式化") Select Case RetFromMsg Case 6 'Yes:表示要格式化硬盘 ' UnComment to do it... 'RetVal = SHFormatDrive(Me.hwnd, DriveNumber, 0&, 0&) Case 7 'No:表示要取消格式化 ' Do nothing End Select End If End Sub
在VB开发中,通过简捷的代码就能快速地调用这个系统对话框,并且,通过设置不 同的调用参数,能够直接选择对话框出现的形式。下面就详细介绍: '声明API函数 '第2个参数表示调用对话框的具体形式 Private Declare Function GetWindowsDirectory Lib "kernel32" _ Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, _ ByVal nSize As Long) As Long '调用通用软盘格式化对话框 Private Sub cmdFormat(ByVal mystyle As Integer) Dim rtn As String Dim Buffer As String Dim WinPath As String Buffer = String$(255, 0) rtn = GetWindowsDirectory(Buffer, Len(Buffer)) WinPath = Left(Trim(Buffer), rtn) rtn = Shell(WinPath + "\rundll32.exe shell32.dll,SHFormatDrive", mystyle ) End Sub '调用例程 Private Sub Command2_Click() Call cmdFormat(1) End Sub
同的调用参数,能够直接选择对话框出现的形式。下面就详细介绍:
'声明API函数
'第2个参数表示调用对话框的具体形式
Private Declare Function GetWindowsDirectory Lib "kernel32" _
Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, _
ByVal nSize As Long) As Long
'调用通用软盘格式化对话框
Private Sub cmdFormat(ByVal mystyle As Integer)
Dim rtn As String
Dim Buffer As String
Dim WinPath As String
Buffer = String$(255, 0)
rtn = GetWindowsDirectory(Buffer, Len(Buffer))
WinPath = Left(Trim(Buffer), rtn)
rtn = Shell(WinPath + "\rundll32.exe shell32.dll,SHFormatDrive", mystyle
)
End Sub
'调用例程
Private Sub Command2_Click()
Call cmdFormat(1)
End Sub
Dim DriveLetter$, DriveNumber&, DriveType&
Dim RetVal&, RetFromMsg%
DriveLetter = UCase(Drive1.Drive) '磁盘代号 ( A / B / C / D..... )
DriveNumber = (Asc(DriveLetter) - 65) '磁盘序号,从 0 开始:A=0,B=1....
DriveType = GetDriveType(DriveLetter) '磁盘型态 ( 软盘 / 硬盘 / 光盘 ... )If DriveType = 2 Then '软盘
RetVal = SHFormatDrive(Me.hwnd, DriveNumber, 0&, 0&)
Else '非软盘
RetFromMsg = MsgBox("这一张磁盘不是软盘,可能是硬盘!" & vbCrLf & _
"您还要继续格式 (Format) 吗?", 276, "格式化")
Select Case RetFromMsg
Case 6 'Yes:表示要格式化硬盘
' UnComment to do it...
'RetVal = SHFormatDrive(Me.hwnd, DriveNumber, 0&, 0&)
Case 7 'No:表示要取消格式化
' Do nothing
End Select
End If
End Sub
alicky(周松)的按其思路应先判断系统,在2000、xp中rundll32.exe 在SysPath而不在WinPath,其实只要Shell "rundll32.exe shell32.dll,SHFormatDrive", mystyle就行了,不用管它在什么位置
lxcc(虫莲)第一个能够格式化,但不能解决问题,第二个,欠妥,2000\xp中无Format.exe
同的调用参数,能够直接选择对话框出现的形式。下面就详细介绍:
'声明API函数
'第2个参数表示调用对话框的具体形式
Private Declare Function GetWindowsDirectory Lib "kernel32" _
Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, _
ByVal nSize As Long) As Long
'调用通用软盘格式化对话框
Private Sub cmdFormat(ByVal mystyle As Integer)
Dim rtn As String
Dim Buffer As String
Dim WinPath As String
Buffer = String$(255, 0)
rtn = GetWindowsDirectory(Buffer, Len(Buffer))
WinPath = Left(Trim(Buffer), rtn)
rtn = Shell(WinPath + "\rundll32.exe shell32.dll,SHFormatDrive", mystyle
)
End Sub
'调用例程
Private Sub Command2_Click()
Call cmdFormat(1)
End Sub
可是仍然不能解决自动格式化的问题,用户只要单击取消就行了!
谢谢了
还有谁有更好的方法?