Hi all,

I'd like to open a file dialog using win32 project ...I need a simple code example that uses FileOpen...I used <commdlg.h> ...::GetOpenFileName or ::GetSaveFileName but i can't get them to work :S ...

Thanks in Advance,

Here is an example of how to use the GetOpenFileName function. Any decent Win32 tutorial on the net would should you how to do this, so in future, search the web.

#include <windows.h>
#include <string.h>
#include <iostream>
using namespace std; 

// Returns an empty string if dialog is canceled
string openfilename(char *filter = "All Files (*.*)\0*.*\0", HWND owner = NULL) {
  OPENFILENAME ofn;
  char fileName[MAX_PATH] = "";
  ZeroMemory(&ofn, sizeof(ofn));

  ofn.lStructSize = sizeof(OPENFILENAME);
  ofn.hwndOwner = owner;
  ofn.lpstrFilter = filter;
  ofn.lpstrFile = fileName;
  ofn.nMaxFile = MAX_PATH;
  ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
  ofn.lpstrDefExt = "";

  string fileNameStr;

  if ( GetOpenFileName(&ofn) )
    fileNameStr = fileName;

  return fileNameStr;
}

int main() {
  cout << openfilename().c_str();
  cin.ignore();
}

Either way, I hope this helps.

Thanks, It was helpful.
The guy that asked did not bother to do that:(

Comments
Don't bump old threads

If you want !!!SAVE FILE DIALOG!!!, SIMPLY USE GetSaveFileNameA(&ofn); WHERE GetOpenFileName(&ofn) IS...

This article has been dead for over six months. Start a new discussion instead.