x11获取新建窗口的window ID 并操作 相关函数

#include <gtk/gtk.h>  
#include <X11/Xlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <gdk/gdkx.h>
#include <X11/extensions/shape.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <X11/Xutil.h>

#include <X11/Xatom.h>
void get_window_nameAndid()
{
    	Display *display;
	Window rootwin;
	display = XOpenDisplay( NULL );
	rootwin = DefaultRootWindow( display );
	XSelectInput( display, rootwin, SubstructureNotifyMask);/*事件可以参考x.h*/
	XEvent event;
	while ( 1 ) 
	{
		XNextEvent(display, &event );
		if ( event.type == ReparentNotify )
		{
			Window x11window;
                        Display *x11display;
			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);	
			XFetchName(x11display, x11window, srname);
			x11display = XOpenDisplay( NULL );/*!!!注意这里以后如果你想对获取到的window ID进行操作必须重新链接*/
			
			if(*srname != NULL)
				printf("%s\n" ,*srname);	

			free(*srname);
		}		
        }	
}
upian: gtk3.c
    gcc gtk3.c -g  -o  tupian -L/opt/X11/lib -lX11 -lXext `pkg-config --cflags --libs gtk+-3.0` -pthread
int main()
{

get_window_nameAndid();
return 0;
}

再贴上我的Makefile

upian: gtk3.c
    gcc gtk3.c -g  -o  tupian -L/opt/X11/lib -lX11 -lXext `pkg-config --cflags --libs gtk+-3.0` -pthread

下面我们有了window ID能干什么呢?

1.还能监听事件

                比如说改变窗口大小,XSelectInput( x11display, x11window, SubstructureNotifyMask);

                 XNextEvent(x11display, &x11event );

                 if ( x11event.type ==  PropertyNotify)

               这里要说一下

typedef struct {
	int type;
	unsigned long serial;	/* # of last request processed by server */
	Bool send_event;	/* true if this came from a SendEvent request */
	Display *display;	/* Display the event was read from */
	Window event;
	Window window;
	int x, y;
	int width, height;
	int border_width;
	Window above;
	Bool override_redirect;
} XConfigureEvent;

                  如果大小发生改变,你会得到这个结构体的数据,这个结构体里的x,y。是相对于父窗口的,下面就是第二点

2.获取窗口大小和相对于屏幕的坐标。

			int screen_x = 0, screen_y = 0;
			Window child_win ;
			Window parent_win;
			XWindowAttributes win_attr;
			
			Window root_win;
			Window* child_windows;
			int num_child_windows = 0;
			XQueryTree(x11display, x11window, &root_win, &parent_win, &child_windows, &num_child_windows);
			XFree(child_windows);
			XTranslateCoordinates(x11display, parent_win, root_win, win_attr.x, win_attr.y, &screen_x, &screen_y, &child_win);
			XGetWindowAttributes(x11display, x11window, &win_attr);

                        /*现在screen_x, screen_y就是相对于屏幕坐标,win_attr.width,win_attr.height就是大小了*/

3.移动窗口,调整窗口显示置顶

int XRaiseWindow(
    Display*        /* display */,
    Window        /* w */
);

int XMoveResizeWindow(
    Display*        /* display */,
    Window        /* w */,
    int            /* x */,
    int            /* y */,
    unsigned int    /* width */,
    unsigned int    /* height */
);

4.设置穿透效果,能穿透点击

       XShapeCombineRectangles( x11srcdisplay, srcwindow, ShapeInput,0,0, NULL, 0, ShapeSet, YXBanded);

5.设置半透明

	XVisualInfo vinfo;
	XMatchVisualInfo(x11srcdisplay, DefaultScreen(x11srcdisplay), 32, TrueColor, &vinfo);
	XSetWindowAttributes attr;
	attr.colormap = XCreateColormap(x11srcdisplay,  parent_window, vinfo.visual, AllocNone);
	attr.border_pixel = 0;
	attr.background_pixel =0x80808080; 
	attr.override_redirect = True;/*边框,不在任务栏显示*/
	unsigned long winmask;
	    
	attr.override_redirect = True;//True
	winmask = CWColormap | CWBorderPixel | CWBackPixel; 
	XChangeWindowAttributes(x11srcdisplay, srcwindow, winmask, &attr);

猜你喜欢

转载自blog.csdn.net/lyw13522476337/article/details/80851769