VB与Windows资源管理器互拷文件

页面导航:首页 > 软件编程 > vb.net > VB与Windows资源管理器互拷文件

VB与Windows资源管理器互拷文件

来源: 作者: 时间:2016-02-05 12:32 【

通过VB编程来拷贝或移动文件的原理可能大家都十分清楚,可以利用Windows APISHFileOperation来进行操作,也可以利用VB内置的函数来操作。但是利用这些方法编写的程序只能在程序内部执行

 

 

   通过VB来拷贝或移动文件的原理可能大家都十分清楚,可以利用Windows APISHFileOperation来进行操作,也可以利用VB内置的函数来操作。但是利用这些方法编写的程序只能在程序内部执行文件的操作。这里我要向大家介绍如何通过VB编程将程序中的文件操作同Windows的资源管理器中的拷贝、剪切操作连接起来。

   在Windows的资源管理器中,选中一个或多个文件,在文件上单击鼠标右键,在弹出菜单中选复制。再切换到另外的目录,单击鼠标右键,点粘贴。就这样执行了一次文件的拷贝操作,那么Windows在拷贝过程中执行了什么操作,是否将整个文件拷贝到剪贴板上了呢?当然没有。实际上,Windows只是将一个文件结构拷贝到了剪贴版,这个结构如下:

   tDropFile+文件1文件名+vbNullChar+文件2文件名+vbNullChar……+文件N文件名+vbNullChar,其中tDropFile是一个DROPFILES结构,这个结构在Windows API中有定义。在粘贴文件时,利用API函数 DragQueryFile 就可以获得拷贝到剪贴板的文件全路径名,然后就可以根据获得的文件名执行文件拷贝函数,实现对文件的粘贴操作。

   下面通过具体的程序来介绍:

   1、在工程文件中加入一个Module,然后在Module中加入如下代码:

  Option Explicit

  Private Type POINTAPI

   x As Long

   y As Long

  End Type

  Private Type SHFILEOPSTRUCT

   hwnd As Long

   wFunc As Long

   pFrom As String

   pTo As String

   fFlags As Integer

   fAnyOperationsAborted As Long

   hNameMappings As Long

   lpszProgressTitle As String

  End Type

  Private Declare Function SHFileOperation Lib shell32.dll" Alias _

   “SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long

  剪贴板处理函数

  Private Declare Function EmptyClipboard Lib user32" () As Long

  Private Declare Function OpenClipboard Lib user32" (ByVal hwnd _

   As Long) As Long

  Private Declare Function CloseClipboard Lib user32" () As Long

  Private Declare Function SetClipboardData Lib user32" (ByVal wFormat _

   As Long, ByVal hMem As Long) As Long

  Private Declare Function GetClipboardData Lib user32" (ByVal wFormat _

   As Long) As Long

  Private Declare Function IsClipboardFormatAvailable Lib user32" _

   (ByVal wFormat As Long) As Long

  Private Declare Function DragQueryFile Lib shell32.dll" Alias _

   “DragQueryFileA" (ByVal hDrop As Long, ByVal UINT As Long, _

   ByVal lpStr As String, ByVal ch As Long) As Long

  Private Declare Function DragQueryPoint Lib shell32.dll" (ByVal _

   hDrop As Long, lpPoint As POINTAPI) As Long

  Private Declare Function GlobalAlloc Lib kernel32" (ByVal wFlags _

   As Long, ByVal dwBytes As Long) As Long

  Private Declare Function GlobalFree Lib kernel32" (ByVal hMem As _

   Long) As Long

  Private Declare Function GlobalLock Lib kernel32" (ByVal hMem As _

   Long) As Long

  Private Declare Function GlobalUnlock Lib kernel32" (ByVal hMem As _

   Long) As Long

  Private Declare Sub CopyMem Libkernel32" Alias RtlMoveMemory" _

   (Destination As Any, Source As Any, ByVal Length As Long)

  剪贴板数据格式定义

  Private Const CF_TEXT = 1

  Private Const CF_BITMAP = 2

  Private Const CF_METAFILEPICT = 3

  Private Const CF_SYLK = 4

Tags:

相关文章

    文章评论

    
    <