用Xlib库进行基本图形编程(3)

陈着 posted @ Nov 06, 2009 09:53:52 AM in GTK+ , 5020 阅读
5.编译基于Xlib的程序
编译基于Xlib的需要把他们和Xlib库进行链接。这是通过使用如下的编译命令行来完成的:
cc prog.c -o prog -lX11
如果编译器抱怨它找不到X11库,尝试加上‘-L’标志,像这样:
cc prog.c -o prog -L/usr/X11/lib -lX11
或者也许是这样(对于用X11的release 6的系统):
cc prog.c -o prog -L/usr/X11R6/lib -lX11
在SunOs 4系统上,X库被放置于/usr/openwin/lib:
cc prog.c -o prog -L/usr/openwin/lib -lX11
6.打开和关闭连接到X服务器的连接
X程序首先需要打开连接到X服务器的连接。在我们完成这件工作的时候,我们需要指定运行
X服务器的机器的地址,以及display号码。X window系统能够支持全部连接于同一个机器的
好几个display。然而,通常只有一个这样的display,它的display号是‘0’。如果我们想
要连接到本地display(也就是我们客户程序所运行的机器的display),我们可以指定displa
y为’:0‘。要连接到地址为”simey“的机器的第一个display,我们能够使用地址”simey
:0“。这儿是连接是如何被打开的:
#include <X11/Xlib.h>   /* defines common Xlib functions and structs. */
.
.
/* this variable will contain the pointer to the Display structure */
/* returned when opening a connection.                             */
Display* display;

/* open the connection to the display "simey:0". */
display = XOpenDisplay("simey:0");
if (display == NULL) {
    fprintf(stderr, "Cannot connect to X server %s\n", "simey:0");
    exit (-1);
}
注意,对于X程序来说检查系统变量‘DISPLAY’是否被定义了是很常见的,而且如果是的话
,使用它的内容作为XOpenDisplay()函数的参数。
当程序完成了它的使命并且需要关闭连接到X服务器的连接的时候,它如下动作:
XCloseDisplay(display);
这将导致所有由程序创造的窗口(如果还有剩下的话)自动被服务器关闭,而且为了客户的利
益任何留在服务器上的资源-将被释放。注意这将不会导致我们的客户程序终止-我们使用
普通的exit()函数来完成。
7.检查关于Display的基本信息
一旦我们打开了一个连接到X服务器的连接,我们应当检查有关它的一些基本信息:他有什
么样的屏幕,尺寸是多少(宽和高),它支持多少颜色(黑白?灰度?256色?更多?),以及
等等。我们将展示一些作一些这样检查的代码片段,以及在使用中解释每个函数的注释。我
们假定‘display’是一个指向‘Display’的结构的指针,由前面对XOpenDisplay()的调用
返回的。
/* this variable will be used to store the "default" screen of the  */
/* X server. usually an X server has only one screen, so we're only */
/* interested in that screen.                                       */
int screen_num;

/* these variables will store the size of the screen, in pixels.    */
int screen_width;
int screen_height;

/* this variable will be used to store the ID of the root window of our */
/* screen. Each screen always has a root window that covers the whole   */
/* screen, and always exists.                                           */
Window root_window;

/* these variables will be used to store the IDs of the black and white */
/* colors of the given screen. More on this will be explained later.    */
unsigned long white_pixel;
unsigned long black_pixel;

/* check the number of the default screen for our X server. */
screen_num = DefaultScreen(display);

/* find the width of the default screen of our X server, in pixels. */
screen_width = DisplayWidth(display, screen_num);

/* find the height of the default screen of our X server, in pixels. */
screen_height = DisplayHeight(display, screen_num);

/* find the ID of the root window of the screen. */
root_window = RootWindow(display, screen_num);

/* find the value of a white pixel on this screen. */
white_pixel = WhitePixel(display, screen_num);

/* find the value of a black pixel on this screen. */
black_pixel = BlackPixel(display, screen_num);
 
有各种其他的宏来得到关于屏幕的更多信息,你可以从任何Xlib参考书中得到它们。还有和
这些宏完成相同功能的函数(例如XWhitePixel,它和WhitePixel干一样的事情)。
Avatar_small
celebrity heights 说:
Jan 25, 2022 12:03:29 AM

If you are interested in your favorite actors' heights and other detailed information, please take a look at <a href="https://www.celebheightwiki.com/" title="celeb height wiki">celeb height wiki</a> database.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter