OPENFILENAME打开对话框 [英] OPENFILENAME open dialog
本文介绍了OPENFILENAME打开对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过Win32中的打开文件"对话框获取完整的文件路径. 我通过此功能做到这一点:
i want to get a full file path by open file dialog in win32. i do it by this function:
string openfilename(char *filter = "Mission Files (*.mmf)\0*.mmf", HWND owner = NULL) {
OPENFILENAME ofn ;
char fileName[MAX_PATH] = "";
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = owner;
ofn.lpstrFilter = filter;
ofn.lpstrFile = fileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrDefExt = "";
ofn.lpstrInitialDir ="Missions\\";
string fileNameStr;
if ( GetOpenFileName(&ofn) )
fileNameStr = fileName;
return fileNameStr;
}
工作正常,并返回path.但是我无法写入该文件,而无法使用openfilename来获取该文件的路径.
it's work fine and return path . but i can't write into the that file i get path of it with openfilename.
注意: 我将这段代码称为写入文件(序列化):
note : i call this code to write to the file (serialization):
string Mission_Name =openfilename();
ofstream ofs ;
ofs = ofstream ((char*)Mission_Name.c_str(), ios::binary );
ofs.write((char *)&Current_Doc, sizeof(Current_Doc));
ofs.close();
推荐答案
尝试编写:
string s = openfilename();
HANDLE hFile = CreateFile(s.c_str(), // name of the write
GENERIC_WRITE, // open for writing
0, // do not share
NULL, // default security
CREATE_ALWAYS, // Creates a new file, always
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
DWORD writes;
bool writeok = WriteFile(hFile, &Current_Doc, sizeof(Current_Doc), &writes, NULL);
CloseHandle(hFile);
...,然后阅读:
HANDLE hFile = CreateFile(s.c_str(), // name of the write
GENERIC_READ, // open for reading
0, // do not share
NULL, // default security
OPEN_EXISTING, // Creates a new file, always
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
DWORD readed;
bool readok = ReadFile(hFile, &Current_Doc, sizeof(Current_Doc), &readed, NULL);
CloseHandle(hFile);
帮助链接:
http://msdn.microsoft.com/zh-CN/library/windows/desktop/bb540534%28v=vs.85%29.aspx
http://msdn.microsoft.com/zh-CN/library/windows/desktop/aa363858%28v=vs.85%29.aspx
这篇关于OPENFILENAME打开对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文