X11获取windows名称和ID代码

源码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <X11/X.h> 
#include <X11/Xlib.h> 

#define WINDOW_X      100
#define WINDOW_Y      100
#define WINDOW_WIDTH  500
#define WINDOW_HEIGHT 300
#define DISPLAY_TEXT  "Taishan Office"

#define FOREGROUND_COLOR 0xFF0000
#define BACKGROUND_COLOR 0x00FF00


static Display *g_pDisplay;
static Window   g_oWindow;
static Window   g_oWindowRoot;
static int      g_iScreen;

static void window_start()
{
    char     name[128] = {0};

    unsigned long foreground_pixel; 
    unsigned long background_pixel; 

    g_pDisplay = XOpenDisplay(NULL);
    if (g_pDisplay == NULL)
    {
       fprintf(stderr, "Cannot open display\n");
       exit(1);
    }
 
    g_iScreen = DefaultScreen(g_pDisplay);
    g_oWindowRoot = DefaultRootWindow( g_pDisplay );
    //foreground_pixel = WhitePixel(g_pDisplay, g_iScreen); 
    //background_pixel = BlackPixel(g_pDisplay, g_iScreen); 
    foreground_pixel = FOREGROUND_COLOR;
    background_pixel = BACKGROUND_COLOR;
    g_oWindow = XCreateSimpleWindow(g_pDisplay,
        g_oWindowRoot,
        WINDOW_X, WINDOW_Y,
        WINDOW_WIDTH, WINDOW_HEIGHT,
        5, //边框粗细
        foreground_pixel,
        background_pixel);

    XStoreName(g_pDisplay, g_oWindow, DISPLAY_TEXT);
    XSetIconName(g_pDisplay, g_oWindow, DISPLAY_TEXT);

    XMapWindow(  g_pDisplay, g_oWindow);

    //XCreateSimpleWindow无法正确定位,所以使用移动
    XMoveWindow(g_pDisplay, g_oWindow, WINDOW_X, WINDOW_Y);
    XSync(g_pDisplay, False);
}

static void window_event()
{
    //响应ReparentNotify
    XSelectInput(g_pDisplay, g_oWindowRoot, SubstructureNotifyMask);
    //响应按键事件
    XSelectInput(g_pDisplay, g_oWindow,     ExposureMask | KeyPressMask);
    XEvent   event;
    while (1)
    {
        XNextEvent(g_pDisplay, &event);
        if ( event.type == ReparentNotify )
        {
            Window   x11window  = g_oWindow;
            Display* x11display = g_pDisplay;
            char **srname = (char **)malloc(sizeof(char *));
            XReparentEvent *reparentevent = (XReparentEvent *)&event;
 
            printf("window: %ld \n", (unsigned long)(reparentevent->window));
            printf("parent: %ld \n", (unsigned long)(reparentevent->parent));
            /*获取到新建窗口的window ID*/
            //x11window  = (unsigned long)(reparentevent->window);
            //x11display = (reparentevent->display);    
            x11window   = g_oWindow;
            x11display  = g_pDisplay;

            XFetchName(x11display, x11window, srname);
            if (*srname != NULL)
            {
                printf("name: %s\n" ,*srname);    
            }
            free(*srname);
        }    
        //任意键退出
        if (event.type == KeyPress)
        {
            break;
        }
    }

}

static void window_close()
{
    XCloseDisplay(g_pDisplay);
}

int main(int argc, char** argv)
{
   window_start();
   window_event();
   window_close();
   return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柳鲲鹏

能给阁下一点帮助,非常荣幸

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值