VB如何利用剪贴板复制、粘贴文件,用到啥API

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VB如何利用剪贴板复制、粘贴文件,用到啥API相关的知识,希望对你有一定的参考价值。

是文件不是文本、图象。解决加500分
强调:《在程序中》把一个文件,,例如:“C:\\教育论文稿102.doc" 复制到剪贴板上

找了一下MSDN,希望对你有用

Clipboard 对象

提供对系统 Clipboard 的访问。

语法

Clipboard

说明

Clipboard 对象用于操作剪贴板上的文本和图形。它使用户能够复制、剪切和粘贴应用程序中的文本和图形。在复制任何信息到 Clipboard 对象中之前,应使用 Clear 方法清除 Clipboard 对象中的内容,例如 Clipboard.Clear。

注意所有 Windows 应用程序共享 Clipboard 对象,因此当切换到其它应用程序时,剪贴板内容会改变。

Clipboard 对象可包含多段数据,只要每段数据的格式不同。例如,可用 SetData 方法把位图以 vbCFDIB 格式放到 Clipboard 中,接着再用 SetText 方法以 vbCFText 格式将文本放到 Clipboard 中。然后用 GetText 方法检索文本或用 GetData 方法检索图形。当用代码或菜单命令把另一段数据放到 Clipboard 中时,原 Clipboard 中相同格式的数据会丢失。

Clipboard 对象常数

常数 值 描述
vbCFRTF -16639 Rich Text 格式( rtf 文件)
vbCFLink -16640 DDE 对话信息
vbCFText 1 文本(.txt 文件)
vbCFBitmap 2 位图(.bmp 文件)
vbCFMetafile 3 元文件(.wmf 文件)
vbCFDIB 8 与设备无关的位图
vbCFPalette 9 调色板
vbCFEMetaFile 14 增强型图元文件(.emf 文件 )
vbCFFiles 15 来自 Windows 资源管理器的文件列表
Clipboard 对象常数 (ActiveX 控件)

常数 值 描述
vbCFLink &HBF00 DDE 对话信息
vbCFRTF &HBF01 Rich Text Format (.rtf文件)
vbCFText 1 文本 (.txt文件)
vbCFBitmap 2 位图 (.bmp文件)
vbCFMetafile 3 元文件 (.wmf文件)
vbCFDIB 8 与设备无关的位图
vbCFPalette 9 调色板
Clipboard 属性

返回一个 Clipboard 对象,该对象提供了对系统 Clipboard 的访问途径。

语法

Clipboard

说明

Clipboard 对象是用来对 Clipboard 上的文本和图形进行操作的。使用该对象就可以让用户把文本或者图形复制、剪切并粘贴到应用程序中。在把任何材料复制到 Clipboard 对象中之前,应先执行 Clear 方法(比如 Clipboard.Clear)来清除该对象的内容。

注意,Clipboard 对象为所有 Windows 应用程序所共享,因此,当切换到另一个应用程序时,其内容可能会被更改。

Clipboard 对象可以包含许多数据片段,只要每个片段都有不同的格式。例如,可以用 SetData 方法将一个具有 vbCFDIB 格式的位图放到的 Clipboard 上,然后用 SetText 方法将具有 vbCFText 格式的文本放到 Clipboard 上。然后就可用 GetText 方法获取文本或用 GetData 方法获取图形了。不论是通过代码还是菜单命令,只要把具有同样格式的另一个数据集合放到 Clipboard 上去,Clipboard 上的数据就会丢失。
参考技术A 可以正确使用的源代码如下:
‘以下代码放在窗体中的一个command中:
Private Sub Command1_Click()
On Error GoTo N1
Dim File As String
Dim Path As String
Dim i As Long, n As Long

Path = "d:\"
File = Path & "1.jpg"
Text1.Text = File
'拷贝文件到Clipboard
If clipCopyFiles(File) Then
MsgBox "拷贝文件成功.", , "Success"
Else
N1:
MsgBox "无法拷贝文件...", , "Failure"
End If
End Sub

’以下代码放在模块中:
Option Explicit

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 Lib "kernel32" Alias "RtlMoveMemory" _
' (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Sub CopyMemory Lib "kernel32" 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
Private Const CF_DIF = 5
Private Const CF_TIFF = 6
Private Const CF_OEMTEXT = 7
Private Const CF_DIB = 8
Private Const CF_PALETTE = 9
Private Const CF_PENDATA = 10
Private Const CF_RIFF = 11
Private Const CF_WAVE = 12
Private Const CF_UNICODETEXT = 13
Private Const CF_ENHMETAFILE = 14
Private Const CF_HDROP = 15
Private Const CF_LOCALE = 16
Private Const CF_MAX = 17

' New shell-oriented clipboard formats
'Private Const CFSTR_SHELLIDLIST As String = "Shell IDList Array"
'Private Const CFSTR_SHELLIDLISTOFFSET As String = "Shell Object Offsets"
'Private Const CFSTR_NETRESOURCES As String = "Net Resource"
'Private Const CFSTR_FILEDESCRIPTOR As String = "FileGroupDescriptor"
'Private Const CFSTR_FILECONTENTS As String = "FileContents"
'Private Const CFSTR_FILENAME As String = "FileName"
'Private Const CFSTR_PRINTERGROUP As String = "PrinterFriendlyName"
'Private Const CFSTR_FILENAMEMAP As String = "FileNameMap"

' 内存操作定义
Private Const GMEM_FIXED = &H0
Private Const GMEM_MOVEABLE = &H2
Private Const GMEM_NOCOMPACT = &H10
Private Const GMEM_NODISCARD = &H20
Private Const GMEM_ZEROINIT = &H40
Private Const GMEM_MODIFY = &H80
Private Const GMEM_DISCARDABLE = &H100
Private Const GMEM_NOT_BANKED = &H1000
Private Const GMEM_SHARE = &H2000
Private Const GMEM_DDESHARE = &H2000
Private Const GMEM_NOTIFY = &H4000
Private Const GMEM_LOWER = GMEM_NOT_BANKED
Private Const GMEM_VALID_FLAGS = &H7F72
Private Const GMEM_INVALID_HANDLE = &H8000
Private Const GHND = (GMEM_MOVEABLE Or GMEM_ZEROINIT)
Private Const GPTR = (GMEM_FIXED Or GMEM_ZEROINIT)

Private Const FO_COPY = &H2

Private Type POINTAPI
x As Long
y As Long
End Type

Private Type DROPFILES
pFiles As Long
pt As POINTAPI
fNC As Long
fWide As Long
End Type

Public Function clipCopyFiles(File As String) As Boolean
On Error Resume Next
Dim data As String
Dim df As DROPFILES
Dim hGlobal As Long
Dim lpGlobal As Long
Dim i As Long
data = ""
'清除剪贴版中现存的数据
If OpenClipboard(0&) Then
Call EmptyClipboard

data = data & File & vbNullChar

hGlobal = GlobalAlloc(GMEM_MOVEABLE, Len(df) + Len(data))

If hGlobal Then
lpGlobal = GlobalLock(hGlobal)

df.pFiles = Len(df)
Call CopyMemory(ByVal lpGlobal, df, Len(df))
Call CopyMemory(ByVal (lpGlobal + Len(df)), ByVal data, Len(data))

Call GlobalUnlock(hGlobal)

If SetClipboardData(CF_HDROP, hGlobal) Then
clipCopyFiles = True
End If

' Call GlobalFree(hGlobal)
End If
Call CloseClipboard
End If
End Function

Public Function clipPasteFiles(Files() As String) As Long
On Error GoTo N1
Dim hDrop As Long
Dim nFiles As Long
Dim i As Long
Dim desc As String
Dim filename As String
Dim pt As POINTAPI
Dim tfStr As SHFILEOPSTRUCT
Const MAX_PATH As Long = 260

'确定剪贴版的数据格式是文件,并打开剪贴版
If IsClipboardFormatAvailable(CF_HDROP) Then
If OpenClipboard(0&) Then
hDrop = GetClipboardData(CF_HDROP)
'获得文件数
nFiles = DragQueryFile(hDrop, -1&, "", 0)

ReDim Files(0 To nFiles - 1) As String
filename = Space(MAX_PATH)

tfStr.wFunc = FO_COPY
' tfStr.pTo = Form1.File1.Path

For i = 0 To nFiles - 1
Call DragQueryFile(hDrop, i, filename, Len(filename))
Files(i) = TrimNull(filename)
tfStr.pFrom = Files(i)
SHFileOperation tfStr
Next i
' Form1.File1.Refresh
' Form1.Dir1.Refresh

Call CloseClipboard
End If
clipPasteFiles = nFiles
End If
N1:
End Function

Private Function TrimNull(ByVal StrIn As String) As String
Dim nul As Long

nul = InStr(StrIn, vbNullChar)
Select Case nul
Case Is > 1
TrimNull = Left(StrIn, nul - 1)
Case 1
TrimNull = ""
Case 0
TrimNull = Trim(StrIn)
End Select
End Function

只要修改路径和文件名就可以用了。如果还有什么不懂,请加QQ:827903713
参考技术B 什么意思?监测剪贴板?然后复制粘贴,还是获得剪贴板中文件的路径?

获取剪贴板中文件路径:http://topic.csdn.net/t/20020124/21/497325.html
用剪贴板复制粘贴文件:http://www.moon-soft.com/download/soft/1567.htm

我也没有测试过……麻烦把功能再说清楚下谢谢。本回答被提问者采纳
参考技术C 不会api,不过没有必要用api吧 filesystemobject 不够用吗?

filecopy 也行哦!
name ...as.... 也有剪切的功能啊!

给你找个例程,你好好研究下:
http://www.92vb.com/root/Soft/WindowsAPI/2008/07/28/16240526.html

参考资料:http://www.92vb.com/root/Soft/WindowsAPI/2008/07/28/16240526.html

参考技术D 他们回答向来未达到你的要求.
任意文件的复制、粘贴文件,有点复杂,无API函数可以用.但可自己做剪贴板,要看你的VB功底啦!!!
已经透露核心技术了,你要给分哦!

BCB中利用剪贴板复制粘贴

    将一个字符串拷贝到剪贴版中的不同方法:
#include <vcl/Clipbrd.hpp>
// 方法一:
    TClipboard *pClipboard = new TClipboard();
    pClipboard->AsText = "拷贝到剪贴版的文字数据";
    delete pClipboard;
// 方法二:
    TClipboard *pClipboard = new TClipboard();
    pClipboard->SetTextBuf("拷贝到剪贴版的文字数据");
    delete pClipboard;
// 方法三:
    Clipboard()->SetTextBuf("拷贝到剪贴版的文字数据");
// 方法四:
    Clipboard()->AsText = "拷贝到剪贴版的文字数据";


// 将文件Copy到剪贴板
bool CopyFileToClip(LPCSTR pcFile)
{
    // sFile为拷贝文件名,多个文件以/0分隔,以/0/0结束
    int nLen;
    BYTE *pData;
    HGLOBAL hGbl;
    DROPFILES df;
    if(!pcFile || !pcFile[0])
        return false;
    if(!OpenClipboard(0))
        return false;

    EmptyClipboard();
    df.fNC = false;
    df.fWide = false;
    df.pFiles = sizeof(df);
    df.pt.x = df.pt.y = 0;

    for(nLen=0; pcFile[nLen]!=0; )
        nLen += strlen(pcFile + nLen) + 1;
    nLen += 1; //最后结尾的第二个/0字符
    hGbl = GlobalAlloc(GHND, sizeof(df) + nLen);
    pData = (BYTE*)GlobalLock(hGbl);
    if(pData != NULL)
    {
        memcpy(pData, &df, sizeof(df));
        memcpy(pData + sizeof(df), pcFile, nLen);
        GlobalUnlock(hGbl);
        if(!SetClipboardData(CF_HDROP, hGbl))
        {
            GlobalFree(hGbl);
            hGbl = NULL;
        }
    }
    else if(hGbl != NULL)
    {
        GlobalFree(hGbl);
        hGbl = NULL;
    }
    CloseClipboard();
    return (hGbl != NULL);
}
//---------------------------------------------------------------------------
// 从剪贴板Paste出文件
bool PasteFileFromClip(LPCSTR pcDesDir)
{
    int nLen;
    bool bReturn;
    LPCSTR lpcData;
    HGLOBAL hGbl;
    AnsiString strTemp;
    DROPFILES *pdf;
    SHFILEOPSTRUCT fos;

    if(!pcDesDir || !pcDesDir[0])
        return false;

    if(!OpenClipboard(0))
        return false;
    hGbl = GetClipboardData(CF_HDROP);
    pdf = hGbl? (DROPFILES*)GlobalLock(hGbl): NULL;
    if(pdf == NULL)
    {
        CloseClipboard();
        return false;
    }
    lpcData = (LPCSTR)pdf + pdf->pFiles;

    memset(&fos, 0, sizeof(fos));
    if(pdf->fWide)
    {
        strTemp = WideCharLenToString((wchar_t*)lpcData, GlobalSize(hGbl)-pdf->pFiles);
        fos.pFrom = strTemp.c_str();
    }
    else
        fos.pFrom = lpcData;
    fos.wFunc = FO_COPY;
    fos.pTo = pcDesDir;
    bReturn = SHFileOperation(&fos)==0;
    GlobalUnlock(hGbl);
    CloseClipboard();
    return bReturn;
}
// if(CopyFileToClip("C://1.txt/0C://Test.exe/0"))
// PasteFileFromClip("D://");

以上是关于VB如何利用剪贴板复制、粘贴文件,用到啥API的主要内容,如果未能解决你的问题,请参考以下文章

用VB实现复制粘贴

制作电影票选购图案例中,如何利用islide插件进行复制

VB中 如何复制Text1.text的字体到剪贴板? 如何剪切?

BCB中利用剪贴板复制粘贴

如何通过python复制xlsx,rar等格式的文件到剪贴板中?

vb如何做剪切、复制、粘贴按钮