LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 894|回复: 2

最菜的XLib程序

[复制链接]
发表于 2006-10-10 15:00:13 | 显示全部楼层 |阅读模式
[php]
// #!/usr/bin/tcc -run -L/usr/X11R6/lib -lX11

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

int main()
{
        Display *display;
        XEvent event;

        display=XOpenDisplay(":0");

        if (display==NULL) {
                printf("Cannot connect to X server %s\n :0");
                exit(-1);
        }
       
        int screen_num;
        int screen_width;
        int screen_height;
       
        Window root_win;
        unsigned long white_pixel;
        unsigned long black_pixel;
        screen_num = DefaultScreen(display);
        screen_width=DisplayWidth(display,screen_num);
        screen_height=DisplayHeight(display,screen_num);

        printf("%d,%d\n",screen_width,screen_height);

        root_win=RootWindow(display,screen_num);

        white_pixel=WhitePixel(display,screen_num);
        black_pixel=BlackPixel(display,screen_num);

        Window win;
        int win_width,win_height,win_x,win_y;
        win_width=690;
        win_height=458;
        win_x=win_y=0;
        win=XCreateSimpleWindow(display,
                                root_win,
                                win_x,win_y,
                                win_width,win_height,
                                9,
                                black_pixel,white_pixel);
       
        XMapWindow(display,win);
       
        //XSync(display, False);
       
        XSelectInput(display, win, ExposureMask | KeyPressMask | ButtonPressMask | StructureNotifyMask);
        XFlush(display);
        sleep(1);
        XMoveWindow(display,win,75,125);
        XRaiseWindow(display,win);
        XFlush(display);
        while (1)
        {
                static i;
                i++;
                XNextEvent(display,&event);
                printf("new coming event!\n");
                switch (event.type) {
                        case Expose:
                                printf("%d times\tExpose iD -> %d\n",i,event.type);
                                break;
                        case ConfigureNotify:
                                printf("%d times\tConfigureNotify iD -> %d\n",i,event.type);
                                break;
                        case ButtonPress:
                                printf("%d times\t x,y -> %d,%d\n",i,event.xbutton.x,event.xbutton.y);
                                break;
                        case KeyPress:
                                printf("%d times\tKeyPress iD -> %d\n",i,event.type);
                                XCloseDisplay(display);
                                exit(1);
                }
        }
}
[/php]

编译:gcc hello.c -o helloX -lX11
如果有tcc的话把行首解注释,可以直接运行。
发表于 2006-10-11 16:49:38 | 显示全部楼层
头痛,,还不如用 wxWidgets
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-10-12 09:05:18 | 显示全部楼层
哈哈,写GUI当然不用了,我学习PyQt4中。

不过XLib真的很强! 迷航。 要写一个WM,实现特殊功能。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表