日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

用Xlib库进行基本图形编程

發布時間:2025/4/16 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用Xlib库进行基本图形编程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

用Xlib庫進行基本圖形編程

用Xlib庫進行基本圖形編程
目錄
1、前言
2、X Window系統的客戶服務器模式
3、GUI編程-同步化的編程模型
4、基本的Xlib概念
? ? ? ? 1、X Display
? ? ? ? 2、GC-圖形上下文
? ? ? ? 3、對象句柄
? ? ? ? 4、Xlib 結構體的內存分配
? ? ? ? 5、事件
5、編譯給予Xlib的程序
6、打開和關閉一個連接到X服務器的連接
7、檢查關于Display的基本信息
8、創建一個簡單的窗口-我們的“hello world”程序
9、在窗口中繪畫
? ? ? ? 1、分配圖形上下文(GC)
? ? ? ? 2、基本繪圖-點,線,框,圓...
10、X事件
? ? ? ? 1、使用事件遮罩給事件型別注冊
? ? ? ? 2、接收事件-撰寫事件循環
? ? ? ? 3、暴露事件
? ? ? ? 4、獲得用戶輸入
? ? ? ? ? ? ? ? 1、鼠標按鈕點擊和釋放事件
? ? ? ? ? ? ? ? 2、鼠標移動事件
? ? ? ? ? ? ? ? 3、鼠標指針進入和離開事件
? ? ? ? ? ? ? ? 4、鍵盤焦點
? ? ? ? ? ? ? ? 5、鍵盤按下和釋放事件
? ? ? ? 5、X事件-完整的例子
11、處理文本和字體
? ? ? ? 1、字體結構
? ? ? ? 2、載入字體
? ? ? ? 3、把字體賦給圖形上下文
? ? ? ? 4、在窗口中繪出文本
12、窗口階層
? ? ? ? 1、根,父和子窗口
? ? ? ? 2、事件傳播
13、和窗口管理器交互
? ? ? ? 1、窗口屬性
? ? ? ? 2、設置窗口名和圖標名
? ? ? ? 3、設置最佳窗口尺寸
? ? ? ? 4、設置窗口管理器的雜項
? ? ? ? 5、設置應用程序的圖標
14、簡單窗口操作
? ? ? ? 1、映射和取消映射窗口
? ? ? ? 2、在屏幕上移動窗口
? ? ? ? 3、縮放窗口
? ? ? ? 4、改變窗口的堆疊次序-提高會放低
? ? ? ? 5、標識會取消標識窗口
? ? ? ? 6、獲得窗口的信息
15、使用顏色來繪出彩虹
? ? ? ? 1、色表
? ? ? ? 2、分配和釋放色表
? ? ? ? 3、分配和釋放單個顏色
? ? ? ? 4、用顏色繪畫
16、X Bitmap和Pixmap
? ? ? ? 1、什么是X Bitmap?什么又是X Pixmap?
? ? ? ? 2、從文件中載入Bitmap
? ? ? ? 3、在窗口中畫出Bitmap
? ? ? ? 4、創建Pixmap
? ? ? ? 5、在窗口中畫出Pixmap
? ? ? ? 6、釋放Pixmap
17、鼠標光標的雜事
? ? ? ? 1、創建和釋放鼠標光標
? ? ? ? 2、設置窗口的鼠標光標
Preface
前言

This tutorial is the first in a series of "would-be" tutorials about graphical programming?

in the X window environment. By itself, it is useless. A real X programmer usually uses a?

much higher level of abstraction, such as using Motif (or its free version, lesstiff), GTK,?

QT and similar libraries. However, we need to start somewhere. More than this, knowing how?

things work down below is never a bad idea.
該教程是“可能會有”的關于在X Window環境下進行圖形化編程的教程的第一個系列。其自身是用處不大

的。一個真正的X程序員一般使用一個更高層次的抽象,比如用Motif(或者它的的免費版本,lesstiff),

GTK,QT和類似的庫。然而,我們需要從一個地方開始入手。不僅如此,知道表象之下的事情是如何工作

的決不會是壞的主意。

After reading this tutorial, one would be able to write very simple graphical programs, but?

not programs with a descent user interface. For such programs, one of the previously?

mentioned libraries would be used.?
在讀完本教程后,你可能能夠些簡單的圖形程序,但是那不會是一個有良好用戶界面的程序。對于這樣的

程序,也許就要用到上述的庫中的某一個。

The Client And Server Model Of The X Window System
X Window系統的客戶和服務器模型

The X window system was developed with one major goal - flexibility. The idea was that the?

way things look is one thing, but the way things work is another matter. Thus, the lower?

levels provide the tools required to draw windows, handle user input, allow drawing graphics?

using colors (or black and white screens), etc. To this point, a decision was made to?

separate the system into two parts. A client that decides what to do, and a server that?

actually draws on the screen and reads user input in order to send it to the client for?

processing.
X window系統開發之初有一個最大的目標-靈活性。想法是這樣的東西看上如如何一回事,東西如何工作

的又是另外一回事。因而,底層提供在畫窗口,處理用戶輸入,允許使用顏色畫圖形(或者黑白屏幕)等動

作中需要的工作。就這點決定了把系統分為兩個部分。客戶決定作什么,而服務器實際在屏幕上畫圖并讀

出用戶輸入以發給客戶進行處理。

This model is the complete opposite of what one is used to when dealing with clients and?

servers. In our case, the user seats near the machine controlled by the server, while the?

client might be running on a remote machine. The server controls the screen, mouse and?

keyboard. A client may connect to the server, request that it draws a window (or several?

windows), and ask the server to send it any input the user sends to these windows. Thus,?

several clients may connect to a single X server - one might be running an email software,?

one running a WWW browser, etc. When input it sent by the user to some window, the server?

sends a message to the client controlling this window for processing. The client decides?

what to do with this input, and sends the server requests for drawing in the window.
這個模型正好和人們在客戶和服務器中所習慣的行為相反。在我們的例子,用戶坐在由服務器控制的機器

旁邊,而客戶可能運行于一個遠程的機器上。服務器控制屏幕,鼠標和鍵盤。客戶可能連接到了服務器,

發出畫一個(或者多個)窗口的請求,并要求服務器把任何用戶發送給這些窗口的輸入給他。因而,幾個客

戶可能連接到了同一個X服務器-一個可能在運行email軟件,一個可能在運行WWW瀏覽器,等等。當由用

戶發送輸入給某些窗口時,服務器向控制這些窗口的客戶發送消息以供處理。客戶決定對輸入作什么,并

給服務器發送請求來在窗口中繪圖。

The whole session is carried out using the X message protocol. This protocol was originally?

carried over the TCP/IP protocol suite, allowing the client to run on any machine connected?

to the same network that the server is. Later on the X servers were extended to allow?

clients running on the local machine more optimized access to the server (note that an X?

protocol message may be several hundreds of KB in size), such as using shared memory, or?

using Unix domain sockets (a method for creating a logical channel on a Unix system between?

two processes).?
整個會話過程是用X消息協議執行的。該協議最初時由TCP/IP協議包執行的,允許客戶運行于任何和服務

器連接在相同網絡上的機器上。后來,X服務器被擴展為允許客戶運行在本地機器上更優的訪問服務器(注

意到X協議消息可能有幾百KB那么大),比如使用共享內存,或者使用Unix域sockets(一個在Unix系統上的

兩個進程間創建邏輯通道的方法)。

GUI programming - the Asynchronous Programming Model
GUI編程-同步化的編程模型

Unlike conventional computer programs, that carry some serial nature, a GUI program usually?

uses an asynchronous programming model, also known as "event-driven programming". This means?

that that program mostly sits idle, waiting for events sent by the X server, and then acts?

upon these events. An event may say "The user pressed the 1st button mouse in spot x,y", or?

"the window you control needs to be redrawn". In order for the program to be responsive to?

the user input, as well as to refresh requests, it needs to handle each event in a rather?

short period of time (e.g. less than 200 milliseconds, as a rule of thumb).
不同于包含某種順序化執行內質的傳統的計算機程序。GUI程序通常使用同步化的編程模型,也被稱為“

事件驅動編程”。這個意味著程序大部分時候時閑著的,等待由X服務器發送的事件,然后根據這些事件

作出反應。事件可能時”用戶在點x,y處按下第一個按鈕“,或者時”你控制的串口需要重畫“。為了程

序能夠響應用戶輸入以及刷新請求,它需要在一個相當短的時間內處理每個事件(比如作為一個大體的規

則,小于200毫秒)。

This also implies that the program may not perform operations that might take a long time?

while handling an event (such as opening a network connection to some remote server, or?

connecting to a database server, or even performing a long file copy operation). Instead, it?

needs to perform all these operations in an asynchronous manner. This may be done by using?

various asynchronous models to perform the longish operations, or by performing them in a?

different process or thread.
這也意味著程序可能不執行在處理過程也許需要很長時間的事件的操作(例如打開一個連接到遠程服務器

的網絡連接,或者連接到數據庫服務器,甚至執行一個大文件的拷貝)。而是,它需要同步化的執行所有

這些操作。這可能通過使用各種同步模型來執行長時間的操作,或者通過用不同的進程或者線程來執行他

們。

So the way a GUI program looks is something like that:
因而GUI程序看上去大概是這樣:

? ?1. Perform initialization routines.
? ?2. Connect to the X server.
? ?3. Perform X-related initialization.
? ?4. While not finished:
? ?? ?? ?1. Receive the next event from the X server.
? ?? ?? ?2. handle the event, possibly sending various drawing requests to the X server.
? ?? ?? ?3. If the event was a quit message, exit the loop.?
? ?5. Close down the connection to the X server.
? ?6. Perform cleanup operations.?
? ?1、執行初始化
? ?2、連接到X服務器
? ?3、執行X相關的初始化工作
? ?4、在沒有結束之前:
? ?? ???1、接收來自于X服務器的下一個事件
? ?? ???2、處理事件,也許向X服務器發送多種繪畫請求
? ?? ???3、如果事件是退出消息,跳出循環
? ?5、關閉連接到X服務器的連接
? ?6、執行清理操作

Basic Xlib Notions
基本的Xlib概念

In order to eliminate the needs of programs to actually implement the X protocol layer, a?

library called 'Xlib' was created. This library gives a program a very low-level access to?

any X server. Since the protocol is standardized, A client using any implementation of Xlib?

may talk with any X server. This might look trivial these days, but back at the days of?

using character mode terminals and proprietary methods of drawing graphics on screens, this?

looked like a major break-through. In fact, you'll notice the big hype going around?

thin-clients, windows terminal servers, etc. They are implementing today what the X protocol?

enabled in the late 80's. On the other hand, the X universe is playing a catch-up game?

regarding CUA (common user access, a notion made by IBM to refer to the usage of a common?

look and feel for all programs in order to ease the lives of the users). Not having a common?

look and feel was a philosophy of the creators of the X window system. Obviously, it had?

some drawbacks that are evident today.?
為了消除程序事件實現X協議層的需求,一個稱為‘Xlib’的庫被創造出來。該庫給程序提供了一個對任

何X服務器非常底層的訪問。因為協議是標準化的,客戶使用Xlib的任何一種實現都可以和和X服務器通話

。這些可能在今天看來沒什么大用,但回到使用字符模式終端和專有方法在屏幕上繪圖的日子,這是一個

很大的突破。事實上,你將注意到咱愛瘦客戶,窗口終端服務器,等等周圍進行的各種虛偽的騙局。他們

在今天實現X協議在80年代晚期已經能夠作的事情。另外一方面,X universe在玩一個關于CUA(共通用戶

感觀,一個由IBM制造的概念,指的是對所有程序使用共通的觀感以使得用戶能夠更加輕松)的catch-up游

戲。沒有共通的感觀是X window系統創造者的哲學。明顯,它有許多在今天看來顯然的缺陷。

The X Display

The major notion of using Xlib is the X display. This is a structure representing the?

connection we have open with a given X server. It hides a queue of messages coming from the?

server, and a queue of pending requests that our client intends to send to the server. In?

Xlib, this structure is named 'Display'. When we open a connection to an X server, the?

library returns a pointer to such a structure. Later, we supply this pointer to any Xlib?

function that should send messages to the X server or receive messages from this server.?

X Display

使用Xlib的最大的概念是X display。這是一個代表我們和一個給定X服務器打開的連接的結構體。它隱藏

了服務器的消息隊列,客戶將要發送給服務器的請求隊列。在Xlib中,這個結構體被命名為‘Display’

。當我們打開一個連接到X服務器的連接的時候,庫返回一個指向這種結構體的指針。然后,我們把這個

指針提供給任何需要發送消息給X服務器或者從這個服務器接收消息的Xlib函數。

The GC - Graphics Context

When we perform various drawing operations (graphics, text, etc), we may specify various?

options for controlling how the data will be drawn - what foreground and background colors?

to use, how line edges will be connected, what font to use when drawing some text, etc). In?

order to avoid the need to supply zillions of parameters to each drawing function, a?

graphical context structure, of type 'GC' is used. We set the various drawing options in?

this structure, and then pass a pointer to this structure to any drawing routines. This is?

rather handy, as we often needs to perform several drawing requests with the same options.?

Thus, we would initialize a graphical context, set the desired options, and pass this GC?

structure to all drawing functions.?

GC -圖形上下文

當我們執行各種繪出(圖形,文本,等)操作的時候,我們可能要指定幾個選項以控制數據怎么被繪出 -?

前景色和背景色是什么,線的邊緣如何連接,在繪出文本的時候使用何種字體,等。為了避免給每個繪出

函數提供n多參數,一個類型為‘GC’的圖形上下文結構被啟用。我們在這個結構中設置各種繪出選項,

并且把指向這個結構的指針傳遞給每個繪出函數。這個是相當方便的,因為我們通常需要用相同選項執行

好幾個繪出請求。因而,我們初始化圖形上下文,設置所需的選項,并把這個GC結構傳遞給所有的繪出函

數。

Object Handles

When various objects are created for us by the X server - such as windows, drawing areas and?

cursors - the relevant function returns a handle. This is some identifier for the object?

that actually resides in the X server's memory - not in our application's memory. We can?

later manipulate this object by supplying this handle to various Xlib functions. The server?

keeps a mapping between these handles and the actual objects it manages. Xlib provides?

various type definitions for these objects (Window, Cursor, Colormap and so on), which are?

all eventually mapped to simple integers. We should still use these type names when defining?

variables that hold handles - for portability reasons.?

對象句柄

當各種對象被創造出來給X服務器使用 - 例如窗口,繪畫區域和光標 - 相關的函數返回一個句柄。這

是實際存在于X服務器的內存中的對象的標識符。我們能夠在后面通過把這些句柄提供給各種Xlib函數來

操縱這些對象。服務器保存了這些句柄和它們管理的對象之間的映射。Xlib提供各種型別定義給這些對象

(窗口,光標,色表等等),它們實際上最終映射為簡單的整數。我們在定義保存這些句柄的變量的時候仍

然應當使用這些型別名-為了有更好的可移植性。

Memory Allocation For Xlib Structures
為Xlib結構體分配內存

Various structure types are used in Xlib's interface. Some of them are allocated directly by?

the user. Others are allocated using specific Xlib functions. This allows the library to?

initialize properly these structures. This is very handy, since these structures tend to?

contain a lot of variables, making it rather tedious for the poor programmer to initialize.?

Remember - Xlib tries to be as flexible as possible, and this means it is also as complex as?

it can get. Having default values will enable a beginner X programmer to use the library,?

without interfering with the ability of a more experienced programmer to tweak with these?

zillions of options.
在Xlib的接口中使用了各種結構型別。他們中的一些直接由用戶分配內存。其他的使用Xlib函數分配。這

使得庫能夠恰當的初始化這些結構。這非常方便,因為這些結構傾向于包含很多變量,使得對于差勁點的

程序員非常難于初始化它們。記住-Xlib嘗試著盡可能的了靈活,而且這意味著他也是盡可能的復雜。由

缺省值使得初學X的程序員能夠使用這個庫,而不打擾有經驗的程序員在n多選項中作調整的可能。

As for freeing memory, this is done in one of two ways. In cases where we allocated the?

memory - we free it in the same manner (i.e. use free() to free memory allocated using?

malloc()). In case we used some Xlib function to allocate it, or we used some Xlib query?

method that returns dynamically allocated memory - we will use the XFree() function to free?

this memory block.
對于釋放內存,由兩種方法完成。在我們分配內存的情況-我們用相同方法釋放它們(也就是使用free()

來釋放由malloc()分配的內存)。在我們用某Xlib函數分配的時候,或者我們使用返回動態分配的內存的

Xlib查詢方法的時候-我們使用XFree()函數來釋放這些內存塊。

Events
事件

A structure of type 'XEvent' is used to pass events received from the X server. Xlib?

supports a large amount of event types. The XEvent structure contains the type of event?

received, as well as the data associated with the event (e.g. position on the screen where?

the event was generated, mouse button associated with the event, region of screen associated?

with a 'redraw' event, etc). The way to read the event's data depends on the event type.?

Thus, an XEvent structure contains a C language union of all possible event types (if you're?

not sure what C unions are, it is time to check your proffered C language manual...). Thus,?

we could have an XExpose event, an XButton event, an XMotion event, etc.?

型別‘XEvent’的結構被用來傳遞從X服務器接收來的事件。Xlib支持很大數量的事件型別。XEvent結構

包含接收事件的類型,以及與該事件相關的數據(例如事件產生的屏幕位置,與事件相關的鼠標按鈕,和

‘redraw’事件相關的屏幕區域,等)。讀取事件的數據的方法和事件類型有關。因而,XEvent結構包含

一個C語言對于所有可能事件型別的聯合(如果你不確知C的聯合是什么,該是查查你的C語言手冊的時候

...)。因而,我們能夠有一個XExpose事件,XButton事件,XMotion事件,等。

Compiling Xlib-Based Programs
編譯基于Xlib的程序

Compiling Xlib-Based programs requires linking them with the Xlib library. This is done?

using a compilation command like this:
編譯基于Xlib的需要把他們和Xlib庫進行鏈接。這是通過使用如下的編譯命令行來完成的:


cc prog.c -o prog -lX11

If the compiler complains that it cannot find the X11 library, try adding a '-L' flag, like?

this:
如果編譯器抱怨它找不到X11庫,嘗試加上‘-L’標志,像這樣:

cc prog.c -o prog -L/usr/X11/lib -lX11

or perhaps this (for a system with release 6 of X11):
或者也許是這樣(對于用X11的release 6的系統):

cc prog.c -o prog -L/usr/X11R6/lib -lX11

On SunOs 4 systems, the X libraries are placed in /usr/openwin/lib:
在SunOs 4系統上,X庫被放置于/usr/openwin/lib:

cc prog.c -o prog -L/usr/openwin/lib -lX11

and so on...
等等...

Opening And Closing The Connection To An X Server
打開和關閉連接到X服務器的連接

An X program first needs to open the connection to the X server. When we do that, we need to?

specify the address of the host running the X server, as well as the display number. The X?

window system can support several displays all connected to the same machine. However,?

usually there is only one such display, which is display number '0'. If we wanted to connect?

to the local display (i.e. the display of the machine on which our client program runs), we?

could specify the display as ":0". To connect to the first display of a machine whose?

address is "simey", we could use the address "simey:0". Here is how the connection is?

opened:
X程序首先需要打開連接到X服務器的連接。在我們完成這件工作的時候,我們需要指定運行X服務器的機

器的地址,以及display號碼。X window系統能夠支持全部連接于同一個機器的好幾個display。然而,通

常只有一個這樣的display,它的display號是‘0’。如果我們想要連接到本地display(也就是我們客戶

程序所運行的機器的display),我們可以指定display為’: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);
}

Note that is common for X programs to check if the environment variable 'DISPLAY' is?

defined, and if it is, use its contents as the parameter to the XOpenDisplay() function.
注意,對于X程序來說檢查系統變量‘DISPLAY’是否被定義了是很常見的,而且如果是的話,使用它的內

容作為XOpenDisplay()函數的參數。

When the program finished its business and needs to close the connection the X server, it?

does something like this:
當程序完成了它的使命并且需要關閉連接到X服務器的連接的時候,它如下動作:


XCloseDisplay(display);


This would cause all windows created by the program (if any are left) to be automatically?

closed by the server, and any resources stored on the server on behalf of the clients - to?

be freed. Note that this does not cause our client program to terminate - we could use the?

normal exit() function to do that.
這將導致所有由程序創造的窗口(如果還有剩下的話)自動被服務器關閉,而且為了客戶的利益任何留在服

務器上的資源-將被釋放。注意這將不會導致我們的客戶程序終止-我們使用普通的exit()函數來完成。

Checking Basic Information About A Display
檢查關于Display的基本信息

Once we opened a connection to an X server, we should check some basic information about it:?

what screens it has, what is the size (width and height) of the screen, how many colors it?

supports (black and white? grey scale? 256 colors? more?), and so on. We will show a code?

snippet that makes few of these checks, with comments explaining each function as it is?

being used. We assume that 'display' is a pointer to a 'Display' structure, as returned by a?

previous call to XOpenDisplay().

一旦我們打開了一個連接到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);

There are various other macros to get more information about the screen, that you can find?

in any Xlib reference. There are also function equivalents for some of these macros (e.g.?

XWhitePixel, which does the same as WhitePixel).
有各種其他的宏來得到關于屏幕的更多信息,你可以從任何Xlib參考書中得到它們。還有和這些宏完成相

同功能的函數(例如XWhitePixel,它和WhitePixel干一樣的事情)。

Creating A Basic Window - Our "hello world" Program
創建一個簡單窗口 - 我們的“hello world”程序

After we got some basic information about our screen, we can get to creating our first?

window. Xlib supplies several functions for creating new windows, one of which is?

XCreateSimpleWindow(). This function gets quite a few parameters determining the window's?

size, its position, and so on. Here is a complete list of these parameters:
在我們獲得了一些有關我們的屏幕的基本信息之后,我們可以開始創建我們第一個窗口。Xlib提供數個函

數來創建新窗口,其中的一個是XCreateSimpleWindow()。這個函數或者少量幾個決定窗口的大小和位置

等的參數。這有一個這些參數的完整列表:

Display* display
? ? Pointer to the Display structure.
Window parent
? ? The ID of an existing window that should be the parent of the new window.
int x
? ? X Position of the top-left corner of the window (given as number of pixels from the left?

of the screen).
int y
? ? Y Position of the top-left corner of the window (given as number of pixels from the top?

of the screen).
unsigned int width
? ? Width of the new window, in pixels.
unsigned int height
? ? Height of the new window, in pixels.
unsigned int border_width
? ? Width of the window's border, in pixels.
unsigned long border
? ? Color to be used to paint the window's border.
unsigned long background
? ? Color to be used to paint the window's background.

Lets create a simple window, whose width is 1/3 of the screen's width, height is 1/3 of the?

screen's height, background color is white, border color is black, and border width is 2?

pixels. The window will be placed at the top-left corner of the screen.
讓我們創建一個簡單窗口,它的寬是屏幕寬的1/3,高是屏幕高的1/3,背景色是白的,邊框顏色是黑色的

,而且邊框寬2象素。窗口將會放置在屏幕的左上角。


/* this variable will store the ID of the newly created window. */
Window win;

/* these variables will store the window's width and height. */
int win_width;
int win_height;

/* these variables will store the window's location. */
int win_x;
int win_y;

/* calculate the window's width and height. */
win_width = DisplayWidth(display, screen_num) / 3;
win_height = DisplayHeight(display, screen_num) / 3;

/* position of the window is top-left corner - 0,0. */
win_x = win_y = 0;

/* create the window, as specified earlier. */
win = XCreateSimpleWindow(display,
? ?? ?? ?? ?? ?? ?? ?? ???RootWindow(display, screen_num),
? ?? ?? ?? ?? ?? ?? ?? ???win_x, win_y,
? ?? ?? ?? ?? ?? ?? ?? ???win_width, win_height,
? ?? ?? ?? ?? ?? ?? ?? ???win_border_width, BlackPixel(display, screen_num),
? ?? ?? ?? ?? ?? ?? ?? ???WhitePixel(display, screen_num));

The fact that we created the window does not mean it will be drawn on screen. By default,?

newly created windows are not mapped on the screen - they are invisible. In order to make?

our window visible, we use the XMapWindow() function, as follows:
我們創造了窗口的事實并不意味著它會被畫在屏幕上。缺省的,新創建的窗口不會被映射于屏幕之上 -?

它們是不可見的。為了使得我們的窗口可見,我們使用XMapWindow()函數,如下:


XMapWindow(win);

To see all the code we have gathered so far, take a look at the simple-window.c program.?

You'll see two more function not explained so far - XFlush() and XSync(). The XFlush()?

function flushes all pending requests to the X server - much like the fflush() function is?

used to flash standard output. The XSync() function also flushes all pending requests to the?

X server, and then waits until the X server finishes processing these requests. In a normal?

program this will not be necessary (you'll see why when we get to write a normal X program),?

but for now we put it there. Try compiling the program either with or without these function?

calls to see the difference in its behavior.
要看我們至今積累寫出的所有代碼,看看simple-window.c程序。你將看到至今沒有解釋的兩個另外的函

數 - XFlush()和XSync()函數用來清除仍未發送給X服務器的請求 - 很想用來清除標準輸出的fflush()

函數。XSync()函數也清除所有仍未發送給X服務器的消息,而且等待X服務器結束處理所有這些請求。在

一個通常的程序中,這將不會是必要的(你可以看到為什么在我們開始寫一個普通的X程序的時候),但對

于現在我們把它放在那兒。嘗試著有和去掉這些函數調用來編譯程序,以觀察它們行為上的不同點。

Drawing In A Window
在窗口中繪圖

Drawing in a window can be done using various graphical functions - drawing pixels, lines,?

circles, rectangles, etc. In order to draw in a window, we first need to define various?

general drawing parameters - what line width to use, which color to draw with, etc. This is?

done using a graphical context (GC).?
在窗口中繪圖能夠通過使用各種圖形函數來完成 - 畫點,線,圓,矩形,等。為了能夠在窗口中繪圖,

我們首先需要定義幾種通用的繪圖參數 - 線寬使用多少的,繪圖的顏色是什么,等。這個是用圖形上下

文(GC)來完成的。

Allocating A Graphics Context (GC)
分配圖形上下文(GC)

As we said, a graphical context defines several attributes to be used with the various?

drawing functions. For this, we define a graphical context. We can use more than one?

graphical context with a single window, in order to draw in multiple styles (different?

colors, different line widths, etc.). Allocating a new GC is done using the XCreateGC()?

function, as follows (in this code fragment, we assume "display" is a pointer to a Display?

structure, and "win" is the ID of a previously created window):
如我所說,圖形上下文給出幾個用于繪圖函數的屬性。因此,我們定義一個圖形上下文。我們能夠在一個

窗口中使用多余一個的圖形上下文,以達到用多種風格(不同的顏色,線寬,等)繪圖。分配一個新的GC是

通過使用XCreateGC()函數來完成的,如下(在這個代碼片段中,我們假定“display”是一個只想Display

結構的指針,而起“win”是先前創建的窗口的ID):


/* this variable will contain the handle to the returned graphics context. */
GC gc;

/* these variables are used to specify various attributes for the GC. */
/* initial values for the GC. */
XGCValues values = CapButt | JoinBevel;
/* which values in 'values' to check when creating the GC. */
unsigned long valuemask = GCCapStyle | GCJoinStyle;

/* create a new graphical context. */
gc = XCreateGC(display, win, valuemask, &values);
if (gc < 0) {
? ? fprintf(stderr, "XCreateGC: \n");
}

Note should be taken regarding the roles of "valuemask" and "values". Since a graphics?

context has zillions of attributes, and since often we don't want to define few of them, we?

need to be able to tell the XCreateGC() which attributes we want to set. This is what the?

"valuemask" variable is for. We then use the "values" variable to specify actual values for?

the attributes we defined in the "valuesmask". Thus, for each constant used in "values",?

we'll use the matching constant in "valuesmask". In this case, we defined a graphics context?

with two attributes:
注意“valuesmask”和“values”的角色。因為圖形上下文有n多屬性,并且我們不想定義它們中的一些

,我們需要能夠告訴XCreateGC()哪些屬性是我們想要設置的。這就是“valuesmask”變量的用處。我們

然后使用“values”變量來指定我們在“valuesmask”中定義的屬性的值。因而,對于每個在“values”

中使用的常量,我們將使用在“valuesmask”中相應的常量。在此例中,我們用兩個屬性定義圖形上下文



? ?1. When drawing a multiple-part line, the lines should be joined in a 'Bevelian' style.
? ?2. A line's end-point will be drawn straight (as opposed to ending the line in a round?

shape, if its width is more than 1 pixel wide).?
? ?1、當在畫多部分的線的時候,線應該以‘Bevelian’風格連接起來。
? ?2、線的終點將被直的畫出來(與以圓角結束線相對,如果它的寬度大于一個象素)。

The rest of the attributes of this GC will be set to their default values.
這個GC的剩余屬性將由它們的缺省值設定。

Once we created a graphics context, we can use it in drawing functions. We can also modify?

its parameters using various functions. Here are a few examples:
一旦我們創建了一個圖形上下文,我們能夠在繪圖函數中使用它。我們還能夠各種函數修改它的參數。這

兒有幾個例子:


/* change the foreground color of this GC to white. */
XSetForeground(display, gc, WhitePixel(display, screen_num));

/* change the background color of this GC to black. */
XSetBackground(display, gc, BlackPixel(display, screen_num));

/* change the fill style of this GC to 'solid'. */
XSetFillStyle(display, gc, FillSolid);

/* change the line drawing attributes of this GC to the given values. */
/* the parameters are: Display structure, GC, line width (in pixels), */
/* line drawing??style, cap (line's end) drawing style, and lines? ???*/
/* join style.? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???*/
XSetLineAttributes(display, gc, 2, LineSolid, CapRound, JoinRound);

for complete information on the various attributes available in a graphics context, refer to?

the manual page of XCreateGC(). We will use just a few simple attributes in our tutorial, to?

avoid over-complicating it.
要獲關于在圖形上下文中有的各種屬性的完整信息,參考XCreateGC()的手冊頁。我們將在我們的教程中

僅僅使用幾個簡單的屬性,以避搞得過度復雜了。

Drawing Primitives - Point, Line, Box, Circle...
基本繪圖-點,線,框,圓...

After we have created a GC, we can draw on a window using this GC, with a set of Xlib?

functions, collectively called "drawing primitives". Without much fuss, lets see how they?

are used. We assume that "gc" is a previously initialized GC, and that 'win' contains the?

handle of a previously created window.
在我們創建了GC之后,我們能夠使用這個GC在窗口上用一套Xlib函數繪畫了,這些函數合成為“基本繪圖

函數”。廢話不多說了,讓我們看看它們是如何使用的吧。我們假定”gc“是先前初始化了的GC,而且‘

win’包含了先前創建的窗口的句柄。

/* draw a pixel at position '5,60' (line 5, column 60) of the given window. */
XDrawPoint(display, win, gc, 5, 5);

/* draw a line between point '20,20' and point '40,100' of the window. */
XDrawLine(display, win, gc, 20, 20, 40, 100);

/* draw an arc whose center is at position 'x,y', its width (if it was a? ???*/
/* full ellipse) is 'w', and height is 'h'. Start the arc at angle 'angle1'??*/
/* (angle 0 is the hour '3' on a clock, and positive numbers go? ?? ?? ?? ???*/
/* counter-clockwise. the angles are in units of 1/64 of a degree (so 360*64 */
/* is 360 degrees).? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? */
int x = 30, y = 40;
int h = 15, w = 45;
int angle1 = 0, angle2 = 2.109;
XDrawArc(display, win, gc, x-(w/2), y-(h/2), w, h, angle1, angle2);

/* now use the XDrawArc() function to draw a circle whose diameter */
/* is 15 pixels, and whose center is at location '50,100'.? ?? ?? ?*/
XDrawArc(display, win, gc, 50-(15/2), 100-(15/2), 15, 15, 0, 360*64);

/* the XDrawLines() function draws a set of consecutive lines, whose? ???*/
/* edges are given in an array of XPoint structures.? ?? ?? ?? ?? ?? ?? ?*/
/* The following block will draw a triangle. We use a block here, since??*/
/* the C language allows defining new variables only in the beginning of */
/* a block.? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???*/
??{
? ? /* this array contains the pixels to be used as the line's end-points. */
? ? XPoint points[] = {
? ?? ?{0, 0},
? ?? ?{15, 15},
? ?? ?{0, 15},
? ?? ?{0, 0}
? ? };
? ? /* and this is the number of pixels in the array. The number of drawn */
? ? /* lines will be 'npoints - 1'.? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?*/
? ? int npoints = sizeof(points)/sizeof(XPoint);

? ? /* draw a small triangle at the top-left corner of the window. */
? ? /* the triangle is made of a set of consecutive lines, whose? ?*/
? ? /* end-point pixels are specified in the 'points' array.? ?? ? */
? ? XDrawLines(display, win, gc, points, npoints, CoordModeOrigin);
??}

/* draw a rectangle whose top-left corner is at '120,150', its width is */
/* 50 pixels, and height is 60 pixels.? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? */
XDrawRectangle(display, win, gc, 120, 150, 50, 60);

/* draw a filled rectangle of the same size as above, to the left of the??*/
/* previous rectangle. note that this rectangle is one pixel smaller than */
/* the previous line, since 'XFillRectangle()' assumes it is filling up? ?*/
/* an already drawn rectangle. This may be used to draw a rectangle using */
/* one color, and later to fill it using another color.? ?? ?? ?? ?? ?? ? */
XFillRectangle(display, win, gc, 60, 150, 50, 60);


Hopefully, you got the point by now. We will mention a few more functions that may be used?

in a similar fashion. For example, XFillArc() takes the same parameters as XDrawArc(), but?

draws only the inside of this arc (like XFillRectangle() does to a rectangle drawn using the?

XDrawRectangle() function). There is also an XFillPolygon() function that fills the inside?

of a polygon. It takes almost the same parameters as XDrawLines(). However, if the last?

point in the array has a different location than the first point in the array, the?

XFillPolygon() function automatically adds another "virtual" lines, connecting these two?

points. Another difference between the two functions, is that XFillPolygon() takes an?

additional parameters, shape, that is used to help the X server optimize its operation. You?

can read about it in your manual pages. There are also plural versions for these functions,?

namely XFillArcs() and XFillRectangles().
但愿你跟上了我的進度。我們還將提到更多的一些使用上差不多的函數。例如,XFillArc()和XDrawArc()

帶有相同的參數,但是只畫出弧的內部(像XFillRectangle()函數所作的和用XDrawRectangle()函數畫出

的矩形一樣)。還有一個填充多邊形內部的XFillPolygon()函數。它和XDrawLines()基本上有相同的參數

。然而,如果數組的最后一個點和第一個點處于不同的位置,XFillPolygon()函數自動添加一條”

virtual“線,連接這兩個點。兩個函數的另外一個不同點就是XFillPolygon()帶另外一個參數,shape。

它用來幫助X服務器優化它的行為。你能夠在手冊頁上學到這些。對于這些函數還有復數版本,名字為

XFillArcs()和XFillRectangles()。

The source code for a program doing these drawings is found in the file simple-drawing.c.
完成這些繪畫的程序的源代碼位于文件simple-drawing.c中。

X Events
X 事件

In an Xlib program, everything is driven by events. Event painting on the screen is?

sometimes done as a response to an event - an "expose" event. If part of a program's window?

that was hidden, gets exposed (e.g. the window was raised above other windows), the X server?

will send an "expose" event to let the program know it should repaint that part of the?

window. User input (key presses, mouse movement, etc.) is also received as a set of events.?
在Xlib程序中,所有的事情都是被事件驅動的。事件繪圖有時是對事件-一個”暴露的“事件-的反應。

如果程序窗口被隱藏的一部分重又暴露了(例如窗口從另外一個窗口后面升上來了),X服務器將發送一個

”暴露的“事件讓程序知道它應當重新畫處窗口的這個部分。用戶輸入(按鍵,鼠標移動,等)也是作為一

套事件被接收的。

Registering For Event Types Using Event Masks
使用事件遮罩給事件型別注冊

After a program creates a window (or several windows), it should tell the X server what?

types of events it wishes to receive for this window. By default, no events are sent to the?

program. It may register for various mouse (also called "pointer") events, keyboard events,?

expose events and so on. This is done for optimizing the server-to-client connection (i.e.?

why send a program (that might even be running at the other side of the globe) an event it?

is not interested in?).
在程序創建了一個窗口(或者幾個窗口)之后,它應當告訴X服務器它想讓這個窗口接收什么型別的事件。

缺省的,沒有事件發送給程序。它可能注冊各種鼠標(也被稱為”指針“)事件,鍵盤事件,暴露事件等等

。這是用于優化服務器和客戶之間的連接(也就是,為什么要發送給程序(那可能是運行于地球的另外一邊

的)它不感興趣的事件的?)。

In Xlib, we use the XSelectInput() function to register for events. This function accepts 3?

parameters - the display structure, an ID of a window, and a mask of the event types it?

wishes to get. The window ID parameter allows us to register for receiving different types?

of events for different windows. Here is how we register for "expose" events for a window?

whose ID is 'win':
在Xlib中,我們使用XSelectInput()函數來注冊事件。這個函數接收3個參數 - display結構,窗口的ID

,以及它想要收到的事件型別的遮罩。窗口ID這個參數使得我們能夠為不同的窗口注冊接收不同型別的事

件。這兒是我們如何給ID為‘win’的窗口注冊”暴露”事件的:


XSelectInput(display, win, ExposureMask);

ExposureMask is a constant defined in the "X.h" header file. If we wanted to register to?

several event types, we can logically "or" them, as follows:
ExposureMask 是定義在頭文件“X.h”中的常量。如果我們想要注冊好幾種事件型別,我們用邏輯或進行

連接,如下:


XSelectInput(display, win, ExposureMask | ButtonPressMask);

This registers for "expose" events as well as for mouse button presses inside the given?

window. You should note that a mask may represent several event sub-types.
遮注冊了“暴露”事件以及鼠標按鈕在給定窗口按下的事件。你應當注意到一個遮罩有可能代表了好幾種

事件子型別。

Note: A common bug programmers do is adding code to handle new event types in their program,?

while forgetting to add the masks for these events in the call to XSelectInput(). Such a?

programmer then could sit down for hours debugging his program, wondering "why doesn't my?

program notice that i released the button??", only to find that they registered for button?

press events, but not for button release events.?
注意:一個常見的蹩腳程序員所作的是在它們的程序中添加代碼來處理新的事件型別,而忘記了在調用

XSelectInput()中添加這些事件的遮罩。這樣的程序員然后坐下來花數個小時調試它們的程序,奇怪于“

為什么我的程序沒注意到我釋放鼠標??”,最后只是發現它們忘記了只注冊鼠標按下事件,而不是鼠標

釋放事件。

Receiving Events - Writing The Events Loop
接收事件-撰寫事件循環

After we have registered for the event types we are interested in, we need to enter a loop?

of receiving events and handling them. There are various ways to write such a loop, but the?

basic loop looks like this:
在我們為感興趣的事件型別注冊了之后,我們需要進入接收事件和處理它們的循環。有好幾種撰寫這樣的

循環的辦法,但基本的循環是這樣的:

/* this structure will contain the event's data, once received. */
XEvent an_event;

/* enter an "endless" loop of handling events. */
while (1) {
? ? XNextEvent(display, &an_event);
? ? switch (an_event.type) {
? ?? ?case Expose:
? ?? ???/* handle this event type... */
? ?? ???.
? ?? ???.
? ?? ???break;
? ?? ?default: /* unknown event type - ignore it. */
? ?? ???break;
? ? }
}

The XNextEvent() function fetches the next event coming from the X server. If no event is?

waiting, it blocks until one is received. When it returns, the event's data is placed in the?

XEvent variable given to the function as the second parameter. After that, the "type" field?

of this variable specifies what type of event we got. Expose is the event type that tells us?

there is a part of the window that needs to be redrawn. After we handle this event, we go?

back and wait for the next event to process. Obviously, we will need to give the user some?

way of terminating the program. This is usually done by handling a special "quit" event, as?

we'll soon see.
XNextEvent()函數取得從X服務器發送來的下一個事件。如果沒有事件在等待,它阻塞在那知道接收到了

一個。當它返回了,事件的數據被放置給函數的第二個參數XEvent變量中。之后,變量的“type”域指定

了我們得到的事件的型別。事件的型別是Expose告訴我們窗口的一部分布需要重畫。在我們處理了事件之

后,我們回過頭來繼續等待下一個要處理的。明顯,我們需要給用戶某種終止程序的途徑。如我們即將看

到的,這通常是通過處理“quit”事件來完成那個的。

Expose Events
暴露事件

The "expose" event is one of the most basic events an application may receive. It will be?

sent to us in one of several cases:
“暴露”事件是程序可能接收的最基本的事件中的一個。它在一下情況中將發送給我們:

? ? * A window that covered part of our window has moved away, exposing part (or all) of our?

window.
? ? * Our window was raised above other windows.
? ? * Our window mapped for the first time.
? ? * Our window was de-iconified.?
? ? * 覆蓋我們一部分窗口的窗口被移開了,暴露我們窗口的部分(或者全部)。
? ? * 我們的窗口從其他窗口后面升上來了
? ? * 我們的窗口第一次映射
? ? * 我們的窗口被取消標識了。

You should note the implicit assumption hidden here - the contents of our window is lost?

when it is being obscured (covered) by other windows. One may wonder why the X server does?

not save this contents. The answer is - to save memory. After all, the number of windows on?

a display at a given time may be very large, and storing the contents of all of them might?

require a lot of memory (for instance, a 256 color bitmap covering 400 pixels by 400 pixels?

takes 160KB of memory to store. Now think about 20 windows, some much larger than this?

size). Actually, there is a way to tell the X server to store the contents of a window in?

special cases, as we will see later.
你應當注意背后隱藏的假設 - 我們窗口的內容在被其他窗口遮蓋時候丟失了。你可能奇怪X服務器為什

么不保存這些內容。答案是 - 為了節省內存。畢竟,窗口在display上的數量在給定時間是非常巨大的

,而且保存它們的所有內容可能需要很多的內存(例如,大小為400*400象素的256色位圖占據160KB的內存

存儲。現在想想20個窗口,比這個數字要大得多)。事實上,在特殊情況下有告訴X服務器保存窗口內容的

辦法,我們將在后面看到。

When we get an "expose" event, we should take the event's data from the "xexpose" member of?

the XEvent structure (in our code example we refer to it as "an_event.xexpose"). It contains?

several interesting fields:
當我們得到一個“暴露”事件的時候,我們應當從XEvent結構的"xexpose“成員處取出事件的數據(在我

們的代碼例子中用”an_event.xexpose“引用它)。它包含幾個有趣的域:

count
? ? Number of other expose events waiting in the server's events queue. This may be useful?

if we got several expose events in a row - we will usually avoid redrawing the window until?

we get the last of them (i.e. until count is 0).?
? ? 在服務器中的事件隊列等待的其他暴露事件的數量。這個可能在我們一次接連得到好幾個的時候有用?

- 我們通常將避免重畫知道我們得到它們的最后一個(也就是知道count為0的時候)。
Window window
? ? The ID of the window this expose event was sent for (in case our application registered?

for events on several windows).?
? ? 暴露事件被發送的窗口的ID(如果我們的程序在幾個窗口中注冊事件)。
int x, y
? ? The x and y coordinates (in pixels) from the top-left of the window, of the window's?

region that needs to be redrawn.?
? ? 需要重畫的窗口區域的從窗口左上角開始的x和y坐標(象素為單位)。
int width, height
? ? The width and height (in pixels) of the window's region that needs to be redraw.
? ? 需要重畫的窗口區域的寬和高(象素為單位)。?

In our demo programs, we will tend to ignore the region supplied, and simply re-draw all the?

window. However, this is very inefficient, and we will try to demonstrate some techniques?

for drawing only the relevant section of screen later on.
在我們的演示程序中,我們將傾向于忽略提供的區域,而僅僅重畫整個屏幕。然而,這是非常沒有效率的

,而且我們將嘗試在后面演示一些僅僅畫出相關屏幕部分的技術。

As an example, here is how we will draw a line across our window, whenever we receive?

"expose" events. Assume this 'case' is part of the event loop's switch command.
作為例子,這是我們將如何橫跨我們的窗口畫一條線,每當我們接收到”暴露“事件的時候。假設這個’

case‘是事件循環switch語句的一部分。


??case Expose:
? ? /* if we have several other expose events waiting, don't redraw. */
? ? /* we will do the redrawing when we receive the last of them.? ? */
? ? if (an_event.xexpose.count > 0)
? ?? ???break;
? ? /* ok, now draw the line... */
? ? XDrawLine(display, win, gc, 0, 100, 400, 100);
? ? break;

Getting User Input
獲得用戶輸入

User input traditionally comes from two sources - the mouse and the keyboard. Various event?

types exist to notify us of user input - a key being pressed on the keyboard, a key being?

released on the keyboard, the mouse moving over our window, the mouse entering (or leaving)?

our window and so on.?
傳統上用戶輸入有兩個來源 - 鼠標和鍵盤。存在多種事件型別來通知我們用戶的輸入 - 鍵盤上的按鍵

被按下,在鍵盤上釋放按鍵,鼠標移動于我們的窗口之上,鼠標進入(或者離開)我們的窗口等等。

Mouse Button Click And Release Events
鼠標按鈕點擊和釋放事件

The first event type we'll deal with is a mouse button-press (or button release) event in?

our window. In order to register to such an event type, we would add one (or more) of the?

following masks to the event types we specify for the XSelectInput() function:
我們將要處理的第一個事件型別是在我們窗口中鼠標按鈕按下(或者按鍵放開)事件。為了注冊這樣的一個

事件,我們將添加一下遮罩中一個(或者更多)來在XSelectInput()函數中指定事件型別:

ButtonPressMask
? ? Notify us of any button that was pressed in one of our windows.
ButtonReleaseMask
? ? Notify us of any button that was released over one of our windows.

The event types to be checked for in our event-loop switch, are any of the following:

ButtonPress
? ? A button was pressed over one of our windows.
ButtonRelease
? ? A button was released over one of our windows.

The event structure for these event types is accessed as "an_event.xbutton", and contains?

the following interesting fields:
這些事件型別的事件結構是通過"an_event.xbutton“來訪問的,并且包括一下有趣的域:

Window window
? ? The ID of the window this button event was sent for (in case our application registered?

for events on several windows).
? ? 鼠標事件發送給的窗口ID(如果我們的程序在幾個窗口中注冊了事件)。
int x, y
? ? The x and y coordinates (in pixels) from the top-left of the window, of the mouse?

pointer, during the click.
? ? 在點擊時,鼠標指針從窗口左上角為原點的x和y坐標(象素為單位)。
int button
? ? The number of mouse button that was clicked. May be a value such as Button1, Button2,?

Button3.
? ? 被點擊的鼠標按鈕的編號。可能是像Button1, Button2, Button3這樣的值。
Time time
? ? time (in millisecond) the event took place in. May be used to calculate "double-click"?

situations by an application (e.g. if the mouse button was clicked two times in a duration?

shorter than a given amount, assume this was a double-click).
? ? 事件發生的時間(毫秒為單位)。可能用于在程序中計算”雙擊“的情況(例如,如果鼠標按鈕在小于

給定時間內被點擊兩次,就認定這個為雙擊)。

As an example, here is how we will draw a black pixel at the mouse position, whenever we?

receive "button press" events, with the 1st mouse button, and erase that pixel (i.e. draw a?

white pixel) when the 2nd mouse button is pressed. We assume the existence of two GCs,?

gc_draw with foreground color set to black, and gc_erase, with foreground color set to?

white.
作為例子,這兒是我們如何每當接收到”鼠標按下“事件時,當按下的是第一個鼠標按鈕的時候在鼠標點

擊位置畫一個黑點的,而是第二個的時候擦除該點(也就是畫個白點)。我們假定存在兩個GC,gc_draw設

置為前景色為黑,而gc_erase前景色為白。
Assume that the following 'case' is part of the event loop's switch command.
假定一下'case’是事件循環的swtich語句的一部分。

??case ButtonPress:
? ? /* store the mouse button coordinates in 'int' variables. */
? ? /* also store the ID of the window on which the mouse was */
? ? /* pressed.? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???*/
? ? x = an_event.xbutton.x;
? ? y = an_event.xbutton.y;
? ? the_win = an_event.xbutton.window;

? ? /* check which mouse button was pressed, and act accordingly. */
? ? switch (an_event.xbutton.button) {
? ?? ???case Button1:
? ?? ?? ?? ?/* draw a pixel at the mouse position. */
? ?? ?? ?? ?XDrawPoint(display, the_win, gc_draw, x, y);
? ?? ?? ?? ?break;
? ?? ???case Button2:
? ?? ?? ?? ?/* erase a pixel at the mouse position. */
? ?? ?? ?? ?XDrawPoint(display, the_win, gc_erase, x, y);
? ?? ?? ?? ?break;
? ?? ???default: /* probably 3rd button - just ignore this event. */
? ?? ?? ?? ?break;
? ? }
? ? break;

Mouse Movement Events
鼠標移動事件

Similar to mouse button press and release events, we also can be notified of various mouse?

movement events. These can be split into two families. One is of mouse pointer movement?

while no buttons are pressed, and the second is a mouse pointer motion while one (or more)?

of the buttons are pressed (this is sometimes called "a mouse drag operation", or just?

"dragging"). The following event masks may be added in the call to XSelectInput() for our?

application to be notified of such events:
與鼠標按下和釋放事件類似,我們也可以得到各種鼠標移動事件的通知。這些能夠劃分為兩類。一類是按

鈕沒有被按下時鼠標指針的移動,而第二類時當一個或者多個按鈕被按下的時候鼠標指針的移動(這有時

被稱為“鼠標托放操作”,或者僅僅“拖放”)。下面的事件遮罩可以加到XSelectInput()的調用中以讓

我們的應用程序得到這些事件的通知。

PointerMotionMask
? ? Events of the pointer moving in one of the windows controlled by our application, while?

no mouse button is held pressed.
指針移動遮罩
? ? 當沒有鼠標按鈕被按下時,由程序控制的窗口中的一個的指針移動的事件
ButtonMotionMask
? ? Events of the pointer moving while one (or more) of the mouse buttons is held pressed.
按鈕移動遮罩
? ? 當鼠標的一個(或者更多)的鼠標按鈕被按下的時候指針移動的事件。
Button1MotionMask
? ? Same as ButtonMotionMask, but only when the 1st mouse button is held pressed.
按鈕1移動遮罩
? ? 核按鈕移動遮罩一樣,只不過當第一個鼠標按鈕被按下的時候。
Button2MotionMask, Button3MotionMask, Button4MotionMask, Button5MotionMask
? ? Likewise, for 2nd mouse button, or 3rd, 4th or 5th.
鼠標2移動遮罩,鼠標3移動遮罩,鼠標4移動遮罩,鼠標5移動遮罩
? ? 類似的,用于第二個鼠標按鈕,或者第三,第四,第五。

The event types to be checked for in our event-loop switch, are any of the following:
在我們的事件循環swtich語句中要檢查的事件型別,是以下的任何一個:

MotionNotify
? ? The mouse pointer moved in one of the windows for which we requested to be notified of?

such events.
移動通知
? ? 在我們需要得到這個消息通知的窗口中移動的鼠標指針。

The event structure for these event types is accessed as "an_event.xbutton", and contains?

the following interesting fields:
這些事件型別的事件結構是以”an_event.xbutton“來訪問的,并且包含一下有趣的域:

Window window
? ? The ID of the window this mouse motion event was sent for (in case our application?

registered for events on several windows).
? ? 鼠標移動事件發送給的窗口的ID(如果我們的應用程序給幾個窗口注冊了事件)。
int x, y
? ? The x and y coordinates (in pixels) from the top-left of the window, of the mouse?

pointer, when the event was generated.
? ? 事件發生的時候,以窗口的左上角為原點鼠標指針所位于的x和y坐標(象素為單位)。
unsigned int state
? ? A mask of the buttons (or keys) held down during this event - if any. This field is a?

bitwise OR of any of the following:
? ? 按鈕(或者按鍵)在事件發生時按下的遮罩 - 如果有的話。改域是以下值的位或:
??# Button1Mask
??# Button2Mask
??# Button3Mask
??# Button4Mask
??# Button5Mask
??# ShiftMask
??# LockMask
??# ControlMask
??# Mod1Mask
??# Mod2Mask
??# Mod3Mask
??# Mod4Mask
??# Mod5Mask
? ? Their names are self explanatory, where the first 5 refer to mouse buttons that are?

being pressed, while the rest refer to various "special keys" that are being pressed (Mod1?

is usually the 'ALT' key or the 'META' key).?
? ? 它們的名字是可以自明的,前五個是指被按下的鼠標按鈕,而剩下的指的是被按下的“特殊按鍵”

(Mod1通常是‘ALT’或者‘META’鍵)。
Time time
? ? time (in millisecond) the event took place in.
? ? 事件發生所處的事件(毫秒為單位)。

As an example, the following code handles a "draw mode" for a painting program, that is, if?

the user moves the mouse while the 1st mouse button is being held down, then we 'draw' on?

the screen. Note that this code has a flow: Since mouse movement may generate many events,?

it might be that we won't get a mouse motion event for each pixel the mouse moved over. Our?

program should be able to cope with such a situation. One way to do that would be to?

remember the last pixel the mouse was dragged over, and draw a line between that position?

and the new mouse pointer position. Assume that the following 'case' is part of the event?

loop's switch command.
作為例子,以下的代碼處理一個繪圖程序的“繪圖模式”,也就是說如果用戶在鼠標1鍵按下的時候移動

了,那么我們在屏幕上“繪圖”。注意代碼有一個慣性:因為鼠標移動可能產生許多事件,可能我們不會

在每個鼠標移到的點都得到鼠標移動事件。我們的程序應當能夠處理這么一個情況。解決的一個辦法可能

是記住鼠標托過的上一個點,并在和新的鼠標指針位置之間畫直線。假定下面的‘case’是事件循環的

switch語句的一部分。


??case MotionNotify:
? ? /* store the mouse button coordinates in 'int' variables. */
? ? /* also store the ID of the window on which the mouse was */
? ? /* pressed.? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???*/
? ? x = an_event.xmotion.x;
? ? y = an_event.xmotion.y;
? ? the_win = an_event.xbutton.window;

? ? /* if the 1st mouse button was held during this event, draw a pixel */
? ? /* at the mouse pointer location.? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???*/
? ? if (an_event.xmotion.state & Button1Mask) {
? ?? ???/* draw a pixel at the mouse position. */
? ?? ???XDrawPoint(display, the_win, gc_draw, x, y);
? ? }
? ? break;

Mouse Pointer Enter And Leave Events
鼠標指針進入和離開事件

Another type of event that applications might be interested at, is a mouse pointer entering?

a window the program controls, or leaving such a window. Some programs use these events to?

show the user that the application is now in focus. In order to register for such an event?

type, we would add one (or more) of the following masks to the event types we specify for?

the XSelectInput() function:
另一個應用程序可能感興趣的事件型別,是鼠標指針進入或者離開程序控制的窗口。一些程序使用這些事

件來向用戶展示應用程序現在在焦點狀態。為了注冊這么一個事件型別,我們將把下面的一個(或者多個)

遮罩添加到我們給XSelectInput()函數指定的事件型別中:

EnterWindowMask
? ? Notify us when the mouse pointer enters any of our controlled windows.
? ? 當鼠標指針進入我們控制的任何窗口時通知我們。
LeaveWindowMask
? ? Notify us when the mouse pointer leaves any of our controlled windows.
? ? 當鼠標指針離開我們控制的任何窗口時通知我們。

The event types to be checked for in our event-loop switch, are any of the following:
要在我們的事件循環swtich中檢查的事件型別是以下的這些:

EnterNotify
? ? The mouse pointer just entered one of our controlled windows.
? ? 鼠標指針剛剛進入了我們控制的窗口
LeaveNotify
? ? The mouse pointer just left one of our controlled windows.
? ? 鼠標指針剛剛離開了我們控制的窗口

The event structure for these event types is accessed as "an_event.xcrossing", and contains?

the following interesting fields:
這些事件型別的事件結構是通過“an_event.xcrossing"來訪問的,并且包含以下有趣的域:

Window window
? ? The ID of the window this button event was sent for (in case our application registered?

for events on several windows).
? ? 鼠標事件發送給的窗口的ID(如果我們的程序給幾個程序注冊了事件)。
Window subwindow
? ? The ID of the child window from which the mouse entered our window (in an EnterNotify?

event), or into which the mouse pointer has moved (in a LeaveNotify event), or None, if the?

mouse moved from outside our window.
? ? 鼠標進入到我們的窗口(在EnterNotify事件中),或者鼠標指針移出到的子窗口ID(在LeaveNotify事

件中),或者兩者均否,如果鼠標從我們的窗口外邊移入。
int x, y
? ? The x and y coordinates (in pixels) from the top-left of the window, of the mouse?

pointer, when the event was generated.
? ? 事件產生的時候,鼠標指針以窗口的左上角為原點的x和y坐標(象素為單位)。
int mode
? ? The number of mouse button that was clicked. May be a value such as Button1, Button2,?

Button3.
? ? 鼠標指針點擊的編號。可能是如Button1, Button2, Button3這樣的值。
Time time
? ? time (in millisecond) the event took place in. May be used to calculate "double-click"?

situations by an application (e.g. if the mouse button was clicked two times in a duration?

shorter than a given amount, assume this was a double-click).
? ? 事件發生的時間(毫秒為單位)。可能用于在程序中計算”雙擊“的情況(例如,如果鼠標按鈕在小于

給定時間內被點擊兩次,就認定這個為雙擊)。
unsigned int state
? ? A mask of the buttons (or keys) held down during this event - if any. This field is a?

bitwise OR of any of the following:
??# Button1Mask
??# Button2Mask
??# Button3Mask
??# Button4Mask
??# Button5Mask
??# ShiftMask
??# LockMask
??# ControlMask
??# Mod1Mask
??# Mod2Mask
??# Mod3Mask
??# Mod4Mask
??# Mod5Mask
? ? Their names are self explanatory, where the first 5 refer to mouse buttons that are?

being pressed, while the rest refer to various "special keys" that are being pressed (Mod1?

is usually the 'ALT' key or the 'META' key).?
Bool focus
? ? Set to True if the window has the keyboard focus, False otherwise.
? ? 如果窗口擁有鍵盤焦點,設置為真。否則反之。

The Keyboard Focus
鍵盤焦點

There may be many windows on a screen, but only a single keyboard attached to them. How does?

the X server then know which window should be sent a given keyboard input? This is done?

using the keyboard focus. Only a single window on the screen may have the keyboard focus at?

a given time. There are Xlib functions that allow a program to set the keyboard focus to a?

given window. The user can usually set the keyboard focus using the window manager (often by?

clicking on the title bar of the desired window). Once our window has the keyboard focus,?

every key press or key release will cause an event to be sent to our program (if it?

registered for these event types...).
屏幕上有許多窗口,但是僅僅有一個鍵盤和他附著。X服務器如何知道鍵盤輸入是發送給哪個窗口的呢?

這個是通過鍵盤焦點來完成的。在給定的時間,屏幕上只有一個窗口能夠有鍵盤焦點。存在Xlib函數來使

得程序給某個窗口設置鍵盤焦點。用戶通常能夠使用窗口管理器來安排鍵盤焦點(通常是通過點擊所需窗

口的標題欄)。一旦我們的窗口擁有了鍵盤焦點,每個鍵的按下和放開都將導致事件發送給我們的程序(如

果它注冊了這些事件類型...)。

Keyboard Press And Release Events
鍵盤按下和釋放事件

If a window controlled by our program currently holds the keyboard focus, it can receive key?

press and key release events. In order to register for such events, any of the following?

masks may be added to the call to XSelectInput():
如果由我們程序控制的窗口當前保有鍵盤焦點,它能夠接收鍵的按下和釋放事件。為了注冊這些事件,下

面的遮罩要加到XSelectInput()的調用中去:

KeyPressMask
? ? Notify our program when a key was pressed while any of its controlled windows had the?

keyboard focus.
KeyPressMask
? ? Notify our program when a key was released while any of its controlled windows had the?

keyboard focus.

The event types to be checked for in our event-loop switch, are any of the following:

KeyPress
? ? A key was just pressed on the keyboard while any of our windows had the keyboard focus.
KeyRelease
? ? A key was just released on the keyboard while any of our windows had the keyboard focus.

The event structure for these event types is accessed as "an_event.xkey", and contains the?

following interesting fields:

Window window
? ? The ID of the window this button event was sent for (in case our application registered?

for events on several windows).
unsigned int keycode
? ? The code of the key that was pressed (or released). This is some internal X code, that?

should be translated into a key symbol, as will be explained below.
int x, y
? ? The x and y coordinates (in pixels) from the top-left of the window, of the mouse?

pointer, when the event was generated.
Time time
? ? time (in millisecond) the event took place in. May be used to calculate "double-click"?

situations by an application (e.g. if the mouse button was clicked two times in a duration?

shorter than a given amount, assume this was a double-click).
unsigned int state
? ? A mask of the buttons (or modifier keys) held down during this event - if any. This?

field is a bitwise OR of any of the following:
??# Button1Mask
??# Button2Mask
??# Button3Mask
??# Button4Mask
??# Button5Mask
??# ShiftMask
??# LockMask
??# ControlMask
??# Mod1Mask
??# Mod2Mask
??# Mod3Mask
??# Mod4Mask
??# Mod5Mask
? ? Their names are self explanatory, where the first 5 refer to mouse buttons that are?

being pressed, while the rest refer to various "special keys" that are being pressed (Mod1?

is usually the 'ALT' key or the 'META' key).?

As we mentioned, the key code is rather meaningless on its own, and is affected by the?

specific keyboard device attached to the machine running the X server. To actually use this?

code, we translate it into a key symbol, which is standardized. We may use the?

XKeycodeToKeysym() function to do the translation. This function gets 3 parameters: a?

pointer to the display, the key code to be translated, and an index (we'll supply '0' for?

this parameter). Standard Xlib key codes are found in the include file "X11/keysymdef.h". As?

an example for using the key press events together with the XKeycodeToKeysym function, we'll?

show how to handle key presses of this sort: Pressing '1' will cause painting the pixel?

where the mouse pointer is currently located. Pressing the DEL key will cause to erase that?

pixel (using a 'gc_erase' GC). Pressing any of the letters (a to z, upper case or lower?

case) will cause it to be printed to standard output. Any other key pressed will be ignored.?

Assume that the following 'case' is part of the event loop's switch command.?
如果我們提到過的,按鍵代碼于其本身是相當沒有意義的,并且是受到了附著于運行X服務器的機器的鍵

盤設備的影響。為了真正使用這些代碼,我們要把他們翻譯為按鍵符號,它們是標準的。我們能用

XKeycodeToKeysym()函數來完成翻譯工作。這個函數需要3個參數:只想display的指針,要翻譯的按鍵代

碼,和一個索引(我們將用‘0’來作這個參數)。標準Xl

總結

以上是生活随笔為你收集整理的用Xlib库进行基本图形编程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: av无限看 | 国产精品久久久久无码av色戒 | xxx.国产| 黄色污小说 | 一区二区三区丝袜 | 欧美xxx视频 | 中国老太婆性做爰 | 婷婷在线免费 | 99国产精品视频免费观看一公开 | 性开放网站 | 深夜福利视频导航 | 我要色综合天天 | 亚洲午码 | 亚洲欧美高清视频 | av在线资源播放 | 国产精品国产三级国产aⅴ下载 | 黄色片在线 | 黄色网址进入 | 国产伦精品一区二区三区免费视频 | 国产视频手机在线播放 | 亚洲影院中文字幕 | 日韩欧美亚洲国产精品字幕久久久 | 中日毛片 | 深夜福利久久 | 亚洲国产精品无码久久久久高潮 | 亚洲欧美综合精品久久成人 | 337p亚洲欧洲色噜噜噜 | 极品粉嫩小仙女高潮喷水久久 | 2019天天干天天操 | 成人小视频在线看 | 女人叉开腿让男人桶 | 国产精品无码一区二区三区在线看 | 婷婷六月综合网 | 草久久久 | 三级黄在线观看 | 激情综合影院 | 日本妈妈3 | 欧美亚洲另类视频 | 国产精品mm | 免费在线观看日韩av | 国毛片| 国产在线免费av | 欧美日韩国产一区二区在线观看 | 久久亚洲精品国产精品黑人v | 99香蕉视频 | 国产一区二区在线电影 | 久久精品亚洲精品 | av网址网站 | 黄网站欧美内射 | 美女又爽又黄视频 | 国产一区二区不卡视频 | 香蕉国产 | 男女福利视频 | 狂野欧美性猛交blacked | 亚洲人成电影在线播放 | 国产黄色在线播放 | 九色porny原创自拍 | 狠狠地日 | 亚洲高清视频在线播放 | 北条麻妃av在线播放 | 色图视频 | eeuss一区二区三区 | 一区二区三区视频免费观看 | 琪琪色网 | 欧美瑟瑟 | 黄色九九 | 日韩免费播放 | 对白超刺激精彩粗话av | 一个人看的www视频在线观看 | xxxx 国产 | 精品在线视频一区二区 | 欧美伊人影院 | 精品人妻无码一区二区三区蜜桃一 | 你懂得在线 | jzjzz成人免费视频 | www天天干 | 欧美 日韩 国产 成人 在线 91 | 干夜夜| 好吊色一区二区三区 | 欧美日韩在线影院 | 荒野求生21天去码版网站 | 少妇性l交大片免潘金莲 | 国产精品无码一区二区桃花视频 | 亚洲天堂网站 | 瑟瑟视频在线免费观看 | 国产精品久久久久一区二区 | 成人免费黄色片 | 色www| 欧美亚洲二区 | 中出一区二区 | 少妇精品无码一区二区免费视频 | 97超碰人人澡人人爱学生 | 脱裤吧导航 | 亚洲免费中文 | 亚洲免费视频一区二区 | 欧美精品乱码久久久久久按摩 | 成人精品一区二区三区在线 | 2021久久 | 欧美高清hd |