X Window研究笔记(22)

<iframe align="center" marginwidth="0" marginheight="0" src="http://www.zealware.com/csdnblog336280.html" frameborder="0" width="336" scrolling="no" height="280"></iframe>
X Window研究笔记(22)

转载时请注明出处和作者联系方式
作者联系方式:李先静 <xianjimli at hotmail dot com><br><br>22.X Window 简单示例<br><br>对大多数linux程序员来说,很少有机会直接用Xlib去开发应用程序,那样开发效率太低,一般都是使用基于像GTK+和QT这样的toolkit。不过了解一下</xianjimli>XWindow编程没有什么坏处,这里举一个简单的例子,供新手参考:

xdemo.c

#include string.h>
#include
X11/Xlib.h>
#include
X11/keysym.h>

intmain(intargc,char*argv[])
...{
Display
*display=XOpenDisplay(NULL);
intscreen=DefaultScreen(display);
intwidth=DisplayWidth(display,screen)/2;
intheight=DisplayHeight(display,screen)/2;

Windowwin
=XCreateSimpleWindow(display,RootWindow(display,screen),
0,0,width,height,3,BlackPixel(display,screen),WhitePixel(display,screen));

XSelectInput(display,win,ExposureMask
|KeyPressMask|ButtonPressMask|StructureNotifyMask);

GCgc
=XCreateGC(display,win,0,NULL);

XMapWindow(display,win);

while(1)
...{
XEvent
event=...{0};
XNextEvent(display,
&event);
switch(event.type)
...{
caseConfigureNotify:
...{
width
=event.xconfigure.width;
height
=event.xconfigure.height;
break;
}

caseExpose:
...{
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;
}

caseKeyPress:
...{
if(event.xkey.keycode==XKeysymToKeycode(display,XK_Escape))
...{
XFreeGC(display,gc);
XCloseDisplay(display);
return0;
}

}

default:break;
}

}


return0;
}



Makefile
all :
gcc
- g - lX11xdemo . c - oxdemo
clean
:
rm
- fxdemo

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

~~end~~


你可能感兴趣的:(window)