views:

842

answers:

3

I know this is a darn simple question, but I'm very used to using Borland and wrappers, so this is a bit of a new approach for me. Can someone simply tell me how I Can open an OpenDialog that only gets .obj files from a visual studio c++ console app?

It's very much appreciated!

A: 

In Visual Studio one usually relies on MFC's CFileDialog class. Take a look at linked MSDN documentation page for sample usage. This page has some examples as well.

If you are using Windows Vista or Windows 7, you can try the new COM interface IFileOpenDialog. Kenny Kerr has a nice article on using the new Vista dialogs.

Filip
not everyone wants to have MFC in their app - especially a console app!
Tim
Makes sense.Use the GetOpenFileName() api call instead.http://msdn.microsoft.com/en-us/library/ms646829(VS.85).aspx
Filip
+3  A: 

There isn't really any difference between a console application and a GUI application, except for entry point (WinMain in a 'GUI' app), and a console app will have a console window opened during startup if not started from a console.

All of the Win32 API is available, so you need to use the GetOpenFileName call, as follows:

OPENFILENAME ofn; char FilterSpec ="Object Files(.obj)\0*.obj\0Text Files(.txt)\0.txt\0All Files(.)\0*.*\0"; char *Title ="Open...."; char szFileName[MAX_PATH]; char szFileTitle[MAX_PATH]; int Result;

*szFileName = 0; *szFileTitle = 0;

/* fill in non-variant fields of OPENFILENAME struct. */
ofn.lStructSize       = sizeof(OPENFILENAME);
ofn.hwndOwner         = GetFocus();
ofn.lpstrFilter       = FilterSpec;
ofn.lpstrCustomFilter = NULL;
ofn.nMaxCustFilter    = 0;
ofn.nFilterIndex      = 0;
ofn.lpstrFile         = szFileName;
ofn.nMaxFile          = MAX_PATH;
ofn.lpstrInitialDir   = "."; // Initial directory.
ofn.lpstrFileTitle    = szFileTitle;
ofn.nMaxFileTitle     = MAX_PATH;
ofn.lpstrTitle        = Title;
ofn.lpstrDefExt   = default_extension;

ofn.Flags             = OFN_FILEMUSTEXIST|OFN_HIDEREADONLY;

if (!GetOpenFileName ((LPOPENFILENAME)&ofn))
{
    return (-1); // Failed or cancelled
}
else
{
    this->filename.Set(szFileName);
}
Richard Harrison
after some messing around with the code, that did 'er! Thanks a lot!
Cyprus106
A: 

Yes it is possible to open an OpenDialog from VC++ console app.

Steps: Create a new project. -> select Win32 Console Application. In the next dialog, select "An Application that supports MFC". you will be provided with the following code:

#include "stdafx.h"
#include "test.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

///////////////////////////////////////////////////////////////////////////// // The one and only application object

CWinApp theApp;

using namespace std;

int tmain(int argc, TCHAR argv[], TCHAR* envp[]) { int nRetCode = 0;

// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
 // TODO: change error code to suit your needs
 cerr << _T("Fatal Error: MFC initialization failed") << endl;
 nRetCode = 1;
}
else
{
 // TODO: code your application's behavior here.
 CString strHello;
 strHello.LoadString(IDS_HELLO);
 cout << (LPCTSTR)strHello << endl;
}

return nRetCode;

}

Add the following code at the begining of "else" part

CFileDialog dlgOpen(TRUE,NULL,NULL,OFN_OVERWRITEPROMPT,"Text Files (.txt)|.txt||"); dlgOpen.DoModal();

Run the application. A open dialog will be opened automatically. Google "CFileDialog" for further help.

GreenShadow
not everyone wants to have MFC in their app - especially a console app! Also - an existing app might not be so easy to convert to mfc...
Tim