今天谈一天windows客户端拖拽上传功能。
其实主要是拖拽功能,上传是自己实现的。
DragAcceptFiles 函数
最重要的就是这个函数了,看看作用:
Registers whether a window accepts dropped files
原型:
VOID DragAcceptFiles(
HWND hWnd,
BOOL fAccept
);
参数:
hWnd
Type: HWND
The identifier of the window that is registering whether it will accept dropped files.
fAccept
Type: BOOL
A value that indicates if the window identified by the hWnd parameter accepts dropped files. This value is TRUE to accept dropped files or FALSE to discontinue accepting dropped files.
头文件以及库:
Shellapi.h
Shell32.lib
这就很简单了,首先获得一个窗口的句柄,然后调用DragAcceptFiles 函数:
DragAcceptFiles(m_hWnd, TRUE);
接下来就是消息响应了~~~
WM_DROPFILES 消息
Sent when the user drops a file on the window of an application that has registered itself as a recipient of dropped files.
现在就要处理WM_DROPFILES消息了:
DragQueryFile函数
Retrieves the names of dropped files that result from a successful drag-and-drop operation.
(在完成)一个成功拖放操作后获取被拖放文件的名称等信息。
原型:
UINT DragQueryFile(
_In_ HDROP hDrop,
_In_ UINT iFile,
_Out_ LPTSTR lpszFile,
UINT cch
);
参数:
hDrop [in]
Type: HDROP
Identifier of the structure that contains the file names of the dropped files.
iFile [in]
Type: UINT
Index of the file to query. If the value of this parameter is 0xFFFFFFFF, DragQueryFile returns a count of the files dropped. If the value of this 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.
lpszFile [out]
Type: LPTSTR
The address of a buffer that receives 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 this buffer.
cch
Type: UINT
The size, in characters, of the lpszFile buffer.
重点看返回值:
Type: UINT
A nonzero value indicates a successful call.
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.
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 therefore remains 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.
可以通过这个返回值来判断拖拽的是否为单个文件。
看到了第一个参数
HDROP
HDROP hDrop = (HDROP)wParam
DragFinish函数
Releases memory that the system allocated for use in transferring file names to the application
windows编程永远要记住,获取资源后要释放。
GetFileAttributes函数
GetFileAttributes Function为一个指定的文件或目录返回文件系统的属性。
如果判断拖拽的是否为文件夹可以这样:
GetFileAttributes(file_path)&FILE_ATTRIBUTE_DIRECTORY
最后献上菊花,手滑了,是献上完整代码:
HDROP hDrop = (HDROP)wParam;
UINT nFileNum = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0);
TCHAR file_path[MAX_PATH];
DragQueryFile(hDrop, 0, file_path, MAX_PATH);
DragFinish(hDrop); //释放hDrop
if (GetFileAttributes(file_path)&FILE_ATTRIBUTE_DIRECTORY)
{
MessageBox(NULL, L"只允许拖拽单个文件", L"拖拽文件", NULL);
}
else if (nFileNum > 1)
{
MessageBox(NULL, L"只允许拖拽单个文件", L"拖拽文件", NULL);
}
else
{
//上传单个文件
}
}