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);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章