简体   繁体   中英

How do I draw a string to a window with a C program including X11/Xlib.h?

I'm trying to create a Hello World program with X11/Xlib, but when I call XDrawString() it doesn't draw "Hello world." in the window? What do I need to change to make it do this?

EDIT: My full code is below. StackOverflow won't let me submit the question when the question description is "mostly code" so I'll also explain what the program is intended to do below the code.

Code:

#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
    //Open default display
    Display *display = XOpenDisplay(NULL);
    if(display != NULL)
    {
        //Get default screen
        Screen *screen = DefaultScreenOfDisplay(display);
        if(screen != NULL)
        {
            //Initialize window
            Window window = XCreateSimpleWindow(display, RootWindowOfScreen(screen), 0, 0, WidthOfScreen(screen), HeightOfScreen(screen), 0, BlackPixelOfScreen(screen), BlackPixelOfScreen(screen));
            XSelectInput(display, window, KeyPressMask || ExposureMask);
            XStoreName(display, window, "Hello World!");
            XMapRaised(display, window);

            //Initialize graphics context
            GC graphics_context = DefaultGCOfScreen(screen);

            //Event handling loop
            XEvent event;
            while(1)
            {
                XNextEvent(display, &event);
                if(event.type == KeyPress)
                {
                    //KeyPress event
                    if(event.xkey.keycode == XKeysymToKeycode(display, XK_Escape))
                    {
                        XDestroyWindow(display, window);
                        XCloseDisplay(display);
                        exit(EXIT_SUCCESS);
                    }
                }
                else if(event.type == Expose)
                {
                    //Expose event (window is visible)
                    char *msg = "Hello world!";
                    XDrawString(display, window, DefaultGCOfScreen(screen), 0, 0, msg, strlen(msg));
                }
            }
            XDestroyWindow(display, window);
        }
        XCloseDisplay(display);
    }
    exit(EXIT_FAILURE);
}

This code is intended to

  • Open the default display;

  • Get the default screen of this display

  • Create a simple window on the default display as a child of the root window of the screen, with x and y zeroed, width and height equal to the default width and height of the screen respectively, a 0-width border, and both the border and window background colored with the color of the black pixel of the screen

  • Select exposure events and keypresses as accepted inputs to the window

  • Store the name "Hello World!" in the window

  • Map and raise the window

  • Get default graphics context of screen

  • Handle events in indefinite infinite loop, exiting with success status when the user presses the Escape key or drawing "Hello world!"to the window on window exposure

  • Exit with failure status if opening the display has failed or the default screen of the display is NULL

You need to call XFlush and you should care of X11 coordinates.

Look into the source code of simple GUI toolkits such as FLTK . Use perhaps ltrace(1) or gdb(1) to understand their behavior. Compile FLTK from its source code using

./configure 'CXX=g++' 'CXXFLAGS=-O2 -g1' --with-abiversion \
           --enable-x11 --enable-shared

then observe (with gdb or ltrace ) the behavior of simple test examples at runtime.

See also this andthat .

It would be better to create a new GC for your app. It must have the same depth and you have to load a font. You may read this and this , both explains how to draw text.

In this other page you have example code:

black=BlackPixel(display,screen),   /* get color black */
white=WhitePixel(display, screen);  /* get color white */

/* create the Graphics Context */
gc=XCreateGC(display, window, 0,0);        

/* here is another routine to set the foreground and background
   colors _currently_ in use in the window.
*/
XSetBackground(display,gc,white);
XSetForeground(display,gc,black);

XDrawString(display,window,gc,x,y, string, strlen(string));

You should take in account that the x,y coordinates are for the font baseline, you have to add the ascent or descent of the font.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM