API |
MFC |
C++ |
C |
Programming Windows API
File Open Save Common Dialog Box Example
//open save common dialog box
#include <windows.h>
#define ID_BUTTON 1
#define ID_BUTTON1 2
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void LoadFile(LPSTR );
void OpenDialog(HWND );
void SaveDialog(HWND );
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,PWSTR pCmdLine, int nCmdShow)
{
MSG msg ;
WNDCLASS wc = {0};
wc.lpszClassName = TEXT("File open save dialog box");
wc.hInstance = hInstance;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = WndProc;
RegisterClass(&wc);
CreateWindow( wc.lpszClassName, TEXT("File open/save dialog box"),WS_OVERLAPPEDWINDOW | WS_VISIBLE,150, 150, 250, 200, 0, 0, hInstance, 0);
while( GetMessage(&msg, NULL, 0, 0))
{
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch(msg) {
case WM_CREATE:
{
CreateWindow(TEXT("button"), TEXT("File Open"),WS_VISIBLE | WS_CHILD ,20, 30, 80, 25,hwnd, (HMENU) ID_BUTTON, NULL, NULL);
CreateWindow(TEXT("button"), TEXT("File save"),WS_VISIBLE | WS_CHILD ,120, 30, 80, 25,hwnd, (HMENU) ID_BUTTON1, NULL, NULL);
break;
}
case WM_COMMAND:
{
if (HIWORD(wParam) == BN_CLICKED)
{
switch (LOWORD(wParam))
{
case ID_BUTTON:
OpenDialog(hwnd);
break;
case ID_BUTTON1:
SaveDialog(hwnd);
break;
}
}
break;
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//fill file open structure and open common dialog box
void OpenDialog(HWND hwnd) {
OPENFILENAME ofn = { 0 }; // common dialog box structure
TCHAR szFileName[MAX_PATH]; // buffer for file name
// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFile = szFileName;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(szFileName);
ofn.lpstrFilter = TEXT("All\0*.*\0Text\0*.TXT\0");
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
// Display the Open dialog box.
if (GetOpenFileName(&ofn))
{
HANDLE hFile;
hFile = CreateFile(szFileName,GENERIC_READ,0,(LPSECURITY_ATTRIBUTES) NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,(HANDLE) NULL);
CloseHandle(hFile);
}
}
//fill file open structure and save common dialog box
void SaveDialog(HWND hwnd) {
OPENFILENAME ofn = { 0 }; // common dialog box structure
TCHAR szFileName[MAX_PATH];//// buffer for file name
// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize= sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = TEXT("Text Files (*.txt)\0*txt\0All Files (*.*)\0*.*\0");
ofn.lpstrFile = szFileName;
ofn.lpstrFile[0] = TEXT('\0');
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY |OFN_OVERWRITEPROMPT;
ofn.lpstrDefExt = TEXT("txt");
// Display the Save dialog box.
if(GetSaveFileName(&ofn))
{
HANDLE hFile;
hFile=CreateFile(szFileName, GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ, NULL, OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL, NULL );
DWORD dwByteCount;
WriteFile( hFile, TEXT("Common dilog demo"), 17, &dwByteCount, NULL );
CloseHandle(hFile);
}
}
Creating a Simple Window |
Common Elements |
Data Types and Character Sets |
The Device Context |
Graphics Device Interface |
Displaying Text |
Displaying Graphics |
Mapping Modes |
Keyboard Input |
Working with the Mouse |
Menus |
Child Windows |
ScrollBar Control |
The Dialog Box |
Windows Message Box |
Common Dialog Box |
Bitmaps |
Common Controls |
Creating a Toolbar |
Multiple Document Interface |
Timers |
DLL’s |
Creating Custom Controls |
Owner Drawn Controls |
API Hooking and DLL Injection |
File Management Functions |
String Manipulation |
System Information Functions |