MCPcopy Create free account
hub / github.com/WallBreaker2/op / FindWindowByProcess

Method FindWindowByProcess

libop/window/WindowService.cpp:1722–1825  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1720}
1721
1722bool WindowService::FindWindowByProcess(const wchar_t *class_name, const wchar_t *title, HWND &rethwnd,
1723 const wchar_t *process_name, DWORD Pid) {
1724 bool bret = false;
1725 rethwnd = nullptr;
1726 if (process_name) {
1727 if (wcslen(process_name) < 1)
1728 return false;
1729 npid.clear();
1730 enum_process_success_count = 0;
1731 if (EnumProcessbyName(0, process_name) == false)
1732 return false;
1733
1734 HWND p = ::GetWindow(GetDesktopWindow(), GW_CHILD); // 获取桌面窗口的子窗口
1735 p = ::GetWindow(p, GW_HWNDFIRST);
1736 while (p != NULL) {
1737 if (::IsWindowVisible(p) && ::GetWindow(p, GW_OWNER) == 0) {
1738 DWORD pid = 0;
1739 GetWindowThreadProcessId(p, &pid);
1740 if (EnumProcessbyName(pid, process_name)) {
1741 if (wcslen(class_name) < 1 && wcslen(title) < 1) {
1742 rethwnd = p;
1743 bret = true;
1744 break;
1745 } else {
1746 auto WindowClassName = WindowClassNameText(p);
1747 auto WindowTitle = WindowTitleText(p);
1748 if (wcslen(WindowClassName) > 1 && wcslen(WindowTitle) > 1) {
1749 const wchar_t *strfindclass = wcsstr(WindowClassName, class_name); // 模糊匹配
1750 const wchar_t *strfindtitle = wcsstr(WindowTitle, title); // 模糊匹配
1751 if ((wcslen(class_name) >= 1 && strfindclass) || (wcslen(title) >= 1 && strfindtitle)) {
1752 rethwnd = p;
1753 bret = true;
1754 break;
1755 }
1756 }
1757
1758 HWND child_hwnd = ::GetWindow(p, GW_CHILD);
1759 if (child_hwnd != NULL) {
1760 const wchar_t *classname = NULL;
1761 const wchar_t *titles = NULL;
1762 if (wcslen(class_name) > 0)
1763 classname = class_name;
1764 if (wcslen(title) > 0)
1765 titles = titles;
1766 HWND dret = FindChildWnd(child_hwnd, titles, classname, NULL, false, false, process_name);
1767 if (dret != nullptr) {
1768 rethwnd = dret;
1769 bret = true;
1770 break;
1771 }
1772 }
1773 }
1774 }
1775 }
1776 p = ::GetWindow(p, GW_HWNDNEXT); // 获取下一个窗口
1777 }
1778 } else if (Pid > 0) {
1779 HWND p = ::GetWindow(GetDesktopWindow(), GW_CHILD); // 获取桌面窗口的子窗口

Callers

nothing calls this directly

Calls 3

WindowClassNameTextFunction · 0.85
WindowTitleTextFunction · 0.85
clearMethod · 0.45

Tested by

no test coverage detected