我已经注册了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);
}
}
单击对话框中的“查找下一个”应该会在控制台中生成消息,但不会发生任何事情。
答案 0 :(得分:2)
正如它在the documentation的开头句中所述:
当用户单击“查找下一个”,“替换”或“全部替换”按钮时,“查找或替换”对话框将发送 FINDMSGSTRING注册消息发送到其所有者窗口的窗口过程,或关闭对话框。
(强调我的。)已发送的消息直接传递到窗口过程,GetMessage
不会检索。一般情况下,您不应该使用GetConsoleWindow
句柄来托管UI,因为您无权访问其消息过程,因此这样的事情将无效。
答案 1 :(得分: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);
}
}