When using the cfiledialog class of MFC to save files, I encountered a small problem. When I write a name with an extension in the file name column, cfiledialog does not determine whether the extension meets my requirements. For example, the extension I specified is. VVT. When I input a.bmp, the expected file name should be a.bmp. VVT. However, the system still returns a.bmp. VVT to me. (Word has implemented this function. you can experiment with it)
What should I do?
When I click OK, the modification will not be complete. So, Baidu, Google, msdn .... + Experiment. The final solution is as follows:
Reload onfilenameok ()
Bool cmyfiledlg: onfilenameok ()
{
......
// If the extension is not what I want, modify the following:
// Modify the window display
Cstring STR;
Ccombobox * pcmb13 = (ccombobox *) getparent ()-> getdlgitem (cmb13 );
Pcmb13-> getwindowtext (STR );
STR + = _ T (". VVT ");
Pcmb13-> setwindowtext (STR );
Pcmb13-> redrawwindow ();
// Modify the PATH value
Cstring strpathnmae = getpathname ();
Strpathnmae + = _ T (". VTF ");
# Ifdef _ Unicode
Memcpy (void *) This-> m_ofn.lpstrfile, strpathnmae. getbuffer (0), strpathnmae. getlength () * 2 + 2 );
# Else
Memcpy (void *) This-> m_ofn.lpstrfile, strpathnmae. getbuffer (0), strpathnmae. getlength () + 1 );
# Endif
// Modify the file name
Cstring strfilename = getfilename ();
Strfilename + = _ T (". VTF ");
# Ifdef _ Unicode
Memcpy (void *) This-> m_ofn.lpstrfiletitle, strfilename. getbuffer (0), strfilename. getlength () * 2 + 2 );
# Else
Memcpy (void *) This-> m_ofn.lpstrfiletitle, strfilename. getbuffer (0), strfilename. getlength () + 1 );
# Endif
// Wait. You can do it without waiting. That is, the action is too fast to be visible to the human eye.
Sleep (50 );
// Determine whether a file exists. In this case, the system does not determine whether the file exists. Therefore, you need to check whether the file exists.
//************
// Required # include <Io. h>
//************
If (_ taccess (strfilename, 0) = 0 ))
{// Exists,-1 does not exist. //
// If it exists, you must check whether it is overwritten !! What do you do?
......
}
Return cfiledialog: onfilenameok ();
}
If there are multiple file types in the list box, but I still want to select the last one during initialization (for example, *. *, the first type is selected by default ).
Method: Write the previous sentence before domodal ().
M_filedlg.getofn (). nfileterindex = 4; // 4 is *. * In the list, the first row is 1.
! ### ¥ ...... Click OK. Slow! I want to know the last line you selected? Some people say, "You can't get the last extension." If the two extensions are of the same type, for example, when you save image files with different compression ratios, they are the same. JPEG, but the compression ratio is different. In this case, only the extension will not be available. ------>
No, but the method is still:
M_filedlg.getofn (). nfileterindex, which is the row number after domodal!
Look good, it's still easy to use, but leave some homework (very useful knowledge), do it yourself.
1. What is cmb13?
2. Why redrawwindow?
3. Why does strfilename. getlength () Multiply by 2 in Unicode? Why do we need to add a number?