سؤال

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.

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

I looked in a wrong place. The message shows up in window procedure of parent window of the dialog, here is working code:

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

UINT uFindReplaceMsg = RegisterWindowMessageW(FINDMSGSTRINGW);

LRESULT CALLBACK MyWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
  if (Msg == uFindReplaceMsg) std::cout << "uFindReplaceMsg catched" << std::endl;
  return DefWindowProcW(hWnd, Msg, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR, int nCmdShow) {
  using namespace std;

  WNDCLASSEXW wc;
  wc.cbSize = sizeof(WNDCLASSEXW);
  wc.style = CS_HREDRAW | CS_VREDRAW;
  wc.lpfnWndProc = &MyWndProc;
  wc.cbClsExtra = 0;
  wc.cbWndExtra = sizeof(PVOID);
  wc.hInstance = hInstance;
  wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  wc.hIconSm = NULL;
  wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  wc.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_BACKGROUND);
  wc.lpszMenuName = L"MainMenu";
  wc.lpszClassName = L"window";
  ATOM class_atom = RegisterClassExW(&wc);

  HWND hWnd = CreateWindowExW(
    0,
    reinterpret_cast<LPCWSTR>(class_atom),
    L"window title",
    WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPCHILDREN | WS_THICKFRAME,
    CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
    NULL,
    NULL,
    hInstance,
    NULL
  );

  WCHAR szFindWhat[MAX_PATH] = {0};

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

  MSG msg;
  for (;;) {
    GetMessageW(&msg, 0, 0, 0);
    TranslateMessage(&msg);
    DispatchMessageW(&msg);
  }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top