xlib-programming-Basic Graphics Programming With The Xlib Library

目录

Creating And Destroying A Mouse Cursor

Setting A Window's Mouse Cursor


Often we see programs that modify the shape of the mouse pointer (also called the X pointer) when in certain states. For example, a busy application would often display a sand clock over its main window, to give the user a visual hint that they should wait. Without such a visual hint, the user might think that the application got stuck. Lets see how we can change the mouse cursor for our windows.

Creating And Destroying A Mouse Cursor

There are two methods for creating cursors. One of them is by using a set of pre-defined cursors, that are supplied by Xlib. The other is by using user-supplied bitmaps.
In the first method, we use a special font named "cursor", and the function XCreateFontCursor(). This function accepts a shape identifier, and returns a handle to the generated cursor. The list of allowed font identifiers is found in the include file <X11/cursorfont.h>.
Here are a few such cursors:
XC_arrow
The normal pointing-arrow cursor displayed by the server.
XC_pencil
A cursor shaped as a pencil.
XC_watch
A sand watch.
And creating a cursor using these symbols is very easy:

#include <X11/cursorfont.h> /* defines XC_watch, etc. */
/* this variable will hold the handle of the newly created cursor. */
Cursor watch_cursor;
/* create a sand watch cursor. */
watch_cursor = XCreateFontCursor(display, XC_watch);

The other methods of creating a cursor is by using a pair of pixmaps with depth of one (that is, two color pixmaps). One pixmap defines the shape of the cursor, while the other works as a mask, specifying which pixels of the cursor will be actually drawn. The rest of the pixels will be transparent.
Creating such a cursor is done using the XCreatePixmapCursor() function. As an example, we will create a cursor using the "icon.bmp" bitmap. We will assume that it was already loaded into memory, and turned into a pixmap, and its handle is stored in the 'bitmap' variable. We will want it to be fully transparent. That is, only the parts of it that are black will be drawn, while the white parts will be transparent. To achieve this effect, we will use the icon both as the cursor pixmap and as the mask pixmap. Try to figure out why...

/* this variable will hold the handle of the newly created cursor. */
Cursor icon_cursor;
/* first, we need to define foreground and background colors for the cursor. */
XColor cursor_fg, cursor_bg;
/* access the default color map of our screen. */
Colormap screen_colormap = DefaultColormap(display, DefaultScreen(display));
/* allocate black and while colors. */
Status rc = XAllocNamedColor(display,screen_colormap,"black",&cursor_fg,&cursor_fg);
if (rc == 0) {fprintf(stderr, "XAllocNamedColor - cannot allocate 'black' ??!!??\n");exit(1);
}
Status rc = XAllocNamedColor(display,screen_colormap,"white",&cursor_bg,&cursor_bg);
if (rc == 0) {fprintf(stderr, "XAllocNamedColor - cannot allocate 'white' ??!!??\n");exit(1);
}
/* finally, generate the cursor. make the 'hot spot' be close to the */
/* top-left corner of the cursor - location (x=5, y=4). */
icon_cursor = XCreatePixmapCursor(display, bitmap, bitmap,&cursor_fg, &cursor_bg,5, 4);

One thing to be explained is the 'hot spot' parameters. When we define a cursor, we need to define which pixel of the cursor is the pointer delivered to the user in the various mouse events. Usually, we will choose a location of the cursor that visually looks like a hot spot. For example, in an arrow cursor, the tip of the arrow will be defined as the hot spot.
Finally, when we are done with a cursor and no longer need it, we can release it using the XFreeCursor() function:

XFreeCursor(display, icon_cursor);

Setting A Window's Mouse Cursor

After we have created a cursor, we can tell the X server to attach this cursor to any of our windows. This is done using the XDefineCursor(), and causes the X server to change the mouse pointer to the shape of that cursor, each time the mouse pointer moves into and across that window. We can later detach this cursor from our window using the XUndefineCursor() function. This will cause the default cursor to be shown when the mouse enter that windows.

/* attach the icon cursor to our window. */
XDefineCursor(display, win, icon_cursor);
/* detach the icon cursor from our window. */
XUndefineCursor(display, win);

As an example, look at our cursor.c program, and see how mouse cursors are set, changed and removed. Run the program, place the mouse pointer over the created window, and watch.

X Window Messing With The Mouse Cursor相关推荐

  1. UE4/UE5 虚幻引擎,设置Mouse Cursor鼠标光标样式

    UE虚幻引擎,设置Mouse Cursor鼠标光标样式的两种方法: 第一种.使用Player Controller中内部提供的鼠标样式. 第二种.在Project Settings项目设置的Softw ...

  2. #711 – 在拖拽的过程中改变鼠标样式(Changing the Mouse Cursor While Dragging)

    原文地址:https://wpf.2000things.com/2012/12/13/711-changing-the-mouse-cursor-while-dragging/ 在WPF拖拽的过程中, ...

  3. 界面(1):对话框和菜单 打印和按钮等杂项

    Q 请教高手,图形对话框的问题 T 我做了一个图象的界面,对话框的,在OnPaint中 画上背景图案,然后用Invalidate 方法刷新每个控件,但是控件并没有完全显示出来,特别是CCtrlList ...

  4. window通过命令行修改屏幕分辨率的方法

    修改的方法是这样的 下载nircmd: http://www.nirsoft.net/utils/nircmd.html 修改分辨率: ./nircmd.exe setdisplay 1280 720 ...

  5. 【CEGUI】 Window环境编译

    CEGUI编译 平台:Window CEGUI版本:0.8.7 前提:Visual Studio任意版本(本文为2013) CEGUI源码下载 登录CEGUI官网网站: http://cegui.or ...

  6. python pygame鼠标点击_Python中pygame的mouse鼠标事件用法实例

    本文实例讲述了Python中pygame的mouse鼠标事件用法.分享给大家供大家参考,具体如下: pygame.mouse提供了一些方法获取鼠标设备当前的状态 ''' pygame.mouse.ge ...

  7. ArcGIS For Flex学习之Mapping---Map Extent and Mouse Coordinates

    效果图如下: 1 <?xml version="1.0" encoding="utf-8"?> 2 <s:Application xmlns: ...

  8. 光标跟随(Cursor following)

    光标跟随 示例 JS 更多有趣示例 尽在 知屋安砖社区 示例 JS // create the scene let scene = new THREE.Scene()// create the cam ...

  9. Mouse.bat 模拟鼠标操作脚本

    mouse.bat 模拟鼠标操作, 调用方式 //clicks at the current position call mouse click //double clicks at the curr ...

最新文章

  1. Python开发基础总结之套接字+字符串+正则表达式
  2. Requirejs定义模块
  3. Android7.0 PowerManagerService(3) 核心函数updatePowerStateLocked的主要流程
  4. OpenGL ES之GLSL实现仿抖音“灰度滤镜”和“颠倒滤镜”效果
  5. C++(STL):02---tuple容器
  6. 7-4 输出最小公倍数 (9 分)
  7. (33)System Verilog模块与包定义同名类冲突
  8. SVG SMIL animation动画详解
  9. 十步一拆:iPhone4S拆机十步曲
  10. 转:用AutoCAD 系统变量编程
  11. 群晖 Docker加速方案
  12. discuz数据字典
  13. 抖音大咖如何寻找广告主?这三种途径值得了解
  14. 0xfffffff1 lr_CortexM处理器的一些特性记录
  15. 长文慎入!经验分享-专科毕业5年,成功入职腾讯!
  16. 开机显示无法登录到你的账户解决方法(亲测)
  17. 亚马逊云科技 Build On - 理解和使用 stepfunction 创建 serverless 应用
  18. socket加入组播
  19. Python:使用opennsfw2对图片/视频进行鉴黄识别
  20. WebSphere电子教程

热门文章

  1. Springboot底层注解(容器功能)
  2. SpringMVC的响应JSON数据和过滤静态资源
  3. linux配置https
  4. POJ 1821 Fence(单调队列优化DP)
  5. codeforces 688 E. The Values You Can Make(01背包+思维)
  6. Android中那些有你不知道的事
  7. 苹果查询水货苹果笔记本(Mac Book)验机流程
  8. 页面缓存,数据源缓存
  9. vim内过长字符串导致的语法加亮错误
  10. 案例:演示JDBC的使用