Solved

Adding A File Open Dialog To A Program

Posted on 1998-02-07
3
197 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now