Solved

Adding A File Open Dialog To A Program

Posted on 1998-02-07
3
195 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

747 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

12 Experts available now in Live!

Get 1:1 Help Now