窗口接收文件

 前段时间做文件解析工具时用到了拖曳功能,现在把实现的步骤记下来以供参考。其实很简单,就是添加个消息映射,在消息响应函数进行处理。我们要做的就是对WM_DROPFILES消息的捕捉,处理,这个消息是系统消息,可以在WINUSER.h文件中找到

实现步骤如下(我建的是对话框工程):

1、选中对话框的Properities->Extended Styles,选中Accept files

2、声明消息处理函数:

在xxDlg.h文件里添加afx_msg void OnDropFiles(HDROP hDropInfo);

3、添加消息映射

在xxDlg.cpp文件里添加:ON_MESSAGE(WM_DROPFILES,OnDropFiles)

4、定义消息处理函数

void xxDlg::::OnDropFiles(HDROP hDropInfo)
{
  char szFile[MAX_PATH];
 int n = DragQueryFile(hDropInfo, 0, szFile, sizeof(szFile));
 if(n != 1)
 {
  AfxMessageBox("Drop fails");return;
 }
 m_strDir.Format("%s", szFile);
 ParseFile(m_strDir);
                                // 我自己的函数
}

我没有对拖曳多个文件做处理,在这里我们要注意DragQueryFile()函数,稍后我会对此函数做介绍。

我本来打算让Edit框也可以接收文件,但由于水平有限没做到,希望能得到高手的指点。

msdn参考:

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.
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.
cch
Size, in characters, of the lpszFile buffer.

Return Values

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.

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.

你可能感兴趣的:(File,null,buffer,Parameters,styles,structure)