【发布时间】:2019-10-13 15:43:24
【问题描述】:
如下创建了一个IP地址窗口,然后尝试使用FindWindowEx函数找到它:
INITCOMMONCONTROLSEX icex;
icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
icex.dwICC = ICC_INTERNET_CLASSES;
InitCommonControlsEx(&icex);
// hwnd is the parent of the IP address window
HWND eIpAddress = CreateWindowW(WC_IPADDRESS,
L"ServerIpAddress",
WS_CHILD | WS_OVERLAPPED | WS_VISIBLE,
MulDiv(LOWORD(units), 40, 4), 50,
MulDiv(LOWORD(units), 70, 4),
MulDiv(HIWORD(units), 11, 8),
hwnd, NULL, NULL, NULL);
// try to retrieve the control; does not work, returns null
HWND wnd_server_ipaddress = FindWindowEx(hwnd, NULL, NULL, L"ServerIpAddress");
DWORD err = GetLastError(); // --> returns 0
但是wnd_server_ipaddress 是 NULL。我正在对另外两个名称不同的标准编辑窗口做同样的事情,并且它正在工作。查看 Spy++ 以确保层次结构正确无误。调用后添加GetLastError(),返回0。
// works
HWND wnd_server_name = FindWindowEx(hwnd, NULL, NULL, L"ServerName");
// does not work
HWND wnd_server_ipaddress = FindWindowEx(hwnd, NULL, WC_IPADDRESS, L"ServerIpAddress");
HWND wnd_server_ipaddress2 = FindWindow(WC_IPADDRESS, L"ServerIpAddress");
// works
wchar_t server_name[512] = { 0 };
GetWindowText(wnd_server_name, server_name, 512);
// does not work because wnd_server_ipaddress is null
wchar_t server_ipaddress[16] = { 0 };
DWORD dwAddr = 0x0;
int iCount = (int)SendMessage(wnd_server_ipaddress, IPM_GETADDRESS, 0, (LPARAM)&dwAddr);
_snwprintf_s(server_ipaddress, sizeof(server_ipaddress) / sizeof(*server_ipaddress), 16,
L"%ld.%ld.%ld.%ld",
(dwAddr >> 24) & 0xff,
(dwAddr >> 16) & 0xff,
(dwAddr >> 8) & 0xff,
(dwAddr) & 0xff);
问题:是否有特定于 WC_IPADDRESS 的东西导致 FindWindowEx 找不到控件?
编辑 添加了适用于标准控件的代码。
// creation with ServerName as lpWindowName
HWND eName = CreateWindowW(L"Edit", L"ServerName",
WS_CHILD | WS_VISIBLE | WS_BORDER,
MulDiv(LOWORD(units), 40, 4), 10,
MulDiv(LOWORD(units), 150, 4),
MulDiv(HIWORD(units), 11, 8),
hwnd, NULL, NULL, NULL);
// reset text
SetWindowText(eName, L"");
// retrieve control - works
HWND wnd_server_name = FindWindowEx(hwnd, NULL, NULL, L"ServerName");
// retrieve value - works, gets whatever the text in the control is
wchar_t server_name[512] = { 0 };
GetWindowText(wnd_server_name, server_name, 512);
【问题讨论】:
-
致反对者:你能解释一下这个问题到底有什么问题吗?
-
一旦用户更改编辑字段的内容,您在标准编辑控件上使用
FindWindowEx()就会失败。 -
@RemyLebeau 我为标准编辑控件添加了整个代码。不确定我们是否在谈论同一件事,但它并没有失败。它适用于更改字段的内容,我检索用户在其中键入的内容。
-
代码可以工作的唯一方法是如果有另一个编辑控件以
ServerName作为其文本内容。仔细检查CreateWindow()和FindWindowEx()是否返回相同的HWND。就像我在另一条评论中所说的那样,Win32 API 中没有窗口名称。窗口由其HWND或父/子 ID 标识,而不是由名称字符串标识。