xlib查找window并且置顶代码

/*
	xraise 0.2 - A small tool to send a X application
                     to the foreground
	Copyright 2010 - socol
	Copyright 2007 - Hd Luc
*/
/**
 * gcc -g -o xraise xraise.c `pkg-config gtk+-2.0 --cflags --libs gthread-2.0 x11`
 */



#include <X11/Xos.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

/* "borrowed" from xwininfo/dsimple.c */

Window Window_With_Name(dpy, top, name)
     Display *dpy;
     Window top;
     char *name;
{
	Window *children, dummy;
	unsigned int nchildren;
	int i;
	Window w=0;
	char *window_name;
        XClassHint *class_hint;
	class_hint = XAllocClassHint();
	int ret = 0;
#if 0
	// find by WM_NAME(STRING) = "window - tilte"
	if (XFetchName(dpy, top, &window_name) && (strstr(window_name, name) == window_name))
	  return(top);
#else
	// find by WM_CLASS(STRING) = "mywindow", "MyWindow"
	if (XGetClassHint(dpy, top, class_hint)) {
		if (strstr(class_hint->res_name, name) == class_hint->res_name) {
			ret = 1;
		}
		if (!class_hint->res_class) {
			XFree(class_hint->res_class);
			XFree(class_hint->res_name);
		}
		if (ret)
			return(top);
	}
#endif
	
	if (!XQueryTree(dpy, top, &dummy, &dummy, &children, &nchildren))
	  return(0);

	for (i=0; i<nchildren; i++)
       	{
		w = Window_With_Name(dpy, children[i], name);
		if (w)
		  break;
	}
	if (children) XFree ((char *)children);
	  return(w);
}

int main(int argc, char **argv)
{
	Window window;
	Display *dpy;
	
	if (argc < 2) 
	{
		fprintf(stderr, "Usage: %s window-name\n", argv[0]);
		exit(1);
	}
	
	dpy = XOpenDisplay(":0");
	if (!dpy) 
	{
		fprintf(stderr, "Cannot open display.\n");
		exit(1);
	}
	
	window = Window_With_Name(dpy, DefaultRootWindow(dpy), argv[1]);
	if (!window) 
	{
		fprintf(stderr, "Cannot find a window by that name.\n");
		exit(1);
	}
	
	XSetInputFocus(dpy, window, RevertToPointerRoot, CurrentTime);

	if(!XRaiseWindow(dpy, window))
	{
		fprintf(stderr, "XRaiseWindow error?\n");
		exit(1);
	}
	XCloseDisplay(dpy);
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Xlib库的 Python 示例代码: ```python from Xlib import X, display, Xutil from Xlib.ext import randr # 获取显示器 d = display.Display() # 获取屏幕 screen = d.screen() # 获取根窗口 root = screen.root # 获取屏幕尺寸 width = root.get_geometry().width height = root.get_geometry().height # 创建窗口 win = root.create_window( 0, 0, width // 2, height // 2, # 窗口坐标和大小 0, # 边框大小 screen.root_depth, X.InputOutput, X.CopyFromParent, background_pixel=screen.white_pixel, event_mask=X.ExposureMask | X.StructureNotifyMask) # 显示窗口 win.map() # 进入事件循环 while True: event = d.next_event() if event.type == X.Expose: # 窗口重绘 win.fill_rectangle(screen.default_gc, 0, 0, width // 2, height // 2) elif event.type == X.ConfigureNotify: # 窗口大小变化 win.fill_rectangle(screen.default_gc, 0, 0, event.width, event.height) elif event.type == X.KeyPress: # 键盘事件 if event.detail == X.Keycode("Q"): break # 销毁窗口 win.destroy() # 断开连接 d.disconnect() ``` 以下是使用PyQt库的 Python 示例代码: ```python from PyQt5.QtWidgets import QApplication, QWidget from PyQt5.QtGui import QPainter, QPen from PyQt5.QtCore import Qt # 创建应用程序 app = QApplication([]) # 创建窗口 win = QWidget() win.setGeometry(100, 100, 400, 400) # 重写绘制函数 def paintEvent(event): painter = QPainter(win) painter.setPen(QPen(Qt.red, 5)) painter.drawLine(0, 0, 400, 400) painter.drawLine(0, 400, 400, 0) # 显示窗口 win.show() # 进入事件循环 app.exec_() ``` 这两个示例代码分别使用了 Xlib 和 PyQT 库来实现窗口创建、绘制和事件处理等功能。其中 Xlib 库提供了对 X Window 系统的底层访问,而 PyQT 库则提供了更高层次的 GUI 组件和事件处理功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值