0

如果用户没有这样做,我想设置文件的扩展名并将文件名和扩展组合框字符串连接起来。是否有一些简单的方法可以做到这一点,或者我需要一些“钩子”?

    OPENFILENAME ofn;       // common dialog box structure
    TCHAR szFile[260];       // buffer for file name                      
    HANDLE hFile;              // file handle

    // Initialize OPENFILENAME
    ZeroMemory(&ofn, sizeof(ofn));
    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = hwnd;
    ofn.lpstrFile = szFile;
    // Set lpstrFile[0] to '\0' so that GetOpenFileName does not 
    // use the contents of szFile to initialize itself.
    ofn.lpstrFile[0] = 0;
    ofn.nMaxFile = sizeof(szFile);
    ofn.lpstrFilter = _T("Текстові файли(*.txt)\0*.txt\0Word(*.doc)\0*.doc\0");
    ofn.nFilterIndex = 1;
    ofn.lpstrFileTitle = NULL;
    ofn.nMaxFileTitle = 0;
    ofn.lpstrInitialDir = NULL;
    ofn.Flags = OFN_EXPLORER;   

    if(GetSaveFileName(&ofn) == TRUE)
    {
        hFile = CreateFile( szFile,
            GENERIC_WRITE,
            0,
            (LPSECURITY_ATTRIBUTES)NULL,
            CREATE_NEW,
            FILE_ATTRIBUTE_NORMAL,
            (HANDLE)NULL);

        int editLen = Edit_GetTextLength(edit);
        TCHAR* buffer = new TCHAR[editLen+1];
        DWORD wroteLen = 0;

        Edit_GetText(edit,buffer,editLen+1);
        //Set extension if need

        ///////////////////////
        if(!WriteFile(hFile,buffer,editLen*sizeof(TCHAR),&wroteLen,NULL))
            MessageBox(hwnd,_T("File wasn`t saved.."),_T("Error"),MB_ICONERROR);

        CloseHandle(hFile);
        delete[]buffer;

感谢您的回复!))

4

2 回答 2

2

您要设置lpstrDefExt成员:

来自MSDN

类型:LPCTSTR

默认扩展名。

如果用户未能键入扩展名,GetOpenFileName 和 GetSaveFileName 会将此扩展名附加到文件名中。此字符串可以是任意长度,但仅附加前三个字符。该字符串不应包含句点 (.)。如果此成员为 NULL 并且用户未能键入扩展名,则不会附加任何扩展名。

于 2012-09-26T21:01:58.817 回答
2

ofn.nFilterIndex设置为文件扩展名组合框选择的从 1 开始的索引。

于 2012-09-26T22:41:11.673 回答