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!
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:
#define DEFAULT_EXTENSION L".obj"
OPENFILENAME ofn;
wchar_t *FilterSpec =L"Object Files(*.obj)\0*.obj\0Text Files(*.txt)\0*.txt\0All Files(*.*)\0*.*\0";
wchar_t *Title =L"Open....";
wchar_t szFileName[MAX_PATH];
wchar_t szFileTitle[MAX_PATH];
int Result;
wchar_t filePath[MAX_PATH]; // Selected file and path
*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 = L"."; // 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; // Failed or cancelled
}
else
{
wcscpy_s(filePath,ofn.lpstrFile);
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With