如何添加自定义项目到C ++中的系统菜单? [英] How to add custom item to system menu in C++?
问题描述
我需要枚举所有正在运行的应用程序。特别是,所有的顶级窗口。
I need to enumerate all running applications. In particular, all top windows. And for every window I need to add my custom item to the system menu of that window.
如何在C ++中实现这一目标?
How can I accomplish that in C++?
更新。
我将非常乐意为Windows,MacOS和Ubuntu ,我不知道是否MacOS和Ubuntu有这样的东西作为系统菜单)。
I would be more than happy to have a solution for Windows, MacOS, and Ubuntu (though, I'm not sure if MacOS and Ubuntu have such thing as 'system menu').
推荐答案
获取顶级窗口(除了EnumWindows,它使用回调)是获取桌面的第一个孩子,然后检索所有的兄弟姐妹:
For Windows, another way to get the top-level windows (besides EnumWindows, which uses a callback) is to get the first child of the desktop and then retrieve all its siblings:
HWND wnd = GetWindow(GetDesktopWindow(), GW_CHILD);
while (wnd) {
// handle 'wnd' here
// ...
wnd = GetNextWindow(wnd, GW_HWNDNEXT);
}
至于获取系统菜单, GetSystemMenu
函数,以FALSE作为第二个参数。 GetMenu $ c $
As for getting the system menu, use the GetSystemMenu
function, with FALSE as the second argument. The GetMenu
mentioned in the other answers returns the normal window menu.
请注意,虽然添加了自定义菜单项到外部进程的窗口很容易,响应选择的项目是有点棘手。你必须在进程中注入一些代码,以便能够子窗口,或者安装一个全局钩子(可能是一个 WH_GETMESSAGE
或 WH_CBT
type)来监视 WM_SYSCOMMAND
消息。
Note, however, that while adding a custom menu item to a foreign process's window is easy, responding to the selection of that item is a bit tricky. You'll either have to inject some code to the process in order to be able to subclass the window, or install a global hook (probably a WH_GETMESSAGE
or WH_CBT
type) to monitor WM_SYSCOMMAND
messages.
这篇关于如何添加自定义项目到C ++中的系统菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!