打开文件对话框 [英] Open file dialog

查看:120
本文介绍了打开文件对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个没有过滤器或其他任何内容的openfile对话框(仅选择文件),然后将文件的目录写入文本框或字符串中
我在VC ++ 2010上
在此先感谢

I want to make an openfile dialog without filters or anything (only choosing the file) and writes the director of the file into a textbox or string
Am on VC++ 2010
thanks in advance

推荐答案

<pre lang="midl">openFileDialog1->ShowDialog();<br />
textBox1->Text = openFileDialog1->FileName;</pre><br />


尽管该示例最有可能运行良好,但它需要.NET和问题关于C ++.
如果您不想使用.NET,则可以使用以下内容:

While that example most likely will work fine, it requires .NET and the question was about C++.
If you wouldn''t want to use .NET, you could use something like this:

BOOL GetFileName(char *filename,int maxlen)
{	OPENFILENAME ofn = {0};

	ofn.lStructSize  = sizeof(ofn);
	ofn.Flags        = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
	ofn.hInstance    = GetModuleHandle(0);
	ofn.lpstrFile    = filename;
	ofn.nMaxFile     = maxlen;
	ofn.nFilterIndex = 1;
	ofn.lpstrFilter	 = "Any file\0*.*\0\0";
	ofn.lpstrDefExt	 = "*";
	return GetOpenFileName(&ofn);
}


char filename[MAX_PATH];
if(!GetFileName(filename,MAX_PATH))
	MessageBox(0,"Canceled by user."," ",0);
else{
	// Do what you want to do with your file
}



现在从filename中提取路径,例如,您可以使用 PathRemoveFileSpec [ ^ ].
如果您只想获取目录,即如果您对文件名不感兴趣,则也可以使用



Now to extract the path from filename, you could for example use PathRemoveFileSpec[^].
If getting a directory is all you want to do, i.e. if you''re not interested in the name of the file, you could also select a directory right away with SHBrowseForFolder[^].


这篇关于打开文件对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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