Solved

Adding A File Open Dialog To A Program

Posted on 1998-02-07
3
200 Views
Last Modified: 2010-04-10
I have a dialog based application that I have created.  As of right now, the user has to manually input the file name and path of where the file is located.  The program then loads the file and allows the user to edit values and then saves the file back.

I want to add the functionality of a File Open dialog like every windows application uses.  I created a menu and attached it to the dialog box and now I have no idea how to call the built in file open dialog that is in the windows/MFC API.

In visual C++ I read that there is a ID_FILE_OPEN derived from the CWinApp class and the ID_FILE_OPEN command has to be placed in a CWinApp derived classes message map.  My program has three classes CLORDSEDApp derived from CWinApp, and CLORDSDLG derived from CDialog, and an AboutBox Class.

Now I added the the command handler to the message map of CLORDSEDApp which is derived from CWinApp, however when the program compiles and runs I get an ASSERT error and the program halts.  I gave up on this so I tried to utilize the CFileDialog class to create the file open dialog, and this produced the same assert error.

My question is what is the best way for me to implement this and how does the implementation take place.  What I am looking for is a way for the user to select the file he/she wants and have the dialog open the file (similar to createfile because thats what I am using now) or return the name and path to the program for me to write the opening and closing code.  
0
Comment
Question by:larockd
3 Comments
 
LVL 7

Accepted Solution

by:
galkin earned 20 total points
ID: 1181108
First I don't undestand where you load the file. MFC standard document/view behaviour assumes creating new document when you open any file. But dialog based application has no document. so you can either read file display its context to the user in a control( for instance in multiline edit box) allow user to edit it and then save to the file back. You cannot use standard MFc implementation. Instead add menu item to dialog menu with your custom ID. Add message handler(If you add it to system menu you must handle it in WM_SYSCOMMAND handler) and from this handler construct CFileDialog the callits DoModal. On this function returns you have filename use selected.

0
 

Author Comment

by:larockd
ID: 1181109




0
 

Expert Comment

by:pdmoro
ID: 2510157
Try GetOpenFileName()

Something like this

OPENFILENAME OpenFileName;
char *lpFilter = "Bat Files (*.bat)\0*.bat\0Link Files (*.lnk)\0*.lnk\0All Files (*.*)\0*.*\0\0";
      OpenFileName.lStructSize            = sizeof(OPENFILENAME);
      OpenFileName.hwndOwner                  = m_hWnd;
      OpenFileName.hInstance                  = AfxGetApp()->m_hInstance;
      OpenFileName.lpstrFilter            = lpFilter;
      OpenFileName.lpstrCustomFilter      = NULL;
    OpenFileName.nMaxCustFilter            = 0;
    OpenFileName.nFilterIndex            = 0;
    OpenFileName.lpstrFile                  = m_szFileName;
    OpenFileName.nMaxFile                  = MAX_PATH;
    OpenFileName.lpstrFileTitle            = NULL;
    OpenFileName.nMaxFileTitle            = 0;
    OpenFileName.lpstrInitialDir      = m_szFullFilePath;
    OpenFileName.lpstrTitle                  = NULL;
    OpenFileName.nFileOffset            = 0;
    OpenFileName.nFileExtension            = 0;
    OpenFileName.lpstrDefExt            = NULL;
    OpenFileName.lCustData                  = NULL;
      OpenFileName.lpfnHook                  = NULL;
      OpenFileName.lpTemplateName            = NULL;
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question