Monday, December 22, 2008

Function SaveFile in Visual C++ 2008

void DoFileSave(HWND hwnd)
{
OPENFILENAME ofn;
wchar_t szFileName[MAX_PATH] = _T("");

ZeroMemory(&ofn, sizeof(ofn));

ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = _T("Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0");
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrDefExt = _T("txt");
ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;

if(GetSaveFileName(&ofn))
{
HWND hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT);
if(SaveTextFileFromEdit(hEdit, szFileName))
{
SendDlgItemMessage(hwnd, IDC_MAIN_STATUS, SB_SETTEXT, 0, (LPARAM)_T("Saved..."));
SendDlgItemMessage(hwnd, IDC_MAIN_STATUS, SB_SETTEXT, 1, (LPARAM)szFileName);
}
}
}

Function OpenFile in Visual C++ 2008


void DoFileOpen(HWND hwnd)
{
OPENFILENAME ofn;
wchar_t szFileName[MAX_PATH] = _T("");

ZeroMemory(&ofn, sizeof(ofn));

ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = _T("Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0");
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrDefExt = _T("txt");

if(GetOpenFileName(&ofn))
{
HWND hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT);
if(LoadTextFileToEdit(hEdit, szFileName))
{
SendDlgItemMessage(hwnd, IDC_MAIN_STATUS, SB_SETTEXT, 0, (LPARAM)"Opened...");
SendDlgItemMessage(hwnd, IDC_MAIN_STATUS, SB_SETTEXT, 1, (LPARAM)szFileName);
}
}
}