简体   繁体   English

如何通过Win32 API函数复制文件并通过(Ctrl + v)粘贴到我的桌面中

[英]How to copy files by win32 API functions and paste by (Ctrl+v) in my Desktop

I want to copy a file by win32 API Functions and "paste" it by (Ctrl+c) in my desktop of other folder of windows. 我想通过win32 API函数复制文件,并通过(Ctrl + c)“粘贴”到Windows其他文件夹的桌面中。

I Know CopyFileEx and copies file with it but this function copy and paste. 我知道CopyFileEx并使用它复制文件,但是可以复制粘贴此功能。 I want to just copy file in my main() program (by win32 API function) and "paste" it in desktop or other folder of windows. 我只想复制文件到main()程序中(通过win32 API函数),然后将其“粘贴”到桌面或其他Windows文件夹中。

I don't want to use SHFileOperation function. 我不想使用SHFileOperation函数。

If you want the actual file to be copied to the destination folder when the clipboard content is pasted to it, you need to put the path string onto the clipboard using the CF_HDROP format, eg: 如果要在粘贴剪贴板内容时将实际文件复制到目标文件夹,则需要使用CF_HDROP格式将路径字符串放入剪贴板,例如:

// error handling omitted for brevity...

LPWSTR path = ...;
int size = sizeof(DROPFILES)+((lstrlenW(path)+2)*sizeof(WCHAR));
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, size);
DROPFILES *df = (DROPFILES*) GlobalLock(hGlobal);
ZeroMemory(df, size);
df->pFiles = sizeof(DROPFILES);
df->fWide = TRUE;
LPWSTR ptr = (LPWSTR) (df + 1);
lstrcpyW(ptr, path);
GlobalUnlock(hGlobal);
SetClipboardData(CF_HDROP, hGlobal);

Update: Alternatively, see the following article on the "The Old New Thing" blog: 更新:或者,请参阅“旧事物”博客上的以下文章:

Copying a file to the clipboard so you can paste it into Explorer or an email message or whatever 将文件复制到剪贴板,以便将其粘贴到资源管理器或电子邮件中

It uses GetUIObjectOfFile() and OleSetClipboard() instead of CF_HDROP . 它使用GetUIObjectOfFile()OleSetClipboard()代替CF_HDROP But the end effect is similar - an object placed on the clipboard that represents the target file, and Explorer recognizes that object during a paste operation so it will copy the file to the folder being pasted in. 但是最终效果是相似的-放置在剪贴板上的代表目标文件的对象,资源管理器在粘贴操作期间会识别该对象,因此它将文件复制到要粘贴的文件夹中。

Here's a minimal working example (console application). 这是一个最小的工作示例(控制台应用程序)。 It compiles for both unicode and none unicode environments. 它针对unicode和非unicode环境进行编译。 As this is kind of minimal there's no error handling of any kind (usage see below). 因为这是最小的,所以没有任何类型的错误处理(用法请参见下文)。

#include <Windows.h>
#include <Shlobj.h> // DROPFILES
#include <tchar.h>  // e.g. _tcslen

int _tmain(int argc, TCHAR* argv[])
{
    // calculate *bytes* needed for memory allocation
    int clpSize = sizeof(DROPFILES);
    for (int i = 1; i < argc; i++)
        clpSize += sizeof(TCHAR) * (_tcslen(argv[i]) + 1); // + 1 => '\0'
    clpSize += sizeof(TCHAR); // two \0 needed at the end

    // allocate the zero initialized memory
    HDROP hdrop   = (HDROP)GlobalAlloc(GHND, clpSize);
    DROPFILES* df = (DROPFILES*)GlobalLock(hdrop);
    df->pFiles    = sizeof(DROPFILES); // string offset
#ifdef _UNICODE
    df->fWide     = TRUE; // unicode file names
#endif // _UNICODE

    // copy the command line args to the allocated memory
    TCHAR* dstStart = (TCHAR*)&df[1];
    for (int i = 1; i < argc; i++)
    {
        _tcscpy(dstStart, argv[i]);
        dstStart = &dstStart[_tcslen(argv[i]) + 1]; // + 1 => get beyond '\0'
    }
    GlobalUnlock(hdrop);

    // prepare the clipboard
    OpenClipboard(NULL);
    EmptyClipboard();
    SetClipboardData(CF_HDROP, hdrop);
    CloseClipboard();

    return 0;
}

Usage: 用法:

fclip.exe "C:\full\path\to\file.dat" "C:\more\files.dat"

That's it! 而已! Feel free to press ctrl + v to paste the files. 随意按ctrl + v粘贴文件。

I think the Function you are looking for is this: Windows CopyFile 我认为您正在寻找的功能是这样的:Windows CopyFile

Example: 例:

BOOL WINAPI CopyFile(
  _In_  LPCTSTR lpExistingFileName,
  _In_  LPCTSTR lpNewFileName,
  _In_  BOOL bFailIfExists
);

Normally a path is a String, so how do you want to paste a String to your Desktop? 通常,路径是字符串,那么如何将字符串粘贴到桌面? You only can copy a file and paste it to your Desktop, but not a path (String)! 您只能复制文件并将其粘贴到桌面上,而不能复制路径(字符串)!

This is how you copy stuff to your clipboard: 这是将内容复制到剪贴板的方式:

BOOL WINAPI EditCopy(VOID) 
{ 
  PLABELBOX pbox; 
  LPTSTR  lptstrCopy; 
  HGLOBAL hglbCopy; 
  int ich1, ich2, cch; 

  if (hwndSelected == NULL) 
      return FALSE; 

  // Open the clipboard, and empty it. 

  if (!OpenClipboard(hwndMain)) 
      return FALSE; 
  EmptyClipboard(); 

  // Get a pointer to the structure for the selected label. 

  pbox = (PLABELBOX) GetWindowLong(hwndSelected, 0); 

  // If text is selected, copy it using the CF_TEXT format. 

  if (pbox->fEdit) 
  { 
      if (pbox->ichSel == pbox->ichCaret)     // zero length
      {   
          CloseClipboard();                   // selection 
          return FALSE; 
      } 

      if (pbox->ichSel < pbox->ichCaret) 
      { 
          ich1 = pbox->ichSel; 
          ich2 = pbox->ichCaret; 
      } 
      else 
      { 
          ich1 = pbox->ichCaret; 
          ich2 = pbox->ichSel; 
      } 
      cch = ich2 - ich1; 

      // Allocate a global memory object for the text. 

      hglbCopy = GlobalAlloc(GMEM_MOVEABLE, 
          (cch + 1) * sizeof(TCHAR)); 
      if (hglbCopy == NULL) 
      { 
          CloseClipboard(); 
          return FALSE; 
      } 

      // Lock the handle and copy the text to the buffer. 

      lptstrCopy = GlobalLock(hglbCopy); 
      memcpy(lptstrCopy, &pbox->atchLabel[ich1], 
          cch * sizeof(TCHAR)); 
      lptstrCopy[cch] = (TCHAR) 0;    // null character 
      GlobalUnlock(hglbCopy); 

      // Place the handle on the clipboard. 

      SetClipboardData(CF_TEXT, hglbCopy); 
  } 

  // If no text is selected, the label as a whole is copied. 

  else 
  { 
      // Save a copy of the selected label as a local memory 
      // object. This copy is used to render data on request. 
      // It is freed in response to the WM_DESTROYCLIPBOARD 
      // message. 

      pboxLocalClip = (PLABELBOX) LocalAlloc( 
          LMEM_FIXED, 
          sizeof(LABELBOX) 
      ); 
      if (pboxLocalClip == NULL) 
      { 
          CloseClipboard(); 
          return FALSE; 
      } 
      memcpy(pboxLocalClip, pbox, sizeof(LABELBOX)); 
      pboxLocalClip->fSelected = FALSE; 
      pboxLocalClip->fEdit = FALSE; 

      // Place a registered clipboard format, the owner-display 
      // format, and the CF_TEXT format on the clipboard using 
      // delayed rendering. 

      SetClipboardData(uLabelFormat, NULL); 
      SetClipboardData(CF_OWNERDISPLAY, NULL); 
      SetClipboardData(CF_TEXT, NULL); 
  } 

  // Close the clipboard. 

  CloseClipboard(); 

  return TRUE; 
}

Code can be found here (paste code too) http://msdn.microsoft.com/en-us/library/windows/desktop/ms649016%28v=vs.85%29.aspx 可以在这里找到代码(也可以粘贴代码) http://msdn.microsoft.com/zh-cn/library/windows/desktop/ms649016%28v=vs.85%29.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM