您的位置:寻梦网首页编程乐园VB 编程乐园VB问题全功略

VB问题全功略(49)

上一页(49)下一页

241、如何启动别的应用程序,并于应用程序结束前返回所需的资料?
242、如何在 VB 中使用 Winzip 来压缩文件?
243、如何取得临时文件名
244、如何确定是 Windows 的可执行文件
245、如何取得文件的扩展名

241、如何启动别的应用程序,并于应用程序结束前返回所需的资料?

在这个范例中,我们会用到一个函数及二个陈述式,说明如下:

Shell 函数: 执行一个执行文件程序,如果成功的话,会返回一个 Variant (Double) 来代表这个程序的 task ID,若不成功,则会返回 o。
语法:Shell(pathname[,windowstyle])
AppActivate 陈述式: 启动一应用程序视窗。
语法:AppActivate title[, wait]
SendKeys 陈述式: 送出一或多个按键讯息到使用中的视窗,就如同在键盘上按下的一样。
语法:SendKeys string[, wait]

它们的完整说明,请自行参阅线上手册或 Msdn!在这里,我们只是要告诉大家一个方法而已!

今天我们要练习的是:在程序中使用 Shell 去执行小算盘,使用 AppActivate 去启动它,使用 SendKeys 传送按键给它做运算,再将运算结果用剪贴簿接收回来,最后关闭小算盘。

在 Form 上放一个 CommandButton 及一个 TextBox,并将以下的程序码复制到 Form 中:

Private Sub Command1_Click()
' 1 启动小算盘并计算
Dim ReturnValue, I
ReturnValue = Shell("Calc.EXE", 1) ' 执行小算盘。
AppActivate ReturnValue ' 启动小算盘。
For I = 1 To 100 ' 设定回圈执行次数。
SendKeys I & "{+}", True ' 按下按键给小算盘
Next I ' 将所有 I 值相加。
SendKeys "=", True ' 取得总和。
' 2 将计算结果抓回程序中
Clipboard.Clear ' 清除剪贴簿内容。
SendKeys "^{C}", True ' 按 CTRL +C 来复制小算盘计算结果。
Text1.Text = Clipboard.GetText ' 将剪贴簿中之计算结果放到 Text1 中。
' 3 关闭小算盘
SendKeys "%{F4}", True ' 按 ALT+F4 来结束小算盘。
End Sub

执行之后您会看到小算盘自动运算,最后将结果 10100 返回 Text1中!OK!

如果您想看分解动作的话,就重新开启一个新的工程,在 Form 上放三个

CommandButton 及一个 TextBox,程序码如下:

Dim ReturnValue

' 1 启动小算盘并计算
Private Sub Command1_Click()
Dim I
ReturnValue = Shell("Calc.EXE", 1) ' 执行小算盘。
AppActivate ReturnValue ' 启动小算盘。
For I = 1 To 100 ' 设定回圈执行次数。
SendKeys I & "{+}", True ' 按下按键给小算盘
Next I ' 将所有 I 值相加。
SendKeys "=", True ' 取得总和。
End Sub

' 2 将计算结果抓回程序中
Private Sub Command2_Click()
Clipboard.Clear ' 清除剪贴簿内容。
AppActivate ReturnValue ' 启动小算盘。
SendKeys "^{C}", True ' 按 CTRL +C 来复制小算盘计算结果。
Text1.Text = Clipboard.GetText ' 将剪贴簿中之计算结果放到 Text1 中。
End Sub

' 3 关闭小算盘
Private Sub Command3_Click()
AppActivate ReturnValue ' 启动小算盘。
SendKeys "%{F4}", True ' 按 ALT+F4 来结束小算盘。
End Sub

当然,结果和上面一个范例是一样的!

242、如何在 VB 中使用 Winzip 来压缩文件?

当今市面上压缩软体一大堆,但是如果您想在 VB 程序中做文件压缩的功能,

您知道要怎么做吗?

我不懂一些烦琐的压缩理论,但是我懂得利用现有的压缩软体来替我达到

我想要的功能!

在这个例子中,我要使用 Winzip 来做,所以,如果您想用下面的范例来做

练习的话,先决条件就是您的电脑中一定要有安装 Winzip 才行!

压缩文件 (加入文件):

语法:winzip[32].exe [-min] action [options] filename[.zip] files

参数说明:

-min 最小化
指定 Winzip 执行时为最小化。如果您要使用 -min 这个参数,它必须是命令列参

数的第一个参数。

action 动作

-a add 加入文件
-f freshen 重新整理目前文件
-u update 更新文件
-m move 移动文件

options 选项

Directories 资料夹选项
-r Recurse Directories 含资料夹内内容。
-p Save Extra Directory Info 储存其他层资料夹讯息。
method 决定压缩的方法选项
-ex eXtra 最大 (最慢)
-en Normal 一般 (预设值)
-ef Fast 快速
-es Super fast 最快速
-e0 no compression 不压缩
其他选项
-s 设定密码 密码必须使用双引号括起来。
例如:-s "Secret Password",不过要注意大小写是不同的!
-hs   包含符合条件的隐藏及系统文件。

filename.zip 压缩文件名
指定压缩后的文件名,要注意的是必须是全路径文件名 (含磁盘代号及资料夹)。

files 所有原始文件
一个或多个文件,每一个文件写在独立的一行,可以使用万用字元,例如:*.bak。

解压缩文件:

语法:winzip[32].exe -e [options] filename[.zip] directory

参数说明:

-e 表示解压缩
是必要的参数!

options 选项

-o Overwrite existing files
without prompting 取代同名的文件。
-j Junk pathnames 跳过旧文件。
除非指定 -J 这个参数,否则会使用原本的资料夹名称。
-s 设定密码 密码必须使用双引号括起来。
例如:-s "Secret Password",不过要注意大小写是不同的!
-hs   包含符合条件的隐藏及系统文件。

filename.zip 压缩文件名
指定压缩后的文件名,要注意的是必须是全路径文件名 (含磁盘代号及资料夹)。

directory 资料夹
解压缩后的文件存放的资料夹。如果资料夹不存在便会自动建立。

注意事项:

※非常重要:
永远使用全路径文件名 (含磁盘代号及资料夹)。
※若希望执行 Winzip 时是最小化,要使用 -min 这个参数,它必
须是命令列参数的第一个参数。
※Winzip 内建只支援 zip 及 unzip 二种操作模式。
※若是长文件名的话,必须使用双引号括起来。
※在设定文件时,前导及最后的字符不可空白,也不可有空白行!
※在命令列中的动作及选项参数,至少要用一个以上的空白分隔。

※WinZip 可以用来配合 cc:Mail 压缩文件:
更改 WMAIL.INI 中的 [cc:Mail] section 的 compress= 这一行
指定 Winzip 的完整路径,并跟随著 "-a %1 @%2" 参数
例如:如果 Winzip 安装在 c:\winzip,则 compress= 这一行必须改成
compress=c:\winzip\winzip.exe -a %1 @%2

范例程序:

Private Sub Command1_Click()
Dim wzipexe As String ' winzip 执行文件的位置
Dim wsource As String ' 原始文件 (压缩前)
Dim wtarget As String ' 目地文件 (压缩后)
Dim wcmd As String ' Shell 指令
Dim retval As Double ' Shell 指令返回值

'拼凑 Shell 指令
wzipexe = "C:\program files\winzip\WINzip32"
wtarget = "c:\TargetFolder\AssignedNameFile.zip"
wsource = "c:\SourceFolder\SourceFile(s)"
wcmd = wzipexe & " -a " & wtarget &
" " & wsource
retval = Shell(wcmd, 6)

'上面的一大串可写成
'retval = Shell("C:\program files\winzip\WINzip32 -a c:\TargetFolder\AssignedNameFile.zip c:\SourceFolder\SourceFile(s)", 6)
End Sub

243、如何取得临时文件名

声明:
Public Const MAX_PATH = 260

Public Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA" (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As String) As Long
Public Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long

代码:
Public Function GetTempFile() As String
Dim lngRet As Long
Dim strBuffer As String, strTempPath As String

'初始化 buffer
strBuffer = String$(MAX_PATH, 0)

'取得临时路径
lngRet = GetTempPath(Len(strBuffer), strBuffer)

'0 错误
If lngRet = 0 Then Exit Function

'去掉尾中的 null
strTempPath = Left$(strBuffer, lngRet)

'初始化 buffer
strBuffer = String$(MAX_PATH, 0)

'取得临时文件名
lngRet = GetTempFileName(strTempPath, "tmp", 0&, strBuffer)

If lngRet = 0 Then Exit Function

lngRet = InStr(1, strBuffer, Chr(0))
If lngRet > 0 Then
GetTempFile = Left$(strBuffer, lngRet - 1)
Else
GetTempFile = strBuffer
End If
End Function

244、如何确定是 Windows 的可执行文件

在文件的第 24 字节,如果为40h,就是 Windows 的可执行文件。

Function WinExe (ByVal Exe As String) As Integer
Dim fh As Integer
Dim t As String * 1
fh = FreeFile
Open Exe For Binary As #fh
Get fh, 25, t
Close #fh
WinExe = (Asc(t) = &H40&)
End Function

245、如何取得文件的扩展名

Function GetExtension(Filename As String)
Dim PthPos, ExtPos As Integer

For i = Len(Filename) To 1 Step -1 ' Go from the Length of the filename, to the first character by 1.
If Mid(Filename, i, 1) = "." Then ' If the current position is '.' then...
ExtPos = i ' ...Change the ExtPos to the number.
For j = Len(Filename) To 1 Step -1 ' Do the Same...
If Mid(Filename, j, 1) = "\" Then ' ...but for '\'.
PthPos = j ' Change the PthPos to the number.
Exit For ' Since we found it, don't search any more.
End If
Next j
Exit For ' Since we found it, don't search any more.
End If
Next i

If PthPos > ExtPos Then
Exit Function ' No extension.
Else
If ExtPos = 0 Then Exit Function ' If there is not extension, then exit sub.
GetExtension = Mid(Filename, ExtPos + 1, Len(Filename) - ExtPos) 'Messagebox the Extension
End If

End Function
使用:
FileExt = GetExtension("c:\windows\vb\vb.exe")

上一页(49)下一页