Cannot get XCreateSimpleWindow to open window at the right position

房东的猫 提交于 2019-12-04 11:41:47

The above solution is sort of correct, but not complete.

Creating a new top-level window on the desktop, and creating a new (child) window within the top-level window of your application use the same XCreateSimpleWindow() call, but the actual behaviour can be different.

When creating a new child window within your application you are in charge, and the origin coordinates (relative to its parent window's origin) and size you give for the new window will be honoured. In other words the window will go where you want it to.

However when creating a new top-level window on the desktop you have to deal with the pesky window manager, for example Motif, KDE, Gnome, etc. This intervenes when you create a top-level window to add borders ("decoration"), title, possibly icons, etc. More to the point it will, by default, ignore your requested origin coordinates in most cases and put the new window where it wants rather than where you asked it to put it. It is only when it has been mapped (somewhere) that you can then move it with XMoveWindow().

To avoid this you can ask, or in X11-speak "Hint to", the Window manager that "no, I want you to put the window where I ask, not where you want to put it". You do this with the following sequence:

(1) Define a XSizeHints structure. (2) Set the flags bit in this structure with a mask of what you want to specify (3) Populate the relevant arguments (4) Call XSetNormalHints() on the newly created window (before you map it).

So in C code you would do:

XSizeHints    my_hints = {0};

my_hints.flags  = PPosition | PSize;     /* I want to specify position and size */
my_hints.x      = wanted_x_origin;       /* The origin and size coords I want */
my_hints.y      = wanted_y_origin;
my_hints.width  = wanted_width;
my_hints.height = wanted_height;

XSetNormalHints(disp, new_window, &my_hints);  /* Where new_window is the new window */

Then map it and - hopefully - it will be where you want it.

I usually declare a XSizeHints first and assign x,y coordinates etc to hints.

When creating x window you could do

 XCreateSimpleWindow(display, 
                     DefaultRootWindow(display), 
                     hints.x, hints.y, 
                     hints.width,hints.height, 
                     borderWidth, 
                     blackPixel, whitePixel)

That always works for me with 100% correct windows location.

Humphrey

I had the same problem. I am just starting with X11. Maybe others with more experience can clarify why this works (and simply specifying the x, y for XCreateSimpleWindow does not).

Here's my fix:

disp is your display, win0 is your canvas_window:

XMoveWindow(disp, win0, 200, 200);

XSync (disp, FALSE);


 ..... do something .....

XMoveWindow(disp, win0, 0, 0);

XSync (disp, FALSE);

... do something

When I run this code snippet, the window moves. You need to follow the XMoveWindow by XSync so that requests (such as a move) are handled appropriately.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!