X11获取windows名称和ID代码

简介: 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;
}


目录
相关文章
|
14天前
|
Windows Python
python获取windows机子上运行的程序名称
python获取windows机子上运行的程序名称
|
14天前
|
Windows
Windows7电脑启动时提示文件winload.exe无法验证其数字签名,错误代码0xc0000428的解决方法
Windows7电脑启动时提示文件winload.exe无法验证其数字签名,错误代码0xc0000428的解决方法
|
2月前
|
Java Windows
【Azure Developer】Windows中通过pslist命令查看到Java进程和线程信息,但为什么和代码中打印出来的进程号不一致呢?
【Azure Developer】Windows中通过pslist命令查看到Java进程和线程信息,但为什么和代码中打印出来的进程号不一致呢?
|
1月前
|
Linux 开发者 Python
从Windows到Linux,Python系统调用如何让代码飞翔🚀
【9月更文挑战第10天】在编程领域,跨越不同操作系统的障碍是常见挑战。Python凭借其“编写一次,到处运行”的理念,显著简化了这一过程。通过os、subprocess、shutil等标准库模块,Python提供了统一的接口,自动处理底层差异,使代码在Windows和Linux上无缝运行。例如,`open`函数在不同系统中以相同方式操作文件,而`subprocess`模块则能一致地执行系统命令。此外,第三方库如psutil进一步增强了跨平台能力,使开发者能够轻松编写高效且易维护的代码。借助Python的强大系统调用功能,跨平台编程变得简单高效。
20 0
|
2月前
|
开发者 C# Windows
WPF与游戏开发:当桌面应用遇见游戏梦想——利用Windows Presentation Foundation打造属于你的2D游戏世界,从环境搭建到代码实践全面解析新兴开发路径
【8月更文挑战第31天】随着游戏开发技术的进步,WPF作为.NET Framework的一部分,凭借其图形渲染能力和灵活的UI设计,成为桌面游戏开发的新选择。本文通过技术综述和示例代码,介绍如何利用WPF进行游戏开发。首先确保安装最新版Visual Studio并创建WPF项目。接着,通过XAML设计游戏界面,并在C#中实现游戏逻辑,如玩家控制和障碍物碰撞检测。示例展示了创建基本2D游戏的过程,包括角色移动和碰撞处理。通过本文,WPF开发者可更好地理解并应用游戏开发技术,创造吸引人的桌面游戏。
112 0
|
2月前
|
C# Windows 开发者
当WPF遇见OpenGL:一场关于如何在Windows Presentation Foundation中融入高性能跨平台图形处理技术的精彩碰撞——详解集成步骤与实战代码示例
【8月更文挑战第31天】本文详细介绍了如何在Windows Presentation Foundation (WPF) 中集成OpenGL,以实现高性能的跨平台图形处理。通过具体示例代码,展示了使用SharpGL库在WPF应用中创建并渲染OpenGL图形的过程,包括开发环境搭建、OpenGL渲染窗口创建及控件集成等关键步骤,帮助开发者更好地理解和应用OpenGL技术。
114 0
|
2月前
|
存储 开发者 C#
WPF与邮件发送:教你如何在Windows Presentation Foundation应用中无缝集成电子邮件功能——从界面设计到代码实现,全面解析邮件发送的每一个细节密武器!
【8月更文挑战第31天】本文探讨了如何在Windows Presentation Foundation(WPF)应用中集成电子邮件发送功能,详细介绍了从创建WPF项目到设计用户界面的全过程,并通过具体示例代码展示了如何使用`System.Net.Mail`命名空间中的`SmtpClient`和`MailMessage`类来实现邮件发送逻辑。文章还强调了安全性和错误处理的重要性,提供了实用的异常捕获代码片段,旨在帮助WPF开发者更好地掌握邮件发送技术,提升应用程序的功能性与用户体验。
40 0
|
2月前
|
iOS开发 Android开发 MacOS
从零到全能开发者:解锁Uno Platform,一键跨越多平台应用开发的神奇之旅,让你的代码飞遍Windows、iOS、Android、macOS及Web,技术小白也能秒变跨平台大神!
【8月更文挑战第31天】从零开始,踏上使用Uno Platform开发跨平台应用的旅程。只需编写一次代码,即可轻松部署到Windows、iOS、macOS、Android及Web(通过WASM)等多个平台。Uno Platform为.NET生态带来前所未有的灵活性和效率,简化跨平台开发。首先确保安装了Visual Studio或VS Code及.NET SDK,然后选择合适的项目模板创建新项目。项目结构类似传统.NET MAUI或WPF项目,包含核心NuGet包。通过简单的按钮示例,你可以快速上手并构建应用。Uno Platform让你的技术探索之旅充满无限可能。
40 0
|
2月前
|
网络安全 API 数据安全/隐私保护
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Windows)
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Windows)
|
2月前
|
Python Windows 内存技术
【Azure 应用服务】Azure App Service (Windows) 使用Flask框架部署Python应用,如何在代码中访问静态文件呢?如何设置文件路径?是相对路径还是绝对路径呢?
【Azure 应用服务】Azure App Service (Windows) 使用Flask框架部署Python应用,如何在代码中访问静态文件呢?如何设置文件路径?是相对路径还是绝对路径呢?