0

我正在编写一个记事本程序,并实现了另存为功能(带有保存对话框等)。我尝试寻找一种只保存的方法(如果文件已经存在,则不显示保存对话框)但找不到任何东西。我该怎么做?我为 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;
4

1 回答 1

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;
    }
于 2013-10-28T23:42:57.703 回答