I'm trying to implement a "Save" capability that uses the existing filename (I already have an OPENFILENAME "Save As"). I'm using the window titlebar to display the file name when a file is opened or saved. The trouble is, when I attempt to use this name in my SaveTextFromEdit(); function using
GetWindowText();, when I click "Save" in the menu, my program crashes ("acknex.exe has encountered a problem and needs to close.").
Here's the
Code:
My save function:
BOOL SaveTextFromEdit(HWND hEdit, LPCSTR 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);
// No need to bother if there's no text.
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;
}
My WndProc:
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
{
PAINTSTRUCT paint;
HDC hdc;
switch(msg)
{
case WM_CREATE:
{
HWND hedit= CreateWindowEx(WS_EX_STATICEDGE,"EDIT",EditContent,
WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
0,0,100,100,hwnd,(HMENU)IDC_MAIN_EDIT,GetModuleHandle(NULL),NULL); //creating the edit window
//creating menus:
long menu= CreateMenu();
long SubMenu= CreateMenu();
InsertMenu(menu,0,MF_BYPOSITION | MF_STRING | MF_POPUP,SubMenu,"&File");
//"File" menu items:
InsertMenu(SubMenu,0,MF_BYPOSITION | MF_STRING,ID_FILE_NEW,"&New");
InsertMenu(SubMenu,1,MF_BYPOSITION | MF_STRING,ID_FILE_OPEN,"&Open...");
InsertMenu(SubMenu,2,MF_BYPOSITION | MF_STRING,ID_FILE_SAVE,"Save &As...");
InsertMenu(SubMenu,3,MF_BYPOSITION | MF_STRING,ID_FILE_SAVE_WITH_CURRENT_NAME,"&Save");
InsertMenu(SubMenu,4,MF_BYPOSITION | MF_SEPARATOR,NULL,NULL);
InsertMenu(SubMenu,5,menuSettings,ID_FILE_OPEN_CURRENT_TEXT,"Open &Current Text on start");
InsertMenu(SubMenu,6,MF_BYPOSITION | MF_STRING,NULL,"Open F&ile on start...");
InsertMenu(SubMenu,7,MF_BYPOSITION | MF_SEPARATOR,NULL,NULL);
InsertMenu(SubMenu,8,MF_BYPOSITION | MF_STRING,ID_FILE_EXIT,"&Quit");
SetMenu(hwnd,menu);
}
break;
case WM_SIZE:
{
HWND hedit= GetDlgItem(hwnd,IDC_MAIN_EDIT);
RECT ClientRect;
GetClientRect(hwnd,&ClientRect);
SetWindowPos(hedit,NULL,0,0,ClientRect.right,ClientRect.bottom,SWP_NOZORDER);
}
break;
case WM_PAINT:
hdc= BeginPaint(hwnd,&paint);
EndPaint(hwnd,&paint);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_COMMAND:
switch(wparam)
{
case ID_FILE_NEW:
SetDlgItemText(hwnd,IDC_MAIN_EDIT,"");
SetWindowText(hwnd,WindowTitle);
case ID_FILE_OPEN:
Open(hwnd);
break;
case ID_FILE_SAVE:
Save(hwnd);
break;
case ID_FILE_OPEN_CURRENT_TEXT:
Toggle^= TRUE;
if(Toggle== FALSE)
menuSettings= MF_BYPOSITION | MF_STRING | MF_UNCHECKED;
else
menuSettings= MF_BYPOSITION | MF_STRING | MF_CHECKED;
break;
case ID_FILE_SAVE_WITH_CURRENT_NAME:
SaveTextFromEdit(hwnd,GetWindowText(hwnd,FileName,256));
break;
case ID_FILE_EXIT:
PostQuitMessage(0);
break;
}
default:
break;
}
return DefWindowProc(hwnd,msg,wparam,lparam);
}
The red part is the part I'm having issues with. I posted the whole WndProc code because it may have something to do with a conflict elsewhere in my message handling.
(FYI, FileName is a global char that I also use with my OPENFILENAME
Open(); and
Save(); functions enabling me to use the file name for the title bar text.)