MFC:DragQueryFile函数

4 篇文章 0 订阅

原文: http://blog.sina.com.cn/s/blog_6294abe701012c2z.html


DragQueryFile函数  Retrieves the

names of dropped files that result from a successful drag-and-drop operation.
  用于一个成功文件拖拽后获取文件名称。
  Syntax 语法:
  
  UINT DragQueryFile(      
  HDROP hDrop, 
  UINT iFile,
  LPTSTR lpszFile,
  UINT cch
  );
  Parameters 参数
  
  hDrop
  Identifier of the structure containing the file names of the dropped files.
  用于区分”包含被拖拽文件名称结构”的句柄。
  即存放所拖放文件名称的数据结构的句柄,也就是文件名缓冲区的句柄
  iFile
  Index of the file to query. If the value of the iFile parameter is 0xFFFFFFFF,
DragQueryFile returns a count of the files dropped. If the value of the iFile
parameter is between zero and the total number of files dropped, DragQueryFile
copies the file name with the corresponding value to the buffer pointed to by
the lpszFile parameter.
  文件索引编号(用于指明所要查询文件的序号, 如果拖进多个文件,则索引编号从零开始),如果iFile值为 0xFFFFFFFF
时,返回的是拖曳到窗体上的文件的个数。如果iFile值在0和拖拽文件总数之间时,DragQueryFile拷贝与文件名存储缓冲区大小适应的文件名称到缓冲区中。
  lpszFile
  Address of a buffer to receive the file name of a dropped file when the
function returns. This file name is a null-terminated string. If this parameter
is NULL, DragQueryFile returns the required size, in characters, of the buffer.
  函数返回时,用于存储拖拽文件名称的缓冲区指针。文件名称是一个以空终止“/0”结尾的字符串。如果此参数是NULL,DragQueryFile函数返回拖拽的文件数目。函数DragQueryFile得到的文件名,是带完整路径的文件名。
  cch
  Size, in characters, of the lpszFile buffer.
  存储拖拽文件名称缓冲区的大小,即lpszFile指针所指缓冲区的字符数。
  Return Value 返回值
  
  When the function copies a file name to the buffer, the return value is a
count of the characters copied, not including the terminating null character.
  如果函数拷贝文件名称到缓冲区中,返回值就是拷贝的字符数,不包括终止的NULL字符。
  If the index value is 0xFFFFFFFF, the return value is a count of the dropped
files. Note that the index variable itself returns unchanged, and will therefore
remain 0xFFFFFFFF.
  如果文件索引值是0xFFFFFFFF,则返回值是被拖拽的文件总数,注意文件索引变量的值将保持不变,依然为0xFFFFFFFF。
  If the index value is between zero and the total number of dropped files and
the lpszFile buffer address is NULL, the return value is the required size, in
characters, of the buffer, not including the terminating null character.
  如果文件索引值在0和拖拽文件总数之间时,并且lpszFile值为NULL时
,返回值是存储此被拖拽文件的名称所需要的缓冲区大小值,此值是不包括终止NULL字符的字符数。
  Windows 95/98/Me: DragQueryFile is supported by the Microsoft Layer for
Unicode. To use this, you must add certain files to your application, as
outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.
  Function Information
      Minimum DLL Versionshell32.dll version 4.0 or later
      Custom ImplementationNo
      Headershellapi.h
      Import libraryshell32.lib
      Minimum operating systemsWindows NT 3.1, Windows 95
      UnicodeImplemented as ANSI and Unicode versions.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 MFC 中实现文件拖拽发送,可以按照以下步骤进行: 1. 在 MFC 对话框的资源视图中添加一个编辑框控件,用于显示拖拽的文件路径。 2. 双击对话框类的头文件 (.h),在类定义中添加以下成员变量和方法: ```cpp CString m_strFilePath; // 保存拖拽的文件路径 afx_msg void OnDropFiles(HDROP hDropInfo); // 拖拽文件的消息处理函数 ``` 3. 在对话框类的源文件 (.cpp) 中添加以下代码: ```cpp BEGIN_MESSAGE_MAP(CYourDialog, CDialogEx) ON_WM_DROPFILES() END_MESSAGE_MAP() CYourDialog::CYourDialog(CWnd* pParent /*=nullptr*/) : CDialogEx(IDD_YOUR_DIALOG, pParent) { // ... } void CYourDialog::OnDropFiles(HDROP hDropInfo) { // 获取拖拽的文件数目 UINT nFiles = ::DragQueryFile(hDropInfo, 0xFFFFFFFF, nullptr, 0); // 获取第一个文件的路径长度 UINT nPathLen = ::DragQueryFile(hDropInfo, 0, nullptr, 0) + 1; // 获取第一个文件的路径 LPWSTR lpwstrFilePath = new WCHAR[nPathLen]; ::DragQueryFile(hDropInfo, 0, lpwstrFilePath, nPathLen); // 将文件路径保存到成员变量中 m_strFilePath = lpwstrFilePath; // 更新编辑框中的文本显示 GetDlgItem(IDC_EDIT_FILEPATH)->SetWindowTextW(m_strFilePath); delete[] lpwstrFilePath; // 调用基类的消息处理函数 CDialogEx::OnDropFiles(hDropInfo); } ``` 4. 在对话框的 OnInitDialog() 函数中添加以下代码,启用文件拖拽功能: ```cpp BOOL CYourDialog::OnInitDialog() { CDialogEx::OnInitDialog(); // 启用文件拖拽功能 DragAcceptFiles(TRUE); // ... return TRUE; } ``` 这样,当用户将文件拖拽到对话框上时,文件路径会显示在编辑框中。你可以进一步自定义发送文件的逻辑,例如在拖拽结束后将文件发送到指定的目标。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值