📄 vb2.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0056)http://www.pconline.com.cn/experience/subject/VB/vb2.htm -->
<HTML><HEAD><TITLE>|><| 太平洋电脑信息网</TITLE><!-- #BeginTemplate "/Templates/0723.dwt" --><!-- #BeginEditable "doctitle" --><!-- #EndEditable -->
<META content="text/html; charset=gb2312" http-equiv=Content-Type><LINK
href="vb2.files/text.css" rel=stylesheet>
<STYLE type=text/css>.sfont {
LINE-HEIGHT: 18pt
}
</STYLE>
<META content="MSHTML 5.00.2014.210" name=GENERATOR></HEAD>
<BODY bgColor=#ffffff leftMargin=0 topMargin=0>
<TABLE align=center border=0 cellPadding=0 cellSpacing=0 width="100%">
<TBODY>
<TR>
<TD vAlign=top width="100%">
<TABLE border=0 cellPadding=0 cellSpacing=0 width="100%">
<TBODY>
<TR bgColor=#303880>
<TD><A href="http://www.pconline.com.cn/"><IMG alt=太平洋电脑信息网 border=0
height=18 hspace=30 src="vb2.files/txtpconline.gif"
width=96></A><IMG height=10 src="vb2.files/blank.gif"
width=400></TD></TR></TBODY></TABLE></TD>
<TD align=right background=vb2.files/bw.gif noWrap width=130><IMG
height=26 src="vb2.files/bw.gif" width=10><A
href="http://www.pconline.com.cn/sitemap/"><IMG alt=网站地图 border=0
height=26 src="vb2.files/sitemap.gif" width=21></A><IMG height=26
src="vb2.files/bw.gif" width=10><A
href="http://www.pconline.com.cn/tpy/"><IMG alt=关于太平洋 border=0 height=26
src="vb2.files/aboutus.gif" width=21></A><IMG height=26
src="vb2.files/bw.gif" width=10><A
href="http://www.pconline.com.cn/tpy/tariff.html"><IMG alt=与我们联系 border=0
height=26 src="vb2.files/contact.gif" width=21></A><IMG height=26
src="vb2.files/bw.gif" width=10></TD></TR></TBODY></TABLE><BR>
<TABLE align=center border=0 cellPadding=0 cellSpacing=0 width=760>
<TBODY>
<TR>
<TD bgColor=#666666><IMG height=1 src="vb2.files/blank.gif" width=1></TD>
<TD bgColor=#666666><IMG height=1 src="vb2.files/blank.gif" width=1></TD>
<TD> </TD>
<TD colSpan=2 noWrap><A
href="http://www.pconline.com.cn/experience/index.html"><IMG
align=absBottom alt=企业之窗 border=0 height=15
src="vb2.files/txtexperience.gif" width=68></A> <A
href="http://www.pconline.com.cn/">首 页</A> <A
href="http://www.pconline.com.cn/market/">今日报价</A> <A
href="http://www.pconline.com.cn/recommend/">产品推介</A> <A
href="http://www.pconline.com.cn/yp/">企业之窗</A> <A
href="http://www.pconline.com.cn/news/">科技新闻</A> <A
href="http://www.pconline.com.cn/product/">产品查询</A> <A
href="http://www.pconline.com.cn/download/">软件下载</A></TD></TR>
<TR>
<TD><IMG height=1 src="vb2.files/blank.gif" width=1></TD>
<TD><IMG height=1 src="vb2.files/blank.gif" width=1></TD>
<TD><IMG height=1 src="vb2.files/blank.gif" width=21></TD>
<TD bgColor=#ffa000 colSpan=2><IMG height=1 src="vb2.files/blank.gif"
width=1></TD></TR>
<TR>
<TD bgColor=#666666><IMG height=1 src="vb2.files/blank.gif" width=1></TD>
<TD bgColor=#cdcdcd><IMG height=5 src="vb2.files/blank.gif" width=1></TD>
<TD align=right colSpan=3><IMG height=6 src="vb2.files/ffa000.gif"
width=1></TD></TR>
<TR>
<TD bgColor=#666666><IMG height=1 src="vb2.files/blank.gif" width=1></TD>
<TD align=right><A href="http://www.pconline.com.cn/"><IMG alt=太平洋电脑信息网
border=0 height=35 src="vb2.files/pconline.com.cn.gif" vspace=10
width=162></A></TD>
<TD> </TD>
<TD vAlign=bottom><!-- #BeginEditable "important_news" --><IMG height=40
src="vb2.files/blank.gif" width=350><!-- #EndEditable --></TD>
<TD align=right vAlign=top><!-- #BeginEditable "adv_top" --><IMG alt=广告位
height=43 src="vb2.files/icon.gif"
width=190><!-- #EndEditable --></TD></TR></TBODY></TABLE>
<TABLE align=center border=0 cellPadding=0 cellSpacing=0 width=760>
<TBODY>
<TR>
<TD bgColor=#666666 width=1><IMG height=1 src="vb2.files/blank.gif"
width=1></TD>
<TD vAlign=top width=170><!-- #BeginEditable "addons" --><!--替代为附加信息 --><!-- #EndEditable --></TD>
<TD bgColor=#666666 width=1><IMG height=1 src="vb2.files/blank.gif"
width=1></TD>
<TD><IMG height=1 src="vb2.files/blank.gif" width=21></TD>
<TD colSpan=3 vAlign=top width=566><!-- #BeginEditable "1" --><IMG
height=1 src="vb2.files/blank.gif" width=566><BR><IMG height=1
src="vb2.files/666666.gif" width=420><BR><IMG height=13
src="vb2.files/blank.gif" width=1><A
href="http://www.pconline.com.cn/experience/">经验共享</A> > <SPAN
class=greenb><A
href="http://www.pconline.com.cn/experience/subject/VB/index.html">专题教程</A></SPAN><A
href="http://www.pconline.com.cn/experience/subject/VB/index.html">
VB专区</A>><BR><IMG height=1 src="vb2.files/666666.gif" vspace=1
width=420><BR>
<P align=center><B><FONT color=#000066><SPAN
class=sfont>VB与Windows资源管理器互拷文件</SPAN></FONT></B> <SPAN
class=sfont><BR></SPAN></P>
<P align=center><SPAN class=sfont>陈锐 </SPAN></P>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT>
通过VB编程来拷贝或移动文件的原理可能大家都十分清楚,可以利用Windows API
SHFileOperation来进行操作,也可以利用VB内置的函数来操作。但是利用这些方法编
写的程序只能在程序内部执行文件的操作。这里我要向大家介绍如何通过VB编程将程序 中的文件操作同Windows的资源管理器中的拷贝、剪切操作连接起来。
</SPAN>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT>
在Windows的资源管理器中,选中一个或多个文件,在文件上单击鼠标右键,在弹
出菜单中选复制。再切换到另外的目录,单击鼠标右键,点粘贴。就执行了一次文件的
拷贝操作,那么Windows在拷贝过程中执行了什么操作,是否将整个文件拷贝到剪贴版
上了呢?当然没有。实际上,windows只是将一个文件结构拷贝到了剪贴版,这个结构 如下: </SPAN>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT>
tDropFile+文件1文件名+vbNullChar文件2文件名+vbNullChar...+文件N文件名+vbNullChar
其中tDropFile是一个DROPFILES结构,这个结构在Windows API中有定义。在粘贴文件 时,利用API函数
DragQueryFile 就可以获得拷贝到剪贴版的文件全路径名,然后就 可以根据获得的文件名执行文件拷贝函数,实现对文件的粘贴操作。
</SPAN>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT> 下面通过具体的程序来介绍: </SPAN>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT>
1、在工程文件中加入一个Module,然后在Module中加入如下<A
href="http://www.ccw.com.cn/99/tips/9906/062805-11.zip">代码</A>: </SPAN>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT>
2、在Form1中加入一个FileListBox,Name属性设置为File1。加入一个DirListBox, </SPAN>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT>
Name属性设置为Dir1,在Dir1的Change事件中加入如下代码: </SPAN><PRE><SPAN class=sfont>
Private Sub Dir1_Change()
File1.Path = Dir1.Path
End Sub
</SPAN></PRE>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT>
加入一个DriveListBox,Name属性设置为Drive1,在Drive1的Change事件中加入如下代码: </SPAN><PRE><SPAN class=sfont>
Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub
</SPAN></PRE>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT>
加入一个CommandButton,Name属性设置为cmdCopy,在cmdCopy的Click事件中加入如下代码: </SPAN><PRE><SPAN class=sfont>
Private Sub cmdCopy_Click()
Dim Files() As String
Dim Path As String
Dim i As Long, n As Long
Path = Dir1.Path
If Right(Path, 1) < > "\" Then
Path = Path & "\"
End If
'根据在List1上的选择建立拷贝文件的列表
With File1
For i = 0 To .ListCount - 1
If .Selected(i) Then
ReDim Preserve Files(0 To n) As String
Files(n) = Path & .List(i)
n = n + 1
End If
Next i
End With
'拷贝文件到Clipboard
If clipCopyFiles(Files) Then
MsgBox "拷贝文件成功.", , "Success"
Else
MsgBox "无法拷贝文件...", , "Failure"
End If
End Sub
</SPAN></PRE>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT>
加入一个CommandButton,Name属性设置为cmdPaste,在cmdPaste的Click事件中加入如下代码: </SPAN><PRE><SPAN class=sfont>
Private Sub cmdPaste_Click()
Dim Files() As String
Dim nRet As Long
Dim i As Long
Dim msg As String
nRet = clipPasteFiles(Files)
If nRet Then
For i = 0 To nRet - 1
msg = msg & Files(i) & vbCrLf
Next i
MsgBox msg, , "共粘贴" & nRet & "个文件"
Else
MsgBox "从剪贴版粘贴文件错误", , "Failure"
End If
End Sub
</SPAN></PRE>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT> 运行文件,在Windows
资源管理器中,选择文件,再在资源管理器菜单中选 编辑 |
复制然后在Form1中点击cmdPaste,从资源管理器中复制的文件就拷贝到Dir1所在的目录中。从
File1中选择文件,按cmdCopy复制,再在资源管理器中选 编辑 | 粘贴 ,选择的文件就被 拷贝到Windows 资源管理器的当前目录下。
</SPAN>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT> 上面的程序在Windows98
VB6.0下运行通过。</SPAN>
<P align=right><A
href="http://www.pconline.com.cn/experience/subject/VB/index.html">----更多的文章</A></P><!-- #EndEditable --></TD></TR></TBODY></TABLE>
<TABLE align=center border=0 cellPadding=0 cellSpacing=0 width=760>
<TBODY>
<TR>
<TD bgColor=#666666 width=1><IMG height=1 src="vb2.files/blank.gif"
width=1></TD>
<TD align=right vAlign=bottom width=170><IMG height=10
src="vb2.files/blank.gif" width=170><BR></TD>
<TD bgColor=#666666 width=1><IMG height=1 src="vb2.files/blank.gif"
width=1></TD>
<TD width=21><IMG height=1 src="vb2.files/blank.gif" width=21></TD>
<TD bgColor=#eeeeee width=566><IMG height=10 src="vb2.files/blank.gif"
width=566><BR>版权所有©1999 太平洋电脑信息网<BR><FONT
face="Arial, Helvetica, sans-serif"><A
href="mailto:exp@pconline.com.cn">exp@pconline.com.cn</A></FONT> <IMG
height=1 src="vb2.files/hhvisit.html" width=1> </TD></TR></TBODY></TABLE><!-- #EndTemplate --></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -