您当前的位置: 首页 >  windows

柳鲲鹏

暂无认证

  • 0浏览

    0关注

    4642博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

X11获取windows名称和ID代码

柳鲲鹏 发布时间:2021-12-20 09:10:34 ,浏览量:0

源码

#include 
#include 
#include 
#include 

#include  
#include  

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

关注
打赏
1665724893
查看更多评论
立即登录/注册

微信扫码登录

0.0810s