VB编程俱乐部
问题1:请问用什么样的方法能使删除后的文件放到“回收站”里?
答:下面的回答是Prisoner 摘自Microsoft Knowledge Base中的一个例子,可以使删除后的文件放到回收站里。
1)首先建立一个新模块;在模块中增加如下声明:
Type SHFILEOPSTRUCT
hWnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAborted As Boolean
hNameMaps As Long
sProgress As String
End Type
Public Const FO_DELETE = &H3
Public Const FOF_ALLOWUNDO = &H40
Declare Function SHFileOperation Lib ″shell32.dll″ Alias ″SHFileOperationA″ (lpFileOp As SHFILEOPSTRUCT) As Long
2)建立一个名为 ShellDelete 的函数,用于删除文件:
Public Function ShellDelete(ParamArray vntFileName() As Variant)
Dim I As Integer
Dim sFileNames As String
Dim SHFileOp As SHFILEOPSTRUCT
For I = LBound(vntFileName) To UBound(vntFileName)
sFileNames = sFileNames & vntFileName(I) & vbNullChar
Next I
sFileNames = sFileNames & vbNullChar
With SHFileOp
.wFunc = FO_DELETE
.pFrom = sFileNames
.fFlags = FOF_ALLOWUNDO
End With
ShellDelete = SHFileOperation(SHFileOp)
End Function
3)下面就是用于删除文件并送到“回收站”的代码,你可以根据情况进行修改,使它符合你的需要:
Private Sub Command1_Click()
Dim FileToKill As String
FileToKill = ″c:\test*.txt″
ShellDelete FileToKill
MsgBox ″File(s) deleted″
End Sub
问题2:我有一含有密码的数据库(mdb),使用以下语句可在VB中打开:Set testdb =Opendatabase(″File.mdb″,false,false,″;PWD=YourPassword″), 我想将结果传给datagrid控件中,不知如何去做或在data控件中如何打开含有密码的数据库。
答:打开的方法如下:
Dta.DatabaseName = App.Path & ″\″ & PubDBName
Dta.Connect = ″;pwd=″ & PubDBPassword
Dta.RecordSource = ″Polo_Browse″
Dta.Refresh
(本题答案由Taylor提供)
问题3:一般用鼠标单击窗口右上角的“X”时,程序立刻退出,但有的程序在退出时会弹出一个确认窗口供确认,请问这是如何实现的?
答:可以利用Form的Form_QueryUnLoad事件在用户单击“”时进行确认:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Cancel = True
Select Case MsgBox(″真的关闭吗?″, vbOKCancel,″请您回答″)
Case vbOK
Cancel = False
Case Else
Cancel = True
End Select
End Sub
问题4:我用VB写了一个应用软件,想将其做成电子注册版。但是,我对怎样获得电脑中的某一个区别于其它电脑的唯一参数(如:CPU串号/硬盘IDE参数等)的方法不了解,请指点。
答:下面的这个例子是使用GetVolumeInformation获得磁盘的序列号 :
Private Declare Function GetVolumeInformation Lib ″kernel32″ Alias
″GetVolumeInformationA″ (ByVal lpRootPathName As String, ByVal
lpVolumeNameBuffer As String, ByVal nVolumeNameSize As Long,
lpVolumeSerialNumber As Long, lpMaximumComponentLength As Long,
lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As String, ByVal
nFileSystemNameSize As Long) As Long
Function GetSerialNumber(strDrive As String) As Long
Dim SerialNum As Long
Dim Res As Long
Dim Temp1 As String
Dim Temp2 As String
Temp1 = String$(255, Chr$(0))
Temp2 = String$(255, Chr$(0))
Res = GetVolumeInformation(strDrive, Temp1,Len(Temp1), SerialNum, 0, 0, Temp2, Len(Temp2))
GetSerialNumber = SerialNum
End Function
调 用 时 使 用 GetSerialNumber(″C:\″)就 可 以 。
(本题答案由askpro提供)