I have these functions for opening and "Save As"ing files, but how would I create a function that, if it has already been saved, saves it without a dialog box, and if it hasn't been saved, open the save as dialog box? Thanks !
Code:
BOOL LoadTextFileToEdit(HWND hEdit, LPCTSTR pszFileName) {
HANDLE hFile;
BOOL bSuccess = FALSE;
hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if(hFile != INVALID_HANDLE_VALUE) {
DWORD dwFileSize;
dwFileSize = GetFileSize(hFile, NULL);
if(dwFileSize != 0xFFFFFFFF) {
LPSTR pszFileText;
pszFileText = GlobalAlloc(GPTR, dwFileSize + 1);
if(pszFileText != NULL) {
DWORD dwRead;
if(ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL)) {
pszFileText[dwFileSize] = 0;
if(SetWindowText(hEdit, pszFileText)) {
bSuccess = TRUE;
}
}
GlobalFree(pszFileText);
}
}
CloseHandle(hFile);
}
return bSuccess;
}
BOOL SaveAsTextFileFromEdit(HWND hEdit, LPCTSTR pszFileName) {
HANDLE hFile;
BOOL bSuccess = FALSE;
hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(hFile != INVALID_HANDLE_VALUE) {
DWORD dwTextLength;
dwTextLength = GetWindowTextLength(hEdit);
if(dwTextLength > 0) {
LPSTR pszText;
DWORD dwBufferSize = dwTextLength + 1;
pszText = GlobalAlloc(GPTR, dwBufferSize);
if(pszText != NULL) {
if(GetWindowText(hEdit, pszText, dwBufferSize)) {
DWORD dwWritten;
if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL)) {
bSuccess = TRUE;
}
}
GlobalFree(pszText);
}
}
CloseHandle(hFile);
}
return bSuccess;
}
// BOOL SaveTextFileFromEdit(HWND hEdit, LPCTSTR pszFileName) {} // Heres what i want
void DoFileOpen(HWND hwnd) {
OPENFILENAME ofn;
char szFileName[MAX_PATH] = "";
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0HTML File (*.html)\0*.html\0Cascading Style Sheet (*.css)\0*.css\0XML Document (*.xml)\0*.xml\0XSL StyleSheet (*.xsl)\0*.xsl\0PERL Source Code (*.pl)\0*.pl\0PHP Source Code (*.php)\0*.php\0C Source Code File (*.c)\0*.c\0C++ Source Code File (*.cpp)\0*.cpp\0C/C++ Header File (*.h)\0*.h\0Java Source Code (*.java)\0*.java\0Pascal Source Code (*.pas)\0*.pas\0All Files (*.*)\0*.*\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrDefExt = "txt";
if(GetOpenFileName(&ofn)) {
HWND hEdit = GetDlgItem(hwnd, ID_CHILD_EDIT);
if(LoadTextFileToEdit(hEdit, szFileName)) {
SetWindowText(hwnd, szFileName);
}
}
}
// void DoFileSave(HWND hwnd) {} //heres what i want
void DoFileSaveAs(HWND hwnd) {
OPENFILENAME ofn;
char szFileName[MAX_PATH] = "";
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0HTML File (*.html)\0*.html\0Cascading Style Sheet (*.css)\0*.css\0XML Document (*.xml)\0*.xml\0XSL StyleSheet (*.xsl)\0*.xsl\0PERL Source Code (*.pl)\0*.pl\0PHP Source Code (*.php)\0*.php\0C Source Code File (*.c)\0*.c\0C++ Source Code File (*.cpp)\0*.cpp\0C/C++ Header File (*.h)\0*.h\0Java Source Code (*.java)\0*.java\0Pascal Source Code (*.pas)\0*.pas\0All Files (*.*)\0*.*\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrDefExt = "txt";
ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
if(GetSaveFileName(&ofn)) {
HWND hEdit = GetDlgItem(hwnd, ID_CHILD_EDIT);
if(SaveAsTextFileFromEdit(hEdit, szFileName)) {
SetWindowText(hwnd, szFileName);
}
}
}