0

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,

5
Contributors
4
Replies
9
Views
8 Years
Discussion Span
Last Post by Quatrus
0

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.

0

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

Votes + Comments
Don't bump old threads
0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.