Monday, May 21, 2012

Keeping a Window Always Top in Xwindow Programing

 This program shows how to create a simple window in Xwindow Programing and keeping that window Always on top of all the Windows.

/ * Program Start */ 

/*Program Name:
Purpose: Creating Xwindow & Keeping that window always top of all windows*/

#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/Xatom.h>
#include <X11/keysym.h>
#include <X11/extensions/shape.h>
#define NIL (0)

void Keep_Window_Always_Top(Display *dpy, Window w)
  Atom stateAbove;
  if (w) {
     stateAbove = XInternAtom(dpy, "_NET_WM_STATE_ABOVE", False);
     XChangeProperty(dpy, w, XInternAtom(dpy, "_NET_WM_STATE", False), XA_ATOM, 32, PropModeReplace, (unsigned char *) &stateAbove, 1);
    return ;

int main()
  char *window_name = (char*)"gem input window";
  Display *dpy = XOpenDisplay(NIL);
  int whiteColor = WhitePixel(dpy, DefaultScreen(dpy));
  Window w = XCreateSimpleWindow(dpy, RootWindow(dpy, DefaultScreen(dpy)), 0, 0,
                     230, 150, 0, whiteColor, whiteColor);
  XStoreName(dpy, w, window_name);
  Keep_Window_Always_Top(dpy, w);
  XMapWindow(dpy, w);
  XMoveWindow(dpy, w, 0, 600);
  return 0;

/ * Program End */ 

To run this program type g++ -lX11 in terminal


  1. Does this only keep the window above other windows from the same application or all windows on the desktop?

    1. It will keep created window on top of all windows :-)