How do I bring a processes window to the foreground on X Windows? (C++)

前端 未结 5 2016
小鲜肉
小鲜肉 2020-12-25 08:47

I have the PID for the process (and the name), I want to bring it to the front on linux (ubuntu). On mac I would simply do SetFrontProcess(pid), on windows I\'d

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-25 09:02

    I had this issue in my application as well, so here's the solution.

    To raise the window you need not only to raise it, but you also need to notify the WM about it. The following code could be used:

            // This is how to get it in Qt; if you don't use it,
            // you can call XOpenDisplay and get it from there;
            Display * display = x11Info().display();
    
            // Main window identifier of your application
            WId win = winId();
    
            XEvent event = { 0 };
            event.xclient.type = ClientMessage;
            event.xclient.serial = 0;
            event.xclient.send_event = True;
            event.xclient.message_type = XInternAtom( display, "_NET_ACTIVE_WINDOW", False);
            event.xclient.window = win;
            event.xclient.format = 32;
    
            XSendEvent( display, DefaultRootWindow(display), False, SubstructureRedirectMask | SubstructureNotifyMask, &event );
            XMapRaised( display, win );
    

提交回复
热议问题