英文:
Kill only the currently focused window without killing others running under the same PID
问题
我要写一个使用Windows API在C++中关闭当前焦点窗口的函数。到目前为止,我已经成功地为每次在不同进程中打开新窗口的应用程序编写了代码。通过查看代码,这一点显而易见。问题出现在应用程序在相同PID下打开多个窗口时,例如记事本,如下面的截图所示:
因此,我的问题是:如何只关闭焦点窗口,而不关闭具有相同PID的其他窗口。
这是我目前拥有的代码:
void kill_focused_window()
{
HWND focused = GetForegroundWindow();
DWORD procID;
GetWindowThreadProcessId(focused, &procID);
HANDLE procHandle = OpenProcess(PROCESS_TERMINATE, FALSE, procID);
TerminateProcess(procHandle, 0);
CloseHandle(procHandle);
}
英文:
I want to write a function that kills the currently focused window in c++ using the windows API. What I have so far works fine for applications that open new windows in a different process each time. This is obvious by looking at the code. The problem is when the application opens multiple windows under the same PID an example being notepad as illustrated in screenshot below:
My question thus becomes: How can I kill only the focused window without killing others with the same PID.
This is what I have at the moment:
void kill_foucused_window()
{
HWND focused = GetForegroundWindow();
DWORD procID;
GetWindowThreadProcessId(focused, &procID);
HANDLE procHandle = OpenProcess(PROCESS_TERMINATE, FALSE, procID);
TerminateProcess(procHandle, 0);
CloseHandle(procHandle);
}
答案1
得分: 0
外部可以终止线程或进程(如果有足够的权限),但不能终止不是你所拥有的窗口。只有创建窗口的线程可以销毁窗口。
外部,你只能请求窗口关闭自己(即发送 WM_CLOSE
消息),希望窗口的所有者选择不忽略关闭请求(这是可能的)。
英文:
Externally, you can kill a thread or a process (if you have adequate permissions), but you can't kill a window that you don't own. Only the thread that created the window can destroy the window.
Externally, all you can do is request the window to close itself (ie, send it a WM_CLOSE
messsage) and hope the window's owner doesn't chooses to ignore the closure (which it can).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论