📄 vbtips7.htm
字号:
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=gb_2312-80">
<meta name="GENERATOR" content="Microsoft FrontPage Express 2.0">
<title>VBTips7</title>
</head>
<body>
<h1 align="center"><a name="home"></a>VB技巧<font size="5"><strong>(7)</strong></font>
</h1>
<blockquote>
<p><strong>1、</strong><a href="#tips00"><strong>自动出现动画、进度和确认的文件操作</strong></a><font
size="5"><strong><br>
</strong></font><strong>2、</strong><a href="#tips01"><strong>快速建立目录</strong></a><strong><br>
3、</strong><a href="#tips02"><strong>开启文件属性窗口</strong></a><strong><br>
4、</strong><a href="#tips03"><strong>使用</strong><font
face="宋体"><strong> WIN95 </strong></font><strong>的选择目录对话框</strong></a><strong><br>
5、</strong><a href="#tips04"><strong>移动文件到回收站</strong></a><strong><br>
6、</strong><a href="#tips05"><strong>比较两个文件</strong></a><strong><br>
7、</strong><a href="#tips06"><strong>取得临时文件名</strong></a><strong><br>
8、</strong><a href="#tips07"><strong>确定是 WINDOWS
的可执行文件</strong></a><strong><br>
9、</strong><a href="#tips08"><strong>建立多级目录</strong></a><strong><br>
10、</strong><a href="#tips09"><strong>取得文件的扩展名</strong></a>
</p>
</blockquote>
<div align="center"><center>
<table border="0" cellspacing="1" width="88%">
<tr>
<td width="80%"><p align="left"><a
href="vbtips.htm#Return">[1]</a> <a href="vbtips1.htm">[2]</a>
<a href="vbtips2.htm">[3]</a> <a href="vbtips3.htm">[4]</a>
<a href="vbtips4.htm">[5]</a> <a href="vbtips5.htm">[6]</a>
[7] <a href="vbtips08.htm">[8]</a> <a href="vbtips9.htm">[9]</a>
<a href="vbtips10.htm">[10]</a></p>
</td>
<td><p align="right"><font size="2">第七页(共十页)</font></p>
</td>
</tr>
</table>
</center></div>
<hr>
<div align="center"><center>
<table border="0" cellpadding="0" cellspacing="0" width="88%">
<tr>
<td valign="top"><a name="tips00"></a><strong>自动出现动画、进度和确认的文件操作</strong><font
color="#000000"><br>
使用以下的 API ,
得到与资源管理器相同的感觉!<br>
Private Type SHFILEOPSTRUCT <br>
hwnd As Long <br>
wFunc As Long<br>
pFrom As String <br>
pTo As String <br>
fFlags As Integer<br>
fAnyOperationsAborted As Long <br>
hNameMappings As Long<br>
lpszProgressTitle As String '只有在 FOF_SIMPLEPROGRESS
时用<br>
End Type<br>
<br>
</font>Private Declare Function SHFileOperation Lib _<br>
"shell32.dll" Alias
"SHFileOperationA" (lpFileOp _<br>
As SHFILEOPSTRUCT) As Long<br>
<font color="#000000"><br>
'wFunc 常数<br>
'FO_COPY 把 pFrom 文件拷贝到 pTo。<br>
Const FO_COPY = &H2<br>
'FO_DELETE 删除 pFrom 中的文件(pTo 忽略)。<br>
Const FO_DELETE = &H3<br>
'FO_MOVE 把 pFrom 文件移动到 pTo。<br>
Const FO_MOVE = &H1<br>
<br>
'fFlag 常数<br>
'FOF_ALLOWUNDO 允许 Undo 。<br>
Const FOF_ALLOWUNDO = &H40<br>
'FOF_NOCONFIRMATION 不显示系统确认对话框。<br>
Const FOF_NOCONFIRMATION = &H10<br>
'FOF_NOCONFIRMMKDIR 不提示是否新建目录。<br>
Const FOF_NOCONFIRMMKDIR = &H200<br>
'FOF_SILENT 不显示进度对话框<br>
Const FOF_SILENT = &H4<br>
<br>
例子:<br>
Dim SHFileOp As SHFILEOPSTRUCT<br>
' 删除<br>
SHFileOp.wFunc = FO_DELETE <br>
SHFileOp.pFrom = "c:\config.old" + Chr(0)<br>
SHFileOp.fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMATION<br>
Call SHFileOperation(SHFileOp)<br>
' 删除多个文件<br>
SHFileOp.wFunc = FO_DELETE<br>
SHFileOp.pFrom = "c:\config.old" +Chr(0) +
"c:\autoexec.old"+Chr(0)<br>
SHFileOp.fFlags = FOF_ALLOWUNDO<br>
Call SHFileOperation(SHFileOp)<br>
' 拷贝<br>
SHFileOp.wFunc = FO_COPY <br>
SHFileOp.pFrom = "c:\t\*.*"<br>
SHFileOp.pTo = "d:\t\*.*"<br>
SHFileOp.fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMMKDIR<br>
Call SHFileOperation(SHFileOp)<br>
' 移动<br>
SHFileOp.wFunc = FO_MOVE <br>
SHFileOp.pFrom = "c:\config.old" + Chr(0)<br>
SHFileOp.pTo = "d:\t"<br>
SHFileOp.fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMATION<br>
</font><a href="#home"><font color="#000000">返回</font></a><p><a
name="tips01"></a><font color="#000000"><strong>快速建立目录</strong></font><br>
声明:<br>
Private Type SECURITY_ATTRIBUTES<br>
nLength As Long<br>
lpSecurityDescriptor As Long<br>
bInheritHandle As Long<br>
End Type<br>
Private Declare Function CreateDirectory Lib
"kernel32" _<br>
Alias "CreateDirectoryA" (ByVal lpPathName As
String, _<br>
lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long <br>
函数:<br>
'Call
CreateNewDirectory("c:\test\directory\vb\tips\")
<br>
Public Sub CreateNewDirectory(NewDirectory As String)<br>
Dim sDirTest As String<br>
Dim SecAttrib As SECURITY_ATTRIBUTES<br>
Dim bSuccess As Boolean<br>
Dim sPath As String<br>
Dim iCounter As Integer<br>
Dim sTempDir As String<br>
iFlag = 0<br>
sPath = NewDirectory<br>
If Right(sPath, Len(sPath)) <> "\" Then<br>
sPath = sPath & "\"<br>
End If<br>
iCounter = 1<br>
Do Until InStr(iCounter, sPath, "\") = 0<br>
iCounter = InStr(iCounter, sPath, "\")<br>
sTempDir = Left(sPath, iCounter)<br>
sDirTest = Dir(sTempDir)<br>
iCounter = iCounter + 1<br>
'create directory<br>
SecAttrib.lpSecurityDescriptor = &O0<br>
SecAttrib.bInheritHandle = False<br>
SecAttrib.nLength = Len(SecAttrib)<br>
bSuccess = CreateDirectory(sTempDir, SecAttrib)<br>
Loop<br>
End Sub <br>
<a href="#home"><font color="#000000">返回</font></a></p>
<p><a name="tips02"></a><font color="#000000"><strong>开启文件属性窗口</strong></font><br>
声明:<br>
<font face="宋体">Type SHELLEXECUTEINFO<br>
cbSize As Long<br>
fMask As Long<br>
hwnd As Long<br>
lpVerb As String<br>
lpFile As String<br>
lpParameters As String<br>
lpDirectory As String<br>
nShow As Long<br>
hInstApp As Long<br>
lpIDList As Long<br>
lpClass As String<br>
hkeyClass As Long<br>
dwHotKey As Long<br>
hIcon As Long<br>
hProcess As Long<br>
End Type<br>
<br>
Public Const SEE_MASK_INVOKEIDLIST = &HC<br>
Public Const SEE_MASK_NOCLOSEPROCESS = &H40<br>
Public Const SEE_MASK_FLAG_NO_UI = &H400<br>
<br>
Declare Function ShellExecuteEX Lib
"shell32.dll" Alias _<br>
"ShellExecuteEx" (SEI As SHELLEXECUTEINFO) As
Long <br>
<br>
</font>代码:<br>
<font face="宋体">' </font>使用:<font face="宋体">
ShowProps("c:\command.com",Me.hWnd)<br>
Public Sub ShowProps(FileName As String, OwnerhWnd As
Long)<br>
Dim SEI As SHELLEXECUTEINFO<br>
Dim r As Long<br>
With SEI<br>
.cbSize = Len(SEI)<br>
.fMask = SEE_MASK_NOCLOSEPROCESS Or _<br>
SEE_MASK_INVOKEIDLIST Or SEE_MASK_FLAG_NO_UI<br>
.hwnd = OwnerhWnd<br>
.lpVerb = "properties"<br>
.lpFile = FileName<br>
.lpParameters = vbNullChar<br>
.lpDirectory = vbNullChar<br>
.nShow = 0<br>
.hInstApp = 0<br>
.lpIDList = 0<br>
End With<br>
r = ShellExecuteEX(SEI) <br>
End Sub <br>
</font><a href="#home"><font color="#000000">返回</font></a></p>
<p><a name="tips03"></a><strong>使用</strong><font
face="宋体"><strong> WIN95 </strong></font><strong>的选择目录对话框</strong><br>
声明:<br>
<font face="宋体">Private Type BrowseInfo<br>
hWndOwner As Long<br>
pIDLRoot As Long<br>
pszDisplayName As Long<br>
lpszTitle As Long<br>
ulFlags As Long<br>
lpfnCallback As Long<br>
lParam As Long<br>
iImage As Long<br>
End Type<br>
</font><br>
Private Const BIF_RETURNONLYFSDIRS = 1<br>
Private Const MAX_PATH = 260<br>
<br>
Private Declare Sub CoTaskMemFree Lib
"ole32.dll" (ByVal hMem As Long)<br>
Private Declare Function lstrcat Lib "kernel32"
Alias "lstrcatA" _ <br>
(ByVal lpString1 As String, ByVal lpString2 As String) As
Long<br>
Private Declare Function SHBrowseForFolder Lib
"shell32" _ <br>
(lpbi As BrowseInfo) As Long<br>
Private Declare Function SHGetPathFromIDList Lib
"shell32" _ <br>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -