Xlib

Xlib

维基百科,自由的百科全书

Xlib 是一种 X Window System 协定的用户端,以C语言撰写。其功能是与 X server 沟通. 这样的功能可以让程式人员撰写程式时, 毋须了解其协定的细节。但甚少应用程式会直接使用 Xlib; 通常是透过其他的函式库来呼叫 Xlib 用以提供部件工具箱(widget toolkits):

Xlib及其应用

Xlib 发表于 1985年,目前使用在许多的 Unix-like操作系统上。

目前XCB有可能取代 Xlib.

目录

   [隐藏

[编辑]资料型别

Xlib主要的资料型别是Display[1]结构。

[编辑]范例

下面是一个XLib的范列, 产生一个视窗。

/*
  Simple Xlib application drawing a box in a window.
  gcc input.c -o output -lX11
*/
 
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
 
int main(void)
{
  Display *d;
  Window w;
  XEvent e;
  char *msg = "Hello, World!";
  int s;
 
  bool done = false;
 
  /* open connection with the server */
  d = XOpenDisplay(NULL);
  if (d == NULL) {
    fprintf(stderr, "Cannot open display\n");
    exit(1);
  }
 
  s = DefaultScreen(d);
 
  /* create window */
  w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 640, 480, 0,
                          BlackPixel(d, s), WhitePixel(d, s));
 
  /* register interest in the delete window message */
  Atom wmDeleteMessage = XInternAtom(d, "WM_DELETE_WINDOW", False);
  XSetWMProtocols(d, w, &wmDeleteMessage, 1);
 
  /* select kind of events we are interested in */
  XSelectInput(d, w, ExposureMask | KeyPressMask | StructureNotifyMask);
 
  /* map (show) the window */
  XMapWindow(d, w);
 
  /* event loop */
  while (!done) {
    XNextEvent(d, &e);
    /* draw or redraw the window */
    if (e.type == Expose) {
      XFillRectangle(d, w, DefaultGC(d, s), 20, 20, 10, 10);
      XDrawString(d, w, DefaultGC(d, s), 50, 50, msg, strlen(msg));
    }
 
    /* exit on key press */
    switch(e.type){
 
    case KeyPress:
      XDestroyWindow(d, w);
      break;
 
    case DestroyNotify:
      done = true;
      break;
 
    case ClientMessage:
      if (e.xclient.data.l[0] == wmDeleteMessage){
        done = true;
      }
      break;      
    }
  }
 
  /* close connection to server */
  XCloseDisplay(d);
 
  return 0;
}

[编辑]注释

  1. ^ Display Structure on freedesktop CVS. Tip search for: typedef struct _XDisplay Display.

[编辑]外部链接

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值