Which class should handle the request to open a file

Hi!
I'm writing a simple game (sdi, doc-view) and new games are started by 
opening files from a menu item (more on what the actual game is about 
below). However, I find it difficult to figure out what I should handle 
where. I was thinking of handling open file request (ON_COMMAND()) in 
the main frame and if the user selects a valid file then notify the 
document class about it (let the document class do the actual reading of 
the file).

The files contain a text-represation of a maze and the user is supposed 
to control a character that must find its way out of the maze. The 
faster and the fewer clicks to do so the higher the score.

I'm using MSVC++ 2008.

- Eric
0
invalid (293)
7/11/2008 11:14:45 PM
vc.mfc 33608 articles. 0 followers. Follow

9 Replies
504 Views

Similar Articles

[PageSpeed] 30

On Sat, 12 Jul 2008 01:14:45 +0200, WP <invalid@invalid.invalid> wrote:

>Hi!
>I'm writing a simple game (sdi, doc-view) and new games are started by 
>opening files from a menu item (more on what the actual game is about 
>below). However, I find it difficult to figure out what I should handle 
>where. I was thinking of handling open file request (ON_COMMAND()) in 
>the main frame and if the user selects a valid file then notify the 
>document class about it (let the document class do the actual reading of 
>the file).
>
>The files contain a text-represation of a maze and the user is supposed 
>to control a character that must find its way out of the maze. The 
>faster and the fewer clicks to do so the higher the score.
>
>I'm using MSVC++ 2008.

This is handled by the document/view architecture in
CDocument::OnOpenDocument. You shouldn't need to do anything but implement
that function. If you are doing something outside the framework, you can
handle the command message anywhere in the routing chain, which IIRC, is
view->frame->document->application by default. But yeah, the document class
is definitely the place to read the contents of a file into the document.
The UI activity (file selector dialog) should be handled by the view or
frame, probably the frame.

-- 
Doug Harrison
Visual C++ MVP
0
dsh (2498)
7/11/2008 11:29:30 PM
Doug Harrison [MVP] wrote:
> On Sat, 12 Jul 2008 01:14:45 +0200, WP <invalid@invalid.invalid> wrote:
> 
>> Hi!
>> I'm writing a simple game (sdi, doc-view) and new games are started by 
>> opening files from a menu item (more on what the actual game is about 
>> below). However, I find it difficult to figure out what I should handle 
>> where. I was thinking of handling open file request (ON_COMMAND()) in 
>> the main frame and if the user selects a valid file then notify the 
>> document class about it (let the document class do the actual reading of 
>> the file).
>>
>> The files contain a text-represation of a maze and the user is supposed 
>> to control a character that must find its way out of the maze. The 
>> faster and the fewer clicks to do so the higher the score.
>>
>> I'm using MSVC++ 2008.
> 
> This is handled by the document/view architecture in
> CDocument::OnOpenDocument. You shouldn't need to do anything but implement
> that function. If you are doing something outside the framework, you can
> handle the command message anywhere in the routing chain, which IIRC, is
> view->frame->document->application by default. But yeah, the document class
> is definitely the place to read the contents of a file into the document.
> The UI activity (file selector dialog) should be handled by the view or
> frame, probably the frame.
> 

Right, so I will keep the code for displaying the choose file dialog in 
the frame class and if the user selects a new maze file to be opened I 
will call OnOpenDocument in the document class, thanks! Very quick reply 
I must say and a quality one at that!

- Eric
0
invalid (293)
7/11/2008 11:49:09 PM
WP wrote:
> Right, so I will keep the code for displaying the choose file dialog in 
> the frame class and if the user selects a new maze file to be opened I 
> will call OnOpenDocument in the document class, thanks! Very quick reply 
> I must say and a quality one at that!

WP:

No.

If you have an MFC SDI application, then when you select Open from the file 
menu, the Open file dialog will be shown automatically, and the virtual method 
OnOpenDocument() will be called by the framework. All you need to do is override 
this method.

-- 
David Wilkinson
Visual C++ MVP
0
no-reply8010 (1791)
7/12/2008 2:02:42 AM
Note that for SDI, you should override CDocument::DeleteContents to reset the contents of
the document.
				joe
On Fri, 11 Jul 2008 22:02:42 -0400, David Wilkinson <no-reply@effisols.com> wrote:

>WP wrote:
>> Right, so I will keep the code for displaying the choose file dialog in 
>> the frame class and if the user selects a new maze file to be opened I 
>> will call OnOpenDocument in the document class, thanks! Very quick reply 
>> I must say and a quality one at that!
>
>WP:
>
>No.
>
>If you have an MFC SDI application, then when you select Open from the file 
>menu, the Open file dialog will be shown automatically, and the virtual method 
>OnOpenDocument() will be called by the framework. All you need to do is override 
>this method.
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15975)
7/12/2008 2:25:08 AM
David Wilkinson wrote:
> WP wrote:
>> Right, so I will keep the code for displaying the choose file dialog 
>> in the frame class and if the user selects a new maze file to be 
>> opened I will call OnOpenDocument in the document class, thanks! Very 
>> quick reply I must say and a quality one at that!
> 
> WP:
> 
> No.
> 
> If you have an MFC SDI application, then when you select Open from the 
> file menu, the Open file dialog will be shown automatically, and the 
> virtual method OnOpenDocument() will be called by the framework. All you 
> need to do is override this method.
> 

OK, I misunderstood you. I noticed that I get an open file dialog 
automatically, but I wanted to make some changes to that dialog. How 
should I go about it then?
0
invalid (293)
7/12/2008 10:02:23 AM
Joseph M. Newcomer wrote:
> Note that for SDI, you should override CDocument::DeleteContents to reset the contents of
> the document.

Thanks, I will make sure to do that. I have quite a bit of dynamically 
allocated memory that needs releasing.

> 				joe
> On Fri, 11 Jul 2008 22:02:42 -0400, David Wilkinson <no-reply@effisols.com> wrote:
> 
>> WP wrote:
>>> Right, so I will keep the code for displaying the choose file dialog in 
>>> the frame class and if the user selects a new maze file to be opened I 
>>> will call OnOpenDocument in the document class, thanks! Very quick reply 
>>> I must say and a quality one at that!
>> WP:
>>
>> No.
>>
>> If you have an MFC SDI application, then when you select Open from the file 
>> menu, the Open file dialog will be shown automatically, and the virtual method 
>> OnOpenDocument() will be called by the framework. All you need to do is override 
>> this method.
> Joseph M. Newcomer [MVP]
> email: newcomer@flounder.com
> Web: http://www.flounder.com
> MVP Tips: http://www.flounder.com/mvp_tips.htm
0
invalid (293)
7/12/2008 10:22:45 AM
WP wrote:
> OK, I misunderstood you. I noticed that I get an open file dialog 
> automatically, but I wanted to make some changes to that dialog. How 
> should I go about it then?

WP:

You mean you want to customize it? That is actually a more difficult thing to 
do, especially if you want to hook your customized file dialog into the MFC 
framework.

Are you sure you want to tackle this right now? What is it that you need to 
customize anyway. If you are just talking about what kinds of files (file 
extensions) to ask for then you can do that quite easily.

I would really recommend to get a good book on MFC and work through it, so that 
you know better what it can do for you out of the box.

-- 
David Wilkinson
Visual C++ MVP
0
no-reply8010 (1791)
7/12/2008 11:23:58 AM
David Wilkinson wrote:
> WP wrote:
>> OK, I misunderstood you. I noticed that I get an open file dialog 
>> automatically, but I wanted to make some changes to that dialog. How 
>> should I go about it then?
> 
> WP:
> 
> You mean you want to customize it? That is actually a more difficult 
> thing to do, especially if you want to hook your customized file dialog 
> into the MFC framework.
> 
> Are you sure you want to tackle this right now? What is it that you need 
> to customize anyway. If you are just talking about what kinds of files 
> (file extensions) to ask for then you can do that quite easily.

Hi David and thanks for replying! I just wanted it to use a particular 
filter and always show a particular initial directory + setting 
OFN_FILEMUSTEXIST, nothing fancy. What I did was simply put a command 
handler in the view which handles the command ID_FILE_OPEN. The handler 
constructs a CFileDialog object with the desired properties and displays 
it. If the user decides to open a file I call the document 
OnOpenDocument() with the path to the file. I moved this code from the 
frame to the view because I realised that the frame cannot get the 
document object as easily as the view can.
I think I'm going to leave this part of the code for now and ask about 
something else (also to do with structure, sorry), but I will start a 
new thread for that.

> 
> I would really recommend to get a good book on MFC and work through it, 
> so that you know better what it can do for you out of the box.
> 

- Eric (WP)
0
invalid (293)
7/12/2008 12:57:12 PM
"WP" <invalid@invalid.invalid> wrote in message 
news:6dro1dF40jl9U1@mid.individual.net...
> Hi David and thanks for replying! I just wanted it to use a particular 
> filter and always show a particular initial directory + setting 
> OFN_FILEMUSTEXIST, nothing fancy. What I did was simply put a command 
> handler in the view which handles the command ID_FILE_OPEN. The handler 
> constructs a CFileDialog object with the desired properties and displays 
> it. If the user decides to open a file I call the document 
> OnOpenDocument() with the path to the file. I moved this code from the 
> frame to the view because I realised that the frame cannot get the 
> document object as easily as the view can.
> I think I'm going to leave this part of the code for now and ask about 
> something else (also to do with structure, sorry), but I will start a new 
> thread for that.
>

To change the filter, you can edit the IDR_MAINFRAME string in the .rc file. 
For example, mine is:

    IDR_MAINFRAME           "DCSoft CDRCue 2004\nUntitled 
cuesheet\nCueSheet\nCue sheets (*.cue)\n.cue\nCDRCue.Document\nCDRCue 
Document"


You can see the part that shows that the file open/save dialog needs to save 
Cue Sheets (*.cue) files.

I don't think you have control over the initial directory or 
OFN_FILEMUSTEXIST.  If you need those, I don't think there is any easier way 
than what you are doing now.



>> I would really recommend to get a good book on MFC and work through it, 
>> so that you know better what it can do for you out of the box.
>>

The nice thing about MFC is that the source code is included.  Simply set a 
breakpoint in CMyDocument::OnOpenDocument().  Trace into the call to 
CDocument::OnOpenDocument().  The debugger may prompt you to specify the 
path to <path where you installed Visual Studio>\<path of MFC source code>, 
but after you do that, you can trace into the MFC code as easily as if it 
were your own source code.  You can see how it's showing the file open 
dialog and perhaps if it allows you to customize it.

Have you checked out Ivor Horton's Visual C++ 2008 book?  It is a very good 
intro to both MFC and C++/CLI and gives plenty of VC2008 examples to work 
through.  I think it is the best (and one of the few) modern MFC books that 
work with VC2008.

-- David 


0
dc2983 (3206)
7/13/2008 1:14:16 PM
Reply:

Similar Artilces:

Primission / resticted access to files using the MS server
I wish I have NEVER deciced to use this facility!!!@ I should have kept to paper and pin.. This word file is totally useless to me now. I have been using the tool facilty sucessfully for years but now it won't let me get to my own files.., I think you can tell what I think about this product. Hello Ron Ron wrote: > I wish I have NEVER deciced to use this facility!!!@ I should have kept to > paper and pin.. This word file is totally useless to me now. I have been > using the tool facilty sucessfully for years but now it won't let me get to > my own fi...

Outlook98 unable to open any attachments
I knew there was a quick fix at one time... removing a registry key? I have already removed the outcmd.dat and used /safe to start Outlook in safe mode.... trying to support one of my users. Thanks! Christina What is your email environment; Exchange, POP3, etc.? Are there error messages when you try to open an attachment? Can you open the attachment if you save it to the desktop instead? Just trying to get some sense of what's going on. -- Kathleen Orland Outlook Tips: http://www.outlook-tips.net/ http://www.howto-outlook.com/ Outlook & Exchange Solutions Center: http://www....

Inherited process handle + process exit code
Hi I'm having one parent process which launches another process. This child process inherits process handle from its parent. Now I want to know the exit status of these two processes. Can I use GetExitCodeProcess() API here? or do I need to do something else, since now the hadles of both the processes is same and this API accepts handle only. Thanks Nishant Usually you use GetExitCodeProcess() on the process handle only after you know the process has stopped; it is poor style to poll using it. I don't know what you mean by "the handles of both the processes is the same". ...

File is not in a recognizable format.
Everytime I open Excel 2000, I get "This file is not in a recognizable format". I wonder if I have a corrupt default file. I tried to replace book.xlt with a newly saved book.xlt, it didn't help. I'd appreciate any help. Thanks, Liz Sounds like you have a file in your XLSTART directory that doesn't belong there. Use Windows' Find to locate the directory. If the rogue file isn't obvious, move all files out of that directory, then move them back one at a time, restarting Excel and quitting each time, to see which one is causing the problem. On Wed, 22 Sep 2...

Single Click Files
Realized that I can no longer single click to open files. Found at Folder Options the single click radio button is grayed-out now. Can this be changed? ...

can I forward mail without opening/viewing?
I know that sounds like a weird question, but my ISP invites users to forward spam to a special mailboxes in their never-ending (heh) effort to reduce the flow of the junk. With Outlook, I cannot figure out how to forward a message without its opening up for viewing. This is exactly what I don't want to do, because if it's html I really don't want to validate my email address. TIA Jason -- reverse my name in email address Jason, What you would have to do is create "rules" to do this, which you can set Outlook to if it receives a certain criteria, it can say FWD ...

Excel opens two worksheets instead of one
I have Office 2007 and recently upgraded to Windows 7. When I open an existing document in Excel, sometimes (about 50% of the time) it opens a second blank worksheet along with it. Word does this also. Is there a way to fix this? -- Thank you ...

Outlook data file check
Hello, I am using Outlook 2007 on XPSP2. Quite often on startup I get a message the Outlook is doing a file check and performance may be slow. I also often get on computer startup a message requesting me to send an error report to Microsoft that includes a few episodes of Outlook not responding. Any idea what this could be about and how to prevent both? -- Thanks, Bob ...

Slow loading of money files
Upgraded from deluxe 2007 to deluxe 2008 (Money Plus Deluxe). Same file now takes 10-15 minutes to load, and that is if it loads at all. Maxes out both CPUs and freezes up sometimes. Have done both quick and standard repair with "no errors". File size increased by 20 MB after repair. Running on Vista Ultimate x32. Right now I can't use the upgrade in the present state. Fist time in eleven years I've had a "real difficult" problem using Money. Any suggestions? What changed behind the scenes to cause this? In microsoft.public.money, wolschon1 wrote: >...

Refresh of links in shared file.
I have created a shared file that has a production schedule. It has links to another non-shared file so that I can use/refresh a query to pull production information from a database and put it into pivot tables. I have a vlookup to find the totals of the part in the pivot table. When I have the schedule file shared and open and I have the "update file" open and then update the query everything in my shared document updates. I save changes. The problem is others cannot see these changes by saving (it works if I type numbers instead of using a link to the update file...

How to Delete fb_540.lck or Any LCK files?
WinXP user here. Trying to clear my temp files however cannot delete files with .lck extension. Says being used by another person or program. Seems these files are accumulating and can't be deleted. TIA shaka For good reason they are locked files! They are usually critical databases that programs will LOCK when in use so they don't get corrupt There are Many Programs that use this Extension http://www.file-extensions.org/lck-file-extension Depending on where you find them would be a good clue on what program is using them. If they are "GROWING" in a d...

Difference between an excel worksheet and XLXS file
I'm a beginner in the world of computer and ms office. On my computer I using Office2007. In excel Icreate a file and saved it as .XLXS file. For the correctons of this file I send to my friend,by using a flash disk. My friend is using Office2003. My file is not oppen on my friend's computer.Why? "Martin" <Martin@discussions.microsoft.com> wrote in message news:8CE55079-436D-411F-BA7B-9C122AB3CFB2@microsoft.com... > I'm a beginner in the world of computer and ms office. On my computer I > using Office2007. In excel Icreate a file and saved it as .XLXS f...

Inserting pictures in a CHM file
With the HTML Help Workshop from MS, I'm trying to add pictures to some of my pages with mixed results. Small pictures seem to work ok but larger ones (>500 kb) don't. After compiling the help file, the frame is present with no picture. Can anyone help? Best regards, Fred Fred, > With the HTML Help Workshop from MS, I'm trying to add pictures to some of > my pages with mixed results. Small pictures seem to work ok but larger ones > (>500 kb) don't. After compiling the help file, the frame is present with > no picture. What tool are you using to ...

The presentation cannot be opened. Outlook 2010 Beta
I am using Outlook 2010 Beta, and every time I receive a PPT or PPS attachment and try to open it, the following message apears: The presentation cannot be opened. Your antivirus program may prevent you from opening the presentation. To fix this problem, make sure your antivirus program is current and working correctly. If the problem persists and the presentation is from someone that you trust, turn off your antivirus program, and then try to open the presentation again. If you do this, make sure you turn on your antivirus program again after you open the presentation. I hav...

calls to extract data from an open database
I have a workbook that on the “open” event connects to an Oracle database using ADO, this works fine. I need to code various calls to the data base to run several queries. How do I achieve this? Do I have to repeat the ADO connection string to the database complete with password etc; I am sure this is not necessary. Any code will be very gratefully received. -- with kind regards Spike The usual way to handle this is have a Public or Private ADO connection object so you can set that up once and keep it alive till you don't need it anymore. So for example: Opti...

How is SMTP handled in a NLB setup ?
We have two FrontEnd Exchange 2003 servers setup in a Network Load Balanced "cluster" - our BackEnd server uses the NLB cluster as smart host for outgoing SMTP.. Can we be sure that all outgoing mail is sent as coming from the common NLB cluster-IP-number or could it be possible that under certain conditions mail is sent as coming from the IP-number of the indivudal server handling the transaction ? Our worry is whether we need to register both the NLB cluster-IP and the individual server-IP's in external DNS - to enable sender verification in all circumstances ? Thank...

transfer OE files from Win2k to XP
I'm going to help my dad install XP on his existing machine. It's going to be a clean install so we're overwriting his 2000 installation completely. He's already backed up everything from the machine that he wants to keep. But it occurred to me that OE could be a problem. He's still using OE for email despite my efforts to get him away from it. I know how to transfer the files from one installation to another (email files and address book). But I recall doing something like this some years ago and ran into a problem. I just don't remember if it's the s...

Download file from another server
Hi , I am working on C# in Asp.net 2.0 . I have to downlaod file from another server . If i wirte Server.mappath("") It gives me local path. I want to access that server from my application server to get that file. please help me. Thanks, Hemant "Hemant" <Hemant@nomail.com> wrote in message news:#dfMx#0yKHA.2436@TK2MSFTNGP04.phx.gbl... > I am working on C# in ASP.NET 2.0. > I have to downlaod file from another server. OK. > If I write Server.mappath("") It gives me local path. Yes, it would. > I want to a...

Opening Calendar 'hangs' Outlook, CPU 100%
Hi, has anyone experienced this? A week ago one of our users complained that his calender made his Outlook 'hang' trying to add an appointment on a date past December 9 2003. System: Exchange 55-sp4 on NT4-sp6a, Outlook 2000 on client Win2000. I noticed that all other functions in Outlook worked without any problems, but trying to open or even look at any date past December 9 in his calendar would freeze the PC (CPU 100%). Logging into another workstation produced exactly the same problem. Opening the calendar in my own Outlook (admin) produced the same problem. Following several Tech...

Can I link *.jpg files to Excel cells?
I would like to connect pictures to cells on a spreadsheet. Ideally, I'd like to connect more than one to a cell. Or to its row. That would work OK also. Can I do it? TIA LAS You can hyperlink a cell to an image stored elsewhere or to an image stored within the workbook. How you do it depends upon the purpose of the linking and the ultimate results you are looking for. Gord Dibben MS Excel MVP On Fri, 25 Jun 2010 17:26:13 -0400, "LAS" <FakeMail@Hotmail.com> wrote: >I would like to connect pictures to cells on a spreadsheet. Ideally, I'd >lik...

Can a workbook be opened w/out Excel on the host machine??
Hi all, I need to distribute my workbook to several sponsors in different locations (cities and states). Some have Excel on their workstations, some don't. Is there a way to package the file to include some sort of self-executable Excel reader -like PowerPoint does-? Does an Excel Reader even exist? The end users will have need to select vba userforms and command buttons to navigate through the workbook, so there does need to be some interactivity capability and not just "read only". Thanks! http://office.microsoft.com/en-gb/officeupdate/CD011347961033.aspx is a free vie...

Is there a way to store a file in resources or therewise in MFC app?
Hello, I'm trying to be able to create a given file to a given directory without copying it from some other directory but rather from a storage location inside the .exe. Is there a way to do that?? Thanks, Ivan "Ivan Starr" <ivan@ivanstarr.com> wrote in message news:ejccNsnZDHA.2620@TK2MSFTNGP09.phx.gbl... > Hello, > > I'm trying to be able to create a given file to a given directory without > copying it from some other directory but rather from a storage location > inside the .exe. Is there a way to do that?? FindResource, LoadResource, SizeofRe...

Animated GIFs and file types
Sorry about this, I'm a neophyte when it comes to Publisher, but I am the support person. I have an upper leverl mgr who is heck bent on using Publisher to create a company newsletter. I know he can save it has an html, but I'm unable to get it to (& he demands that) his GIFs be animated. Any ideas? Thanks! Ooops, version is Pub 2002. >-----Original Message----- >Sorry about this, I'm a neophyte when it comes to >Publisher, but I am the support person. >I have an upper leverl mgr who is heck bent on using >Publisher to create a company newsletter. I kno...

archiving ... at what file size does this become necessary?
My Personal Folder(1).pst is less than 120 megs. At what size, with a modern computer, does the size of that file make it necessary to archive it? Dave Horne <davehorne@home.nl> wrote: > My Personal Folder(1).pst is less than 120 megs. At what size, with a > modern computer, does the size of that file make it necessary to > archive it? With Outlook 2003 or 2007, you can store up to 20 GB in a PST out of the box. In actuality, you never need to use autoarchive, if you choose not to, although you may have to move things around between multiple PSTs so that any one PST ...

Problem with Outlook 2003 pst file
I am attempting to help a computer club member. I received a call yesterday that he couldn't open Office Outlook 2003 on a Windows XP SP 2 system. I dropped by and found that when one attempted to open Outlook, a message would appear saying that there was a conflict between Outlook and WinFav Pro the system would open the Norton Web site. He has recently installed Norton 360. The Windows XP system does not have WinFax Pro installed. The a message would be received that stated that the pst file hadn't been closed properly and it would look for errors and find some stating that I...