0

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

ali
  • 10,927
  • 20
  • 89
  • 138
  • is "IDC_FILE_NAME_INPUT" sent when you close, somehow? – Charlie Oct 15 '16 at 20:30
  • @DavidHeffernan, I am trying to simulate the behavior of an HTML file input, where when you click on it, the dialog is open. – ali Oct 15 '16 at 20:31
  • It's kind of an odd design to make clicking on an edit control do something other than activate the edit control. The usual interface would have an associated "Browse" button which opens the file dialog. – Jonathan Potter Oct 15 '16 at 21:36
  • "*I want the Save Dialog to open up when **this control is clicked**.*" - then why are you using `WM_COMMAND` at all, and not something more like `WM_LBUTTONDOWN/UP` instead? – Remy Lebeau Oct 16 '16 at 02:53
  • This is going to make for the worst imaginable user experience. It is so criminally non-standard, and inconsistent, that it's hard to understand, what thought process lead to this design decision. Users do not expect that clicking inside an Edit Control would issue a command. And if they did, they would expect keyboard navigation to have the same effect. Please, stop doing what you are trying to do, before it's too late. – IInspectable Oct 16 '16 at 11:39

1 Answers1

3

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490