Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Adding A File Open Dialog To A Program

Posted on 1998-02-07
3
Medium Priority
?
203 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

722 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