Chinaunix首页 | 论坛 | 博客
  • 博客访问: 192110
  • 博文数量: 111
  • 博客积分: 3010
  • 博客等级: 中校
  • 技术积分: 1240
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-07 07:46
文章分类

全部博文(111)

文章存档

2015年(2)

2014年(1)

2011年(1)

2010年(7)

2009年(100)

我的朋友

分类: LINUX

2009-08-07 09:43:43


转载时请注明出处和作者联系方式
文章出处:http://www.limodev.cn/blog
作者联系方式:李先静

22.X Window 简单示例

对大多数linux程序员来说,很少有机会直接用Xlib去开发应用程序,那样开发效率太低,一般都是使用基于像GTK+和QT这样的toolkit。不过了解一下XWindow编程没有什么坏处,这里举一个简单的例子,供新手参考:

xdemo.c

#include 
#include
#include
 
int main(int argc, char* argv[])
{
Display* display = XOpenDisplay(NULL);
int screen = DefaultScreen(display);
int width = DisplayWidth(display, screen)/2;
int height = DisplayHeight(display, screen)/2;
 
Window win = XCreateSimpleWindow(display, RootWindow(display, screen),
0, 0, width, height, 3, BlackPixel(display, screen), WhitePixel(display, screen));
 
XSelectInput(display, win, ExposureMask|KeyPressMask | ButtonPressMask | StructureNotifyMask);
 
GC gc = XCreateGC(display, win, 0, NULL);
 
XMapWindow(display, win);
 
while(1)
{
XEvent event = {0};
XNextEvent(display, &event);
switch(event.type)
{
case ConfigureNotify:
{
width = event.xconfigure.width;
height = event.xconfigure.height;
break;
}
case Expose:
{
XSetForeground(display, gc, WhitePixel(display, screen));
XFillRectangle(display, win, gc, 0, 0, width, height);
XSetForeground(display, gc, BlackPixel(display, screen));
XDrawString(display, win, gc, width/2, height/2, "XWindow", 7);
break;
}
case KeyPress:
{
if(event.xkey.keycode == XKeysymToKeycode(display, XK_Escape))
{
XFreeGC(display, gc);
XCloseDisplay(display);
return 0;
}
}
default:break;
}
}
 
return 0;
}
 
Makefile
all:
gcc -g -lX11 xdemo.c -o xdemo
clean:
rm -f xdemo

(我并不擅长X Window编程,请不要问我关于X Window编程的问题。)

阅读(275) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:X Window研究笔记(21)-字符串与Atom

给主人留下些什么吧!~~