Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MFC -adding extension in Save As

Status
Not open for further replies.

rachelason

Programmer
Jun 28, 2004
114
0
0
GB
Hi All,
I would like to add extensions(more than one) when the user clicks on 'SaveAs' option. At the moment i have only one and i can't find a way to add more. The readme doc says i need to implement in serialize method of doc file. But i have no idea about the code.
Any help?
Thanks
 
hi, i donnt understand exactly what kind of extensions yoy mean, but the serialization involves the data stream between app and disk so i don't think is what you need...can u be more precise in your request?
 
Do you mean this: 1) User chooses Save As menu item in an application you're writing. 2) Dialog pops up. You want to add more lines to the wildcard/type combobox.

If so, you need to add a parameter to the structure passed to the common library routine that displays the dialog.

This parameter should be something like:

"*.*\0All Files\0*.txt\0Text Files\0*.package\0Sims2 Files\0\0"

Basically each file type has 2 parts, the wild card and the description. After listing all your pairs with nul characters between them, add a final nul at the end so the dialog knows when to stop.
 
Thanks for you reply Miros.
Yes, I am trying to add file type in the Save As menu. MSDN documentation says use a semicolon to differentiate each file type. So in the 'String Table' in resources view, i have tried to add *.txt;*.xml but it wouldn't accept. And what i lack is some example to show me how it works.
The IDR_<name>Type in my String Table at the moment says
\nReportGen\nReportGen\nReport Generator Files(*.SDR)\n.SDR\nReportGen.Document.
I tried different ways to add txt files, but it wouldn't work.

You say 'add a parameter to the sturcture passed to the commmon library routine'. Where is this commom library routine located?
thanks
 
Are you doing MFC or C/C++ without MFC?

If the second, I can post an example. The first, I'll need to track down my MFC book.
 
if tis MFC then something along the lines of...
Code:
CFileDialog dlg(FALSE,NULL,"Directory Selection",OFN_ENABLESIZING|OFN_EXPLORER|OFN_FILEMUSTEXIST,"Text (*.txt)|*.txt|All Files (*.*)|*.*||",this);
if (dlg.DoModal() == IDOK)
{
    CString fileName = dlg.GetFileName();
}
the bit I think you are interested in is the...
Code:
"Text (*.txt)|*.txt|All Files (*.*)|*.*||"
to add .bat to the above for example
Code:
"Batch (*.bat)|*.bat|Text (*.txt)|*.txt|All Files (*.*)|*.*||"
hope this helps.
O.B
 
And C/C++ Windows GUI works the same way with nul (\0) in place of the pipes (|).
 
HI!
I AM working on MFC. I tried creating another ID in the string table with .txt in it(also modifying related places). It works only on 'File open' but not on 'save As'.
I tried to override CDocument::OnFileSave. Added this code with the existing code.
<code>
CString textFilter;
textFilter = _T("Text Files (*.TXT)");
// VERIFY(allFilter.LoadString(AFX_IDS_ALLFILTER));
strFilter += textFilter;
strFilter += (TCHAR)'\0'; // next string please
strFilter += _T("*.TXT");
strFilter += (TCHAR)'\0'; // last string
dlgFile.m_ofn.nMaxCustFilter++;
</code>

File Save As shows *.txt and *.* - Fine. But when i try saving it as .txt, it tries to save it as *.sdr(which i have provided in the application wizard). I am sure there is some easy way to solve this problem. I will be very surprised if Microsoft has added windows programming so easy through MFC but not included the bit i am struggling with.

To OlafBogus.... I am not using any additional Dialog box. Do you mean adding this code in the program(where?Serialize() or OnNewDocument()), would bring up the SaveAs dialog box with a .txt or any .<extn>? I tried , and the compiler does not like the object 'this' as it cannot convert myDoc to CWnd.

Let me explain my problem again in a simplest way.. I have created an MFC program (through application wizard)which opens explorer with a site. When i run the program,i click on 'Save As', i would like it to display additional filetypes apart from
all Files(*.*) (we can ignore this bit as it may be confusing)and anyother i have provided in the application wizard. Any idea?
thanks
 
What if you add it to the string in the string table in your resource file?
 
i belive to understand that u wanna set a filter for estensions in Save As dialog...so u have to:
1)set a filter
2)create the common dialog window
3)Call the DoModal method

here is the code:

static char BASED_CODE szFilter[] = "Bitmap Files (*.bmp)|*.bmp||";

CFileDialog m_ldFile(FALSE, ".bmp", "DefaultFileName",
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter); m_ldFile.DoModal() ;


So szFilter is the filter for the file type desired,m_ldFile is the common dialog variable with the right parameters.Finally DoModal on it.
Look at first parameter of m_ldFile constructor:it's FALSE.
In this case the window is SAVE AS.In case it's TRUE the window will be OPEN AS.
I think it's all.
 
this is the code to handle the common dialog window save as with related filter type of files,but the programmer have to implement the serialization of desired data.
i think | pipes isn't the same of /0 that represent the string terminated character.
 
Thanks MarcoMb, the code seems to work well and very simpler than the one i posted. Now my task is to save xml files in text format in the serialise method.
Thanks everyone for you help.
Rach
 
I just posted an example of how to write an XML file in
Basically, you'd do your file open and header writes before starting the serialize and the write of the final tag and close the files after the serialize is finished. During your serialize you'd write one XML tag for your data record.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top