Solution to the Problem of saving the extension of the cfiledialog file in MFC

Source: Internet
Author: User

Original post see http://blog.csdn.net/shiwei0124/archive/2009/09/10/4538327.aspx

After posting on the csdn forum, I worked with people to solve the problem.

The post content is as follows:

First look at a piece of code:
Static tchar based_code szfilter [] = _ T ("Map Files (*. txt) | *. txt | mapcfg files (*. txty) | *. txty | ");
Cfiledialog DLG (false, null, null, ofn_hidereadonly | ofn_overwriteprompt ,/
Szfilter, afxgetmainwnd ());
Cstring strpath;
Cfileexception E;
// Determine whether to press the "save" button
If (DLG. domodal () = idok)
{
// Obtain the file storage path, which is convenient for the following right (4) without getfilename (); if the file is not configured with a suffix, an error occurs if the file is smaller than 4 characters
Strpath = DLG. getpathname ();
// Determine the added extension based on the selected extension drop-down box
Switch (DLG. m_ofn.nfilterindex)
{
// Txt
Case 1:
{
// Determine the file extension
If (strpath. Right (4 )! = _ T (". txt "))
Strpath + = _ T (". txt ");
Break;
}
// Txty
Case 2:
{
// Determine the file extension
If (strpath. Right (7 )! = _ T (". txty "))
Strpath + = _ T (". txty ");
Break;
}
}
If (access (strpath, 0) = 0) // # include
{
Cstring strquery;
Strquery. Format ("% s already exists. Do you want to replace it? ", Strpath );
If (IDNO =: MessageBox (m_hwnd, strquery, "File Overwrite", mb_iconquestion | mb_yesno ))
{
Onbnclickednew2 ();
}
}
In this Code, the cfiledialog contains a prompt to overwrite the file, but there is a problem. For example, I want to save a file named "Temp", but I didn't specify the suffix of this file, cfiledialog can only check whether the file in the input box has a file named "Temp" in the folder. I have added some features to the code and automatically added unwritten extensions according to the file extension filtering box, then, you can determine whether "temp.txt" exists. Here, we perform two overwrites to determine whether the "Temp" check is automatically performed in the dialog box, and the "temp.txt" check I set"
What I want to do is to judge "temp.txt" in the dialog box, instead of "Temp", that is, to automatically add the extension in the dialog box and then judge.
(This method is used when the web browser saves images .)
Does cfiledialog have any functions or parameters that can be set? Or need to be reloaded?
To answer this question, it is best to provide research on the code segment.

Replies: 13

  • Shiwei0124
  • Level 1:

# 1st floor score: 0Reply to: 15:12:16


No one answered...

Excellent recommendation: Hash: terminator of Input Method

  • Jiabin_007
  • ()
  • Level 1:

# Second floor score: 0Reply: 13:17:10

Paying attention

  • Shiwei0124
  • Level 1:

# Third floor score: 0Reply: 14:47:01

Excuse me, do you have any experts to solve this problem...

  • Oyster2008
  • (Oyster2008)
  • Level 1:

# 4th floor score: 0Reply: 14:55:24

I think we should rewrite the onfilenamechange or onfilenameok of cfiledialog.

  • Oyster2008
  • (Oyster2008)
  • Level 1:

# Fifth floor score: 0Reply: 15:15:54

I saw it in vc2008 just now. cfiledialog will add the suffix name and determine whether the file exists? No. What version of MFC do you use?

  • Shiwei0124
  • Level 1:

# Score on the 6th floor: 0Reply: 15:34:09

My name is vs2005. Do you mean to give a parameter with the default extension when defining the cfiledialog object, instead of null? I also thought that if there is only one extension, as you said, setting a default extension can automatically add an extension and overwrite it. However, I have multiple extension filtering rules. The default extension parameter is null, how does MFC automatically overwrite the corresponding extension based on the selected Filtering Rule?

  • Oyster2008
  • (Oyster2008)
  • Level 1:

#7-floor score: 0Reply: 15:39:00

Let's take a look at the fact that the default extension is set just now.

  • Shiwei0124
  • Level 1:

#8 floor score: 0Reply: 15:56:51

I also tested it and found that as long as lpszdefext is not null, it can satisfy my needs, but I am surprised that it does not seem to match the words on msdn ..
Lpszdefext: default file extension. If you do not include an extension in the file name editing box, the extension defined by lpszdefext is automatically added to the file name. If it is null, no extension is added.
My test result is that as long as the value of lpszdefext is not null, It can automatically overwrite the extension based on the extension of the filter rule.

  • Oyster2008
  • (Oyster2008)
  • Level 1:

#9 Floor score: 0Reply: 16:04:28

Reference the reply from shiwei0124 on the eighth floor:
I also tested it and found that as long as lpszdefext is not null, it can satisfy my needs, but I am surprised that it does not seem to match the words on msdn ..
Lpszdefext: default file extension. If you do not include an extension in the file name editing box, the extension defined by lpszdefext is automatically added to the file name. If it is null, no extension is added.
My test result is that as long as the value of lpszdefext is not null, It can automatically overwrite the extension based on the extension of the filter rule.

Well, it does.

  • Oyster2008
  • (Oyster2008)
  • Level 1:

#10 floor score: 20Reply: 16:07:58

It is also possible to override the ontypechange function of cfiledialog.
Void cfiledialogex: ontypechange ()
{
Int n = m_ofn.nfilterindex;
If (n = 1)
{
Setdefext ("TXT ");
}
Else if (n = 2)
{
Setdefext ("xls ");
}
Cfiledialog: ontypechange ();
}

  • Shiwei0124
  • Level 1:

#11 floor score: 0Reply: 16:09:45

I read the description of lpszdefext and thought that the added extension is the value set in lpszdefext, but it has nothing to do with the selected filtering rule. I didn't expect the result to be like this, it seems that practice is the only criterion for testing truth ..
Thanks to oyster2008. After discussing with you, you are also willing to test the code. Otherwise, you should guess based on what you see ..

  • Oyster2008
  • (Oyster2008)
  • Level 1:

# Floor 12 score: 0Reply: 16:18:10

Reference the reply from shiwei0124 on the 11 th floor:
I read the description of lpszdefext and thought that the added extension is the value set in lpszdefext, but it has nothing to do with the selected filtering rule. I didn't expect the result to be like this, it seems that practice is the only criterion for testing truth ..
Thanks to oyster2008. After discussing with you, you are also willing to test the code. Otherwise, you should guess based on what you see ..

Well, I feel the same way.

  • Shiwei0124
  • Level 1:

#13 floor score: 0Reply: 16:19:59

Solve this problem, paste it, and give it to you.

-------------------------- Invincible split line --------------------------

 

The problem that has been plagued for a long time has been solved smoothly. In addition, please reply from this

It is also possible to override the ontypechange function of cfiledialog.
Void cfiledialogex: ontypechange ()
{
Int n = m_ofn.nfilterindex;
If (n = 1)
{
Setdefext ("TXT ");
}
Else if (n = 2)
{
Setdefext ("xls ");
}
Cfiledialog: ontypechange ();
}

After in-depth analysis, we found that the source code has such a description,

// Overridable callbacks
Protected:
Friend uint_ptr callback _ afxcommdlgproc (hwnd, uint, wparam, lparam );
Virtual uint on1_violation (lpctstr lpszpathname );
Virtual bool onfilenameok ();
Virtual void onlbselchangednotify (uint nidbox, uint icursel, uint ncode );

// Only called back If ofn_explorer is set
Virtual void oninitdone ();
Virtual void onfilenamechange ();
Virtual void onfolderchange ();
Virtual void ontypechange ();

I have heard of the callback function (callbacks) before, but I have never understood any function. I just learned this time. After the original callback function inherited the class, it provided great convenience to set some functions. Although I understand how to use it, I still don't know much about the running mechanism of the callback function. If you have time to study it, design your own callback function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.