Kill只杀死当前焦点窗口,不杀死在相同PID下运行的其他窗口。

huangapple go评论67阅读模式
英文:

Kill only the currently focused window without killing others running under the same PID

问题

我要写一个使用Windows API在C++中关闭当前焦点窗口的函数。到目前为止,我已经成功地为每次在不同进程中打开新窗口的应用程序编写了代码。通过查看代码,这一点显而易见。问题出现在应用程序在相同PID下打开多个窗口时,例如记事本,如下面的截图所示:Kill只杀死当前焦点窗口,不杀死在相同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:Kill只杀死当前焦点窗口,不杀死在相同PID下运行的其他窗口。

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).

huangapple
  • 本文由 发表于 2023年6月9日 05:00:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76435657.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定