0

我已经注册了 FINDMSGSTRINGW,但它没有显示在主循环中:

#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);
  }
}

单击对话框上的“查找下一个”应在控制台中产生消息,但没有任何反应。

4

2 回答 2

2

正如它在文档的开场白中所说:

当用户单击“查找下一个”、“替换”或“全部替换”按钮或关闭对话框时,查找或替换对话框将FINDMSGSTRING 注册消息发送到其所有者窗口的窗口过程。

(强调我的。)发送的消息直接传递到窗口过程,而不是由GetMessage. 一般来说,您不应该使用GetConsoleWindow句柄来托管 UI,因为您无权访问它的消息过程,因此这样的事情不会起作用。

于 2013-03-15T00:11:30.213 回答
0

我看错地方了。该消息显示在对话框的父窗口的窗口过程中,这是工作代码:

#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);
  }
}
于 2013-03-15T00:13:57.477 回答