Solved

Adding A File Open Dialog To A Program

Posted on 1998-02-07
3
201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
FMX enumerated colours 2 155
Safe conversion? 4 86
c++, dynamic object by json 1 65
Winapi.Windows.hpp problem 7 44
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 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.

738 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