Archived

This topic is now archived and is closed to further replies.

Guest Anonymous Poster

GetOpenFileName

This topic is 6563 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


Hmm...I don't see how the code worked in the first place? Anyways, I made some adjustments and am reposting the code:


//: changed OFNJaunt references to OFN
//: changed Filename from char * to array of char
//: changed CurDir from char * (LPTSTR) to array of char
//: changed nMaxFile to = sizeof(FileName);
//: Removed lpstrCustomFilter (set to 0L)
//: Set nMaxCustFilter to 0L
//: Set nMaxFileTitle to 0L

OPENFILENAME OFN;
char FileName[255] = { 0 }; //: where
char CurDir[255] = { 0 };


GetCurrentDirectory(100, CurDir);

OFN.lStructSize = sizeof(OPENFILENAME);
OFN.hwndOwner = hwnd;
OFN.hInstance =hInst;
OFN.lpstrFilter = "Object Files (*.cob,*.3ds,*.x)\0*.cob; *.3ds; *.x\0All Files (*.*)\0*.*\0\0";
OFN.lpstrCustomFilter = 0L;
OFN.nMaxCustFilter = 0L;/* not used if lpstrCustomFilter=NULL*/
OFN.nFilterIndex = 0;
OFN.lpstrFile = FileName;
OFN.nMaxFile = sizeof(FileName);
OFN.lpstrFileTitle = NULL;
OFN.nMaxFileTitle = 0; /* ignored if lpstrFileTitle is NULL */
OFN.lpstrInitialDir = CurDir;
OFN.lpstrTitle = "Open The File";
OFN.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
OFN.nFileOffset = NULL;
OFN.nFileExtension = NULL;
OFN.lpstrDefExt = ".cob";
OFN.lCustData = NULL;
OFN.lpfnHook = NULL;
OFN.lpTemplateName = NULL;

if ( GetOpenFileName(&OFN) )
MessageBox(0L, OFN.lpstrFile, "Selected File", MB_OK);
else
MessageBox(0L, "None selected!", "Selected File", MB_OK);


HTH,

-mordell

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hey thanks, I tried the code but I got the same result, the program just closes entirely when I either double click on a file or hit "open" in the dialog box. Also still doesn't return true or false from the function.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I've been trying to get my code that I wrote in Borland to work in MSVC++. here's the code that's giving me problems right now:

OPENFILENAME OFN;
LPTSTR CurDir;
GetCurrentDirectory(100, CurDir);

OFN.lStructSize = sizeof(OFNJaunt);
OFN.hwndOwner = hwnd;
OFN.hInstance =hInst;
OFN.lpstrFilter = NormFilter;
OFN.lpstrCustomFilter = CustFilters;
OFN.nMaxCustFilter = sizeof(CustFilters);
OFN.nFilterIndex = 0;
OFN.lpstrFile = FileName;
OFN.nMaxFile = 500;
OFN.lpstrFileTitle = NULL;
OFN.nMaxFileTitle = 40;
OFN.lpstrInitialDir = CurDir;
OFN.lpstrTitle = "Open The File";
OFNJaunt.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
OFN.nFileOffset = NULL;
OFN.nFileExtension = NULL;
OFN.lpstrDefExt = ".cob";
OFN.lCustData = NULL;
OFN.lpfnHook = NULL;
OFN.lpTemplateName = NULL;

GetOpenFileName(&OFNJaunt);

The call to GetOpenFileName causes the whole program to quit. No message or anything. It doesn't even return True or False. It works in Borland, but not in Microsoft. help?

Share this post


Link to post
Share on other sites
I use:

code:
int TelBytes(const char *FileNaam)
{
// This opens a file and counts te number of bytes in it,
// if zero, we return -1 (error)
FILE *fp = fopen(FileNaam, "rb");
if (fp == NULL)
return -1;
int n=0;
while (getc(fp) != EOF) n++;
fclose(fp);
return n;
}


CString ShowBrowse(const char* const FileType, int OpenSave, HWND m_hWnd, CString Extensie)
{
// This shows the Open File dialog box
if (OpenSave == 1) // Show an Open... box
{
static char szFile[256] = "", szFileTitle[256] = "";
static int iLengte = 0;
OPENFILENAME ofn;
memset(&ofn, 0, sizeof(ofn));
const char* const szFilter = FileType;
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.lpstrFilter = szFilter;
ofn.nFilterIndex = 1;
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFileTitle = szFileTitle;
ofn.nMaxFileTitle = sizeof(szFileTitle);
ofn.lpstrInitialDir = NULL;
ofn.hwndOwner = m_hWnd;
ofn.lpstrTitle = "Open file";
if (GetOpenFileName(&ofn))
{
iLengte = TelBytes(szFile);
if (iLengte == -1)
{
MessageBox(m_hWnd, "Unable to open file!", "ERROR", MB_OK | MB_ICONSTOP);
return "";
}
InvalidateRect(m_hWnd, NULL, TRUE);
return CString(szFile);
}
}
else if (OpenSave == 2) // Show a Save As... box
{
static char szFile[256] = "", szFileTitle[256] = "";
static int iLengte = 0;
OPENFILENAME ofn;
memset(&ofn, 0, sizeof(ofn));
const char* const szFilter = FileType;
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.lpstrFilter = szFilter;
ofn.nFilterIndex = 1;
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFileTitle = szFileTitle;
ofn.nMaxFileTitle = sizeof(szFileTitle);
ofn.lpstrInitialDir = NULL;
ofn.hwndOwner = m_hWnd;
ofn.lpstrTitle = "Save file";
ofn.lpstrInitialDir = NULL;
if (GetSaveFileName(&ofn))
{
CString temp = CString(szFile);
temp.MakeUpper();
if (temp.Right(4) != Extensie )
{
temp += Extensie;

}

return temp;
}
}
return "";
}


most of the time........just copied and pasted it right now........but at least it works. For me.......VC++

------------------
Dance with me......

http://members.xoom.com/CJdeVos/index.htm

Share this post


Link to post
Share on other sites