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,

Recommended Answers

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 …
Jump to Post

All 4 Replies

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:(

commented: Don't bump old threads -1

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

Thank you for the information. It was useful.
I found another very useful link:
http://www.winprog.org/tutorial/
There is a very nice tutorial and set of examples!

Sergey

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.