XWindows background colors

This is a discussion on XWindows background colors within the Linux Programming forums, part of the Platform Specific Boards category; I'm still playing with X Windows programming. I can get windows on the screen, but I'm having trouble with the ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    77

    XWindows background colors

    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;
                }
            }
        }
    }

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    1. Obtain the id of the default colormap with XDefaultColormap
    2. Declare and define an XColor struct variable.
    3. Use XAllocColor to get the closest match to your XColor value from the default colormap.
    Something like:
    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;
      
        Colormap cmap;
        
        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);
        }
        
        cmap=XDefaultColormap(cdisDisplay,ciScreenNum);
        
        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;*/
                    XColor colour_bk;
                  
                    printf("Mouse button %d pressed!\n", xeEvent.xbutton.button);
                  
                    colour_bk.flags=DoRed|DoGreen|DoBlue; 
                    colour_bk.red=10000; 
                    colour_bk.green=40000; 
                    colour_bk.blue=0;
                    if (XAllocColor(cdisDisplay,cmap,&colour_bk)==0)
                    {
                      printf("XAllocColor failure.\n");
                    }
                    if (XSetWindowBackground(cdisDisplay,Handle, colour_bk.pixel)==BadGC)
                    {
                      printf("XSetBackground failure.\n");
                    }
                    XClearWindow(cdisDisplay, Handle);
                    XFlush(cdisDisplay);
                    break;
                }
                default :
                {
                    printf("Unknown event: %d (discarding)\n", xeEvent.type);
                    break;
                }
            }
        }
    }
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    77
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why I only get 8 colors out of ncurses?
    By Nazgulled in forum C Programming
    Replies: 3
    Last Post: 05-08-2007, 07:06 PM
  2. Windows background color
    By Exile in forum Windows Programming
    Replies: 2
    Last Post: 01-23-2005, 07:55 AM
  3. Detect Close of a background process
    By Schwarzhelm in forum C Programming
    Replies: 1
    Last Post: 11-05-2003, 01:46 AM
  4. Edit Controls - Colors - Messages
    By G'n'R in forum Windows Programming
    Replies: 3
    Last Post: 09-28-2003, 06:21 AM
  5. Background colors invert when resizing?
    By HisWord in forum Windows Programming
    Replies: 1
    Last Post: 10-22-2001, 12:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21