问题
Sup
What i'm trying to do is a way to find a window though it's process ID, get the name of that window and change it to something that i want.
I found some things about that : ( Code 1 )
int WINAPI GetWindowText(
_In_ HWND hWnd,
_Out_ LPTSTR lpString,
_In_ int nMaxCount
);
this : ( Code 2 )
CWnd* pWnd = GetDlgItem(); GetDlgIt
pWnd->SetWindowText(_T("WindowName"));
CString str;
pWnd->GetWindowText(str);
ASSERT(str == _T("WindowName"));
and also this ( Code 3 )
HWND WindowHandel = FindWindowA(0, "WindowName");
DWORD proccesID = 0
GetWindowThreadProcessId(WindowHandel, &proccesID);
My questions are : How can i get the Process ID, get the window name of that process, put that so the codes can recognize Hwnd as my window name and change it for something that i want. Something like this :
Process = "anyprocess.exe"
Get Process ID <
Process ID = 1234567
Get window name from the ProcessID we have <
Window name = "ILoveYou"
Change "ILoveYou" to "IHaveYou"
And if i have more then one window with the same name, theres a way to check the first one opened so i don't change the other windows ?
In my head, taking the WindowName from the process id seems safer, the thing is that i don't wanna say to the code that i know the window name, i want it to find it and change it just like Liam Neeson did with those bad guys.
And i also would like to know more about using those "(0,...." or "(NULL,....", the "FindWindowA" command was just an example, i see this always and don't know how to use it properly :
FindWindowA(0, "WindowName")
An example of what i want would help me a lot =) Thanks for your patience.
回答1:
No, this cannot be done. More than one window can have the same PID.
However, assuming that the target application is single-threaded with only one visible window, you can go through all the windows and check their PID to find a match. You have to skip the invisible windows. Or better yet, skip non Alt-Tab windows. Here is how it's done.
Note, I just wrote this for fun, I wouldn't put this in any application myself.
BOOL IsAltTabWindow(HWND hwnd)
{
if (!IsWindowVisible(hwnd)) return FALSE;
HWND next = NULL;
HWND parent = GetAncestor(hwnd, GA_ROOTOWNER);
while (parent != next)
{
next = parent;
parent = GetLastActivePopup(parent);
if (IsWindowVisible(parent)) break;
}
if (next != hwnd) return FALSE;
TITLEBARINFO ti = { 0 };
ti.cbSize = sizeof(ti);
GetTitleBarInfo(hwnd, &ti);
if (ti.rgstate[0] & STATE_SYSTEM_INVISIBLE) return FALSE;
if (GetWindowLong(hwnd, GWL_EXSTYLE) & WS_EX_TOOLWINDOW) return FALSE;
return TRUE;
}
BOOL CALLBACK enumProc(HWND hwnd, LPARAM lp)
{
DWORD search_pid = DWORD(lp);
if (!IsAltTabWindow(hwnd))
return 1;
DWORD pid = 0;
GetWindowThreadProcessId(hwnd, &pid);
if (pid != search_pid) return 1;
SetWindowText(hwnd, "un-advised code");
return 0;
}
int main()
{
DWORD search_pid = 0;
HWND hwnd = FindWindow(0, "Find me");
GetWindowThreadProcessId(hwnd, &search_pid);
EnumWindows(enumProc, LPARAM(search_pid));
return 0;
}
Edit, explanation:
Try the code below. First run Window's notepad, make sure there is only one instance of notepad.
If you run the code below on Windows 10, it will show not one, but 3 windows with the same PID as Notepad. 2 of those windows are invisible, they are used by operating system, we are not supposed to touch those.
So we want to skip invisible windows, at the very least. There are a number of other system windows, we don't want anything to do with them either.
IsAltTabWindow
is good way to avoid those windows. This makes sure we only look through windows which can be accessed through Alt-Tab key.
int main()
{
HWND hwnd = FindWindow("Notepad", 0);
if (!hwnd) return 0;
DWORD search_pid = 0;
GetWindowThreadProcessId(hwnd, &search_pid);
for (hwnd = GetWindow(GetDesktopWindow(), GW_CHILD);
hwnd; hwnd = GetWindow(hwnd, GW_HWNDNEXT))
{
char buf[300];
GetWindowText(hwnd, buf, 300);
DWORD pid = 0;
GetWindowThreadProcessId(hwnd, &pid);
if (pid == search_pid)
std::cout << pid << "\n";
}
return 0;
}
回答2:
You might have a look to this old MFC based application published in www.codeguru.com 15 years ago (time flies...).
The mechanism of getting the HWND based on a mouse-click on the screen might interest you.
http://www.codeguru.com/cpp/misc/samples/article.php/c1465/Password-Retrieval-Application.htm
Z.
来源:https://stackoverflow.com/questions/35917056/find-window-and-change-its-name