I'm still playing with X Windows programming. I can get windows on the screen, but I'm having trouble with the background color. I can set the color using BlackPixel() or WhitePixel() macros, but I can't figure out how to choose say a green background.
The XSetWindowBackground() is looking for an unsigned long that represents the color. How can I arbitrarily select a color to use there?
Here is teh code I am using at the moment:
Code:#include<stdio.h> #include <X11/Xlib.h> #include <X11/Xutil.h> #include <X11/Xos.h> #include <X11/Xatom.h> int main (int ArgC, char **ArgV) { Display *cdisDisplay; int ciScreenNum; Screen *cscrScreenPtr; XWMHints *xwmhHints; XClassHint *xchClass; XTextProperty xtpWinName, xtpIconName; XSizeHints *xshSize; char *pcIconName = "Icon Name?", *pcWinName = "Window Name?", *pcProgName = "Test App"; XEvent xeEvent; Window Handle; unsigned long Pixel; cdisDisplay = XOpenDisplay(NULL); if (cdisDisplay == NULL) {//Failed to connect to an X Server printf("\nFailed to connect to the display\n"); return 0; } else { ciScreenNum = DefaultScreen(cdisDisplay); cscrScreenPtr = DefaultScreenOfDisplay(cdisDisplay); } Handle = XCreateSimpleWindow(cdisDisplay, RootWindow(cdisDisplay, ciScreenNum), 0, 0, 200, 200, 0, BlackPixel(cdisDisplay, ciScreenNum), WhitePixel(cdisDisplay, ciScreenNum)); //Allocate space for the hints xshSize = XAllocSizeHints(); xwmhHints = XAllocWMHints(); xchClass = XAllocClassHint(); xshSize->flags = PPosition | PSize; XStringListToTextProperty(&pcWinName, 1, &xtpWinName); XStringListToTextProperty(&pcIconName, 1, &xtpIconName); xwmhHints->initial_state = NormalState; xwmhHints->input = True; xwmhHints->flags = StateHint | InputHint; xchClass->res_name = pcProgName; xchClass->res_class = "Base Win"; XSetWMProperties(cdisDisplay, Handle, &xtpWinName, &xtpIconName, 0, 0, xshSize, xwmhHints, xchClass); XSelectInput(cdisDisplay, Handle, ExposureMask | KeyPressMask | ButtonPressMask | StructureNotifyMask); XMapWindow(cdisDisplay, Handle); //Show the window while (1) { XNextEvent(cdisDisplay, &xeEvent); switch (xeEvent.type) { case (ButtonPress) : { //How can I set Pixel to any color? Pixel = BlackPixel(cdisDisplay, ciScreenNum); printf("Mouse button %d pressed!\n", xeEvent.xbutton.button); XSetWindowBackground(cdisDisplay, Handle, Pixel); XClearWindow(cdisDisplay, Handle); XFlush(cdisDisplay); break; } default : { printf("Unknown event: %d (discarding)\n", xeEvent.type); break; } } } }



LinkBack URL
About LinkBacks


