X11 为什么我无法绘制任何文字? [英] X11 Why I can't draw any text?
问题描述
我正在尝试学习 X11.这对我来说很难,因为我没有在 Linux 上使用窗口应用程序的经验.
我写了一些简单的代码,但我无法解决这个不可见的文本问题.可能一切正常,当我尝试使用 DrawRectangle 函数绘制矩形时,它正在工作.
代码如下:
I'm trying to learn X11. It's very hard to me, because I don't have experience with window applications on Linux.
I wrote some simple code and I can't resolve this not visible text problem.
Everything is working good probably, when I was trying to draw rectangle with DrawRectangle function it was working.
Here is the code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <X11/Xlib.h>
int main()
{
Display* myDisplay;
Window myWindow;
int myScreen;
GC myGC;
XEvent myEvent;
unsigned long black, white;
char* hello = "Hello world!";
XFontStruct* myFont;
if((myDisplay = XOpenDisplay(NULL)) == NULL)
{
puts("Error in conneting to X Server!");
return -1;
}
myScreen = DefaultScreen(myDisplay);
black = BlackPixel(myDisplay, myScreen);
white = WhitePixel(myDisplay, myScreen);
myWindow = XCreateSimpleWindow(myDisplay, RootWindow(myDisplay, myScreen), 0, 0, 640, 320, 5, black, white);
XSelectInput(myDisplay, myWindow, ExposureMask);
XClearWindow(myDisplay, myWindow);
XMapWindow(myDisplay, myWindow);
myGC = XCreateGC(myDisplay, myWindow, 0, 0);
XSetForeground(myDisplay, myGC, black);
XSetBackground(myDisplay, myGC, white);
myFont = XLoadQueryFont(myDisplay, "-Misc-Fixed-Medium-R-Normal--7-70-75-75-C-50-ISO10646-1");
XSetFont(myDisplay, myGC, myFont->fid);
while(1)
{
XNextEvent(myDisplay, &myEvent);
if(myEvent.type == Expose)
{
XClearWindow(myDisplay, myWindow);
// HERE I DONT KNOW WHY IT DOESNT WORK!
XDrawString(myDisplay, myWindow, myGC, 0, 0, hello, strlen(hello));
}
}
XFreeGC(myDisplay, myGC);
XDestroyWindow(myDisplay, myWindow);
XCloseDisplay(myDisplay);
return 0;
}
感谢您的帮助!
推荐答案
XLoadQueryFont
的字体路径参数错误(在我的 Linux/Debian 桌面上).使用 xlsfonts
命令检查正确的(它们都是小写的).
Your font path argument to XLoadQueryFont
is wrong (on my Linux/Debian desktop). Check with the xlsfonts
command the right ones (they are all lowercases).
与
myFont = XLoadQueryFont
(myDisplay,
"-misc-fixed-medium-r-normal--9-90-75-75-c-60-iso10646-1");
它可以更好地工作.也尝试使用 "lucidasanstypewriter-bold-14"
it could work better. Try also with "lucidasanstypewriter-bold-14"
最重要的是传递给XDrawString
的坐标是错误的.请记住,它们是文本基线的坐标.x=0, y=0 是窗口的左上角,y 向下增长,x 增长到 em>对.因此,您的文本是在窗口外绘制的,位于其顶部上方.所以 y 应该是正数并且大于字体高度.
And most importantly the coordinates passed to XDrawString
are wrong. Remember that they are the coordinates of the baseline of your text. And x=0, y=0 is the top left corner of the window, and y is growing downwards and x is growing to the right. Hence your text is drawn off-window, above its top. So y should be positive and more than the font height.
试试
XDrawString (myDisplay, myWindow, myGC, 15, 20, hello,
strlen (hello));
正如我所评论的,您需要处理很多个事件.
As I commented, you need to handle a lot more events.
我没有在 Linux 上使用窗口应用程序的经验.
I don't have experience with window applications on Linux.
并且要了解 GUI 编程,我强烈建议首先使用一些工具包像 GTK 或 Qt 或者 SDL.
And to learn about GUI programming, I strongly recommend first using some toolkit like GTK or Qt or perhaps SDL.
原始 X11 编程太难了(当您学习它时,它将过时,例如通过 Wayland),特别是因为 X11 应用程序需要 ICCCM &EWMH 兼容.请注意,整个 X11 文档需要近万页.
Raw X11 programming is too hard (and by the time you'll learn it is will be obsolete, e.g. by Wayland), in particular because an X11 application needs to be ICCCM & EWMH compliant. Notice that the entire X11 documentation requires nearly ten thousand pages.
另见https://tronche.com/gui/x/xlib/
顺便说一句,大多数 Linux GUI 应用程序都在绘制像素图客户端并将其发送到 X11 服务器.阅读合成窗口管理器.在实践中不再使用像 XDrawString
这样的绘图请求.最近的字体相关库,如 libfontconfig
、libXft
等,正在客户端端工作.
BTW, most Linux GUI applications are drawing pixmap client side and sending it to the X11 server. Read about compositing window managers. Drawing requests like XDrawString
are no more used anymore in practice. Recent font related libraries like libfontconfig
, libXft
, etc are working on the client side.
这篇关于X11 为什么我无法绘制任何文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!