winapiwindows-messagescommon-dialog

how to make FindText dialog put its messages onto main loop?


I have registered FINDMSGSTRINGW but it doesn't show on the main loop:

#include <windows.h>
#include <iostream>

int main() {
  using namespace std;
  UINT uFindReplaceMsg = RegisterWindowMessageW(FINDMSGSTRINGW);
  WCHAR szFindWhat[MAX_PATH] = {0};  // buffer receiving string

  FINDREPLACEW fr;
  ZeroMemory(&fr, sizeof(fr));
  fr.lStructSize = sizeof(fr);
  fr.hwndOwner = GetConsoleWindow();
  fr.lpstrFindWhat = szFindWhat;
  fr.wFindWhatLen = MAX_PATH;
  fr.Flags = 0;
  HWND hdlg = FindTextW(&fr);

  MSG msg;
  for (;;) {
    GetMessageW(&msg, 0, 0, 0);
    TranslateMessage(&msg);
    if (msg.message == uFindReplaceMsg) {
      cout << "uFindReplaceMsg detected" << endl;
    }
    DispatchMessageW(&msg);
  }
}

Clicking "find next" on the dialog should produce messages in console but nothing is happening.


Solution

  • As it states in the opening sentence of the documentation:

    A Find or Replace dialog box sends the FINDMSGSTRING registered message to the window procedure of its owner window when the user clicks the Find Next, Replace, or Replace All button, or closes the dialog box.

    (Emphasis mine.) Sent messages are delivered directly to the window procedure and are not retrieved by GetMessage. In general, you should not be using the GetConsoleWindow handle for hosting UI because you do not have access to its message procedure and therefore things like this will not work.