将GetOpenFileName()与multiselect标志一起使用时,如何获取所选文件的列表? [英] How to get list of selected files when using GetOpenFileName() with multiselect flag?

查看:48
本文介绍了将GetOpenFileName()与multiselect标志一起使用时,如何获取所选文件的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用谷歌搜索,但是人们似乎也遇到了同样的问题:我们无法获得所选文件的列表.

I have tried googling, but people seem to have the same problem: we can't get a list of the selected files.

这是一段简单的工作代码,类似于我使用的代码:

This is a simple piece of working code that is similar to what I use:

OPENFILENAME ofn = { sizeof ofn };
wchar_t file[1024];
file[0] = '\0';
ofn.lpstrFile = file;
ofn.nMaxFile = 1024;
ofn.Flags = OFN_ALLOWMULTISELECT | OFN_EXPLORER;
GetOpenFileName(&ofn);

实际上如何获得所选的文件名?目前,我只能在没有OFN_ALLOWMULTISELECT标志的情况下使用它,因此它将选择的一个文件名返回到 ofn.lpstrFile 中.我试图打印出该结构中的所有字符串变量,但未发现任何结果.它仅显示所选文件的主文件夹.

How do I actually get the filenames I selected? Currently I can only get it to work without OFN_ALLOWMULTISELECT flag, so it returns the one selected filename into ofn.lpstrFile. I tried to print out all the string variables inside that struct, but found nothing. It only shows the main folder of the selected files.

推荐答案

看起来 ofn.lpstrFile 包含所有文件名,以NULL分隔并以另一个NULL结束(有效地以空字符串).

It looks like the ofn.lpstrFile contains all the filenames, separated with a NULL and ending with another NULL (effectively ending with an empty string).

如果设置了OFN_ALLOWMULTISELECT标志并且用户选择了多个文件,则缓冲区将包含当前目录,后跟所选文件的文件名.对于资源管理器风格的对话框,目录和文件名字符串用NULL分隔,最后一个文件名后带有一个额外的NULL字符.对于旧式对话框,这些字符串用空格分隔并且该函数使用短文件带有空格的文件名的名称.您可以使用FindFirstFile函数在长文件名和短文件名之间进行转换.如果用户仅选择一个文件,则lpstrFile字符串在路径和文件名之间没有分隔符.

If the OFN_ALLOWMULTISELECT flag is set and the user selects multiple files, the buffer contains the current directory followed by the file names of the selected files. For Explorer-style dialog boxes, the directory and file name strings are NULL separated, with an extra NULL character after the last file name. For old-style dialog boxes, the strings are space separated and the function uses short file names for file names with spaces. You can use the FindFirstFile function to convert between long and short file names. If the user selects only one file, the lpstrFile string does not have a separator between the path and file name.

来自 MSDN.

解析内容的可能实现可能是

A possible implementation to parse the contents could be;

wchar_t* str = ofn.lpstrFile;
std::wstring directory = str;
str += ( directory.length() + 1 );
while ( *str ) {
  std::wstring filename = str;
  str += ( filename.length() + 1 );
  // use the filename, e.g. add it to a vector
}

这篇关于将GetOpenFileName()与multiselect标志一起使用时,如何获取所选文件的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆