XDrawRectangle绘制矩形缺失右下角像素,求排查解决方法
XDrawRectangle包装函数绘制矩形时右下角像素缺失问题
在Linux系统基于X11/Xlib.h实现XDrawRectangle的包装函数后,绘制矩形时右下角像素未显示。
X11版本信息
$ dpkg -l | grep libx11-dev ii libx11-dev:amd64 2:1.7.5-1ubuntu0.3 amd64 X11 client-side library (development headers)
XDrawRectangle函数定义
当前安装的X11/Xlib.h中该函数定义如下:
extern int XDrawRectangle( Display* /* display */, Drawable /* d */, GC /* gc */, int /* x */, int /* y */, unsigned int /* width */, unsigned int /* height */ );
我的代码实现
头文件声明
/* Draw a rectangle at (x,y) with width (w) and height (h) */ void gfx_rect( int x, int y, unsigned int w, unsigned int h );
函数实现
/* Draw a rectangle at (x,y) with width (w) and height (h) */ void gfx_rect( int x, int y, unsigned int w, unsigned int h ) { XDrawRectangle(gfx_display,gfx_window,gfx_gc,x,y,w,h); }
调用代码
const int offset = 10; int x = 0; int y = 0; gfx_rect(x+offset, y+offset, 20, 20);
绘制效果
矩形右下角像素缺失,呈现不完整的边框。
可复现代码及编译命令
编译命令
gcc main.c -o fontview -lX11 -lm
完整可复现代码
#include <X11/Xlib.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> /* Open a new graphics window. */ void gfx_open( int width, int height, const char *title ); /* Wait for the user to press a key or mouse button. */ char gfx_wait(); static Display *gfx_display=0; static Window gfx_window; static GC gfx_gc; int main() { char c; // Open a new window for drawing. gfx_open(532,276,"Font Viewer"); XDrawRectangle(gfx_display,gfx_window,gfx_gc,10,10,20,20); while(1) { // Wait for the user to press a character. c = gfx_wait(); // Quit if it is the letter q. if(c=='q') break; } return 0; } /* Open a new graphics window. */ void gfx_open( int width, int height, const char *title ) { gfx_display = XOpenDisplay(0); if(!gfx_display) { fprintf(stderr,"gfx_open: unable to open the graphics window.\n"); exit(1); } int blackColor = BlackPixel(gfx_display, DefaultScreen(gfx_display)); int whiteColor = WhitePixel(gfx_display, DefaultScreen(gfx_display)); gfx_window = XCreateSimpleWindow(gfx_display, DefaultRootWindow(gfx_display), 0, 0, width, height, 0, blackColor, blackColor); XSelectInput(gfx_display, gfx_window, StructureNotifyMask|KeyPressMask|ButtonPressMask); XMapWindow(gfx_display,gfx_window); gfx_gc = XCreateGC(gfx_display, gfx_window, 0, 0); XSetForeground(gfx_display, gfx_gc, whiteColor); // Wait for the MapNotify event for(;;) { XEvent e; XNextEvent(gfx_display, &e); if (e.type == MapNotify) break; } } /* Wait for the user to press a key or mouse button. */ char gfx_wait() { XEvent event; while(1) { XNextEvent(gfx_display,&event); if(event.type==KeyPress) { return XLookupKeysym(&event.xkey,0); } else if(event.type==ButtonPress) { return event.xbutton.button; } } }
补充吐槽
- 有人居然只用10行代码就实现了?!
内容的提问来源于stack exchange,提问作者Mike
相关产品推荐
相关产品推荐

