I have an Win32 EDIT
control with an assigned ID (IDC_FILE_NAME_INPUT
) and I want the Save Dialog to open up when this control is clicked.
I handle this in the WM_COMMAND
message of the window processor:
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case IDC_FILE_NAME_INPUT:
{
SetFocus(hWnd); // If I remove this the dialog opens every time it's closed
OPENFILENAME ofn;
wchar_t szFileName[MAX_PATH] = TEXT("");
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.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY;
ofn.lpstrDefExt = TEXT("txt");
if (GetSaveFileName(&ofn))
{
}
break;
}
}
}
break;
When I click on the EDIT
control, the dialog opens up but when I close it (Cancel, Save or X), it shows up again. Only this time, when I close it again, it stays closed. If I remove the SetFocus(hWnd);
part, it just keeps opening itself until I close the entire application. I need to know how to make the EDIT
lose focus as soon as the dialog is open, or maybe I should do this with another message than WM_COMMAND
The EDIT
control's parent is sent WM_COMMAND
for a multitude of different events. This is documented here: https://msdn.microsoft.com/en-us/library/windows/desktop/bb775458(v=vs.85).aspx. Have a look at how many different events are notified this way.
You are treating all such notifications in the same way. Instead you need to discriminate on the notification code which is passed in the high word of wParam
. You need to test that notification code and only respond to the specific event or events of interest. So it's not enough to detect notification of an event for that control, you must also detect which event it is.