The following two functions bring up the file open and file save dialog boxes-
BOOL GetOpenFileName(LPOPENFILENAME lpofn); BOOL GetSaveFileName(LPOPENFILENAME lpofn);
Both of these functions require a pointer to an OPENFILENAME structure. This structure controls such things as the required file extension, the starting path to look in and file access permissions. When the function returns, the structure will contain the name of the file selected.
#include <windows.h> #include <commdlg.h> #define ID_OPEN 1 #define ID_SAVE 2 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); void OpenDialog(HWND); void SaveDialog(HWND); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASS wc; MSG msg; ZeroMemory(&wc,sizeof(wc)); wc.lpszClassName = TEXT("FileDialogDemo"); wc.hInstance = hInstance; wc.lpfnWndProc = WndProc; wc.hCursor = LoadCursor(NULL,IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); RegisterClass(&wc); CreateWindow( TEXT("FileDialogDemo"), TEXT("Open / Save Dialog Demo"), WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 300, 180, NULL, NULL, hInstance, NULL); while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_CREATE: CreateWindow( TEXT("BUTTON"), TEXT("Open File"), WS_CHILD | WS_VISIBLE, 30, 30, 100, 30, hwnd, (HMENU)ID_OPEN, NULL, NULL); CreateWindow( TEXT("BUTTON"), TEXT("Save File"), WS_CHILD | WS_VISIBLE, 150, 30, 100, 30, hwnd, (HMENU)ID_SAVE, NULL, NULL); return 0; case WM_COMMAND: switch(LOWORD(wParam)) { case ID_OPEN: OpenDialog(hwnd); break; case ID_SAVE: SaveDialog(hwnd); break; } return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd,msg,wParam,lParam); } void OpenDialog(HWND hwnd) { OPENFILENAME ofn; TCHAR fileName[MAX_PATH] = TEXT(""); ZeroMemory(&ofn,sizeof(ofn)); ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = hwnd; ofn.lpstrFile = fileName; ofn.nMaxFile = MAX_PATH; ofn.lpstrFilter = TEXT("Text Files (*.txt)\0*.txt\0") TEXT("All Files (*.*)\0*.*\0"); ofn.nFilterIndex = 1; ofn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST; if(GetOpenFileName(&ofn)) { MessageBox( hwnd, fileName, TEXT("Selected File"), MB_OK); } } void SaveDialog(HWND hwnd) { OPENFILENAME ofn; TCHAR fileName[MAX_PATH] = TEXT(""); ZeroMemory(&ofn,sizeof(ofn)); ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = hwnd; ofn.lpstrFile = fileName; ofn.nMaxFile = MAX_PATH; ofn.lpstrFilter = TEXT("Text Files (*.txt)\0*.txt\0") TEXT("All Files (*.*)\0*.*\0"); ofn.lpstrDefExt = TEXT("txt"); ofn.Flags = OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST; if(GetSaveFileName(&ofn)) { HANDLE hFile; hFile = CreateFile( fileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if(hFile != INVALID_HANDLE_VALUE) { TCHAR text[] = TEXT("Common Dialog Demo"); DWORD bytesWritten; WriteFile( hFile, text, lstrlen(text) * sizeof(TCHAR), &bytesWritten, NULL); CloseHandle(hFile); MessageBox( hwnd, TEXT("File saved successfully."), TEXT("Save"), MB_OK); } } }