How to add custom item to system menu in C++?

后端 未结 4 1267
刺人心
刺人心 2021-01-13 03:05

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.

Ho

4条回答
  •  半阙折子戏
    2021-01-13 03:58

    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);
    }
    

    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.

    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.

提交回复
热议问题