我正在写一个记事本程序,并实现了saveas功能(带保存对话框等)。我试图寻找一种只保存的方法(如果文件已存在则不显示保存对话框)但找不到任何内容。我该怎么办?我对saveas功能的代码是:
case ID_FILE_SAVEAS:
{
OPENFILENAME ofn;
char szFileName[ MAX_PATH ] = "";
ZeroMemory( &ofn, sizeof( ofn ) );
ofn.lStructSize = sizeof( ofn );
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrDefExt = "txt";
ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
if( GetSaveFileName( &ofn ) )
{
// DO FUN STUFF
}
}
break;
答案 0 :(得分:0)
// Prerequisite: You need to keep track of the filename that you have loaded...
case ID_FILE_SAVE:
{
if (... there is already a filename loaded ...)
{
// open the filename that is being tracked...
// write data to the file as needed...
// close the file...
break;
}
// there is no file loaded yet so fall through to the next case...
}
case ID_FILE_SAVEAS:
{
OPENFILENAME ofn = {0};
char szFileName[ MAX_PATH ] = "";
ofn.lStructSize = sizeof( ofn );
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrDefExt = "txt";
ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
if( GetSaveFileName( &ofn ) )
{
// open the selected filename...
// write data to the file as needed...
// close the file...
// track the selected filename for later use in ID_FILE_SAVE...
}
break;
}