Why isn't the text showing up within my window?

65 Views Asked by At

I'm trying to create a window that has "Hello, world" printed in it, and the window is working but the text is not working or is not showing up.

I've tried changing the colors of the text and background, but to no avail. I've also made many efforts to searching this problem online but since I'm using Kali Linux its a very niece problem and as such hard to find.

#include <X11/Xlib.h>

int main()
{
    // Initialize the X display
    Display* display = XOpenDisplay(NULL);

    // Create a window
    Window window = XCreateSimpleWindow(display, DefaultRootWindow(display), 0, 0, 640, 480, 0, 0, WhitePixel(display, 0));

    // Set the window title
    XStoreName(display, window, "My Window");

    // Map the window to the screen
    XMapWindow(display, window);

    // Wait for the window to be mapped
    XEvent event;
    do {
        XNextEvent(display, &event);
    } while (event.type != MapNotify || event.xmap.event != window);

    // Create a graphics context
    GC gc = XCreateGC(display, window, 0, NULL);

    // Set the font and color
    XSetFont(display, gc, XLoadFont(display, "fixed"));
    XSetForeground(display, gc, BlackPixel(display, 0));

    // Draw the text
    XDrawString(display, window, gc, 10, 20, "Hello, World!", 13);

    // Flush the display
    XFlush(display);

    // Wait for events
    while (1)
    {
        XNextEvent(display, &event);
    }

    // Close the display
    XCloseDisplay(display);

    return 0;
}


1

There are 1 best solutions below

0
cipher On

try this :

#include <X11/Xlib.h>

int main()
{
// Initialize the X display
Display* display = XOpenDisplay(NULL);

// Create a window
Window window = XCreateSimpleWindow(display, DefaultRootWindow(display), 0, 0, 640, 480, 0, 0, WhitePixel(display, 0));

// Set the window title
XStoreName(display, window, "My Window");

// Map the window to the screen
XMapWindow(display, window);

// Wait for the window to be mapped
XEvent event;
do {
    XNextEvent(display, &event);
} while (event.type != MapNotify || event.xmap.event != window);

// Create a graphics context
GC gc = XCreateGC(display, window, 0, NULL);

// Set the font and color
XSetFont(display, gc, XLoadFont(display, "9x15"));
XSetForeground(display, gc, BlackPixel(display, 0));

// Draw the text
XDrawString(display, window, gc, 10, 20, "Hello, World!", 13);

// Flush the display
XFlush(display);

// Wait for events
while (1)
{
    XNextEvent(display, &event);
}

// Close the display
XCloseDisplay(display);

return 0;
}