gpt4 book ai didi

c - 为什么 FindWindowEx 找不到 IP 地址窗口?

转载 作者:太空宇宙 更新时间:2023-11-04 03:09:10 25 4
gpt4 key购买 nike

如下创建一个 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);

最佳答案

IP Address controlnot a standard edit control .将诸如 "ServerIpAddress" 之类的窗口文本分配给 IP 地址控件是没有意义的,并且它很可能在窗口创建期间被丢弃。要将 IP 地址分配给 IP 地址控件,您必须使用 IPM_SETADDRESS窗口消息。

此外,根据 FindWindowEx()文档:

If the lpszWindow parameter is not NULL, FindWindowEx calls the GetWindowText function to retrieve the window name for comparison.

GetWindowText() 不适用于 IP 地址控件,就像它用于标准编辑控件一样。要从 IP 地址控件检索 IP 地址,您必须使用 IPM_GETADDRESS窗口消息。

因此,当使用 FindWindow/Ex() 时,无法通过其文本内容找到 IP 地址控件,就像标准编辑控件一样。它只能通过其 WC_IPADDRESS 类名找到。

要执行您正在尝试的操作,您需要:

  1. 保存 CreateWindow() 返回的 IP 地址控件的 HWND(这是首选解决方案):

    HWND gIpAddress; // stored somewhere that you can reach it

    ...

    // hwnd is the parent of the IP address window
    gIpAddress = CreateWindowW(WC_IPADDRESS,
    L"",
    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);

    ...

    DWORD dwAddr = 0x0;
    SendMessage(gIpAddress, IPM_GETADDRESS, 0, (LPARAM)&dwAddr);
    ...
  2. 为 IP 地址控件指定一个 Control ID(在 CreateWindow()hMenu 参数中),然后使用GetDlgItem()在需要时获取 IP 地址控件的 HWND:

    // hwnd is the parent of the IP address window
    HWND eIPAddress = CreateWindowW(WC_IPADDRESS,
    L"",
    WS_CHILD | WS_OVERLAPPED | WS_VISIBLE,
    MulDiv(LOWORD(units), 40, 4), 50,
    MulDiv(LOWORD(units), 70, 4),
    MulDiv(HIWORD(units), 11, 8),
    hwnd, (HMENU)12345, NULL, NULL);

    ...

    // hwnd is the parent of the IP address window
    HWND wnd_server_ipaddress = GetDlgItem(hwnd, 12345);
    DWORD dwAddr = 0x0;
    SendMessage(wnd_server_ipaddress, IPM_GETADDRESS, 0, (LPARAM)&dwAddr);
    ...

关于c - 为什么 FindWindowEx 找不到 IP 地址窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58365163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com