C ++和Xlib-中心窗口 [英] C++ and Xlib - center window
问题描述
我已经开始研究直接基于XLib的GUI应用程序编程,并且我试图在屏幕上创建一个居中的窗口。我不知道用于实现此目的的常用技术。我的代码(无效)是(我使用CodeBlocks)
I have started to study GUI applications programming based on XLib directly and I am trying to create a centered window on the screen. I don't know the usual technique used to achieve this. My code (which doesn't work) is this (I use CodeBlocks)
#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 <GL/glew.h>
#include <GL/freeglut.h>
int screen;
Display *display;
XSetWindowAttributes window_attributes;
Window mainwindow;
XEvent events;
int main(int argc, char** argv) {
display = XOpenDisplay(NULL);screen = DefaultScreen(display);
window_attributes.background_pixel = XWhitePixel(display, screen);
window_attributes.border_pixel = XBlackPixel(display, screen);
window_attributes.win_gravity = SouthWestGravity;
mainwindow = XCreateWindow(display,
RootWindow(display, screen),
1, 1,
600, 400,
0,
CopyFromParent,
InputOutput,
CopyFromParent,
CWBackPixel|CWBorderPixel,
&window_attributes
);
XMapWindow(display, mainwindow);
XFlush(display);
XSelectInput(display, mainwindow, ExposureMask|KeyPressMask|ButtonPressMask);
while (1) {
XNextEvent(display, &events);
switch (events.type) {
case Expose:
// Lots of code that doesn't matter for my problem
break;
}
}
return 0;
}
所以,我认为 window_attributes.win_gravity
可以解决问题,但似乎我做错了,或者肯定是与操作系统相关的问题。
感谢您的帮助!
So, I thought that window_attributes.win_gravity
would do the trick but it seems like I'm doing something wrong or it must be an OS related issue.
Thanks for your help!
更新
我已经将X(左侧位置)更改为100,将y(顶部位置)更改为100,但窗口的位置未更改。因此,我的问题是,无论窗口的位置如何,都无法更改。
UPDATE
I have changed X (left position) by 100 and y (top position) by 100 but the window's position doesn't change. So, my problem is that the window's position can't be changed, no matter what it is.
推荐答案
- 引力适用于窗口自身的内容和子元素,而不适用于其在父窗口中的位置。
- 窗口管理器可以并且确实会覆盖顶级窗口位置请求。在
XMapWindow
调用之后,使用XMoveWindow
。
- Gravity applies to the window's own content and children, not to its position in the parent window.
- Window managers can and do override top-level window position requests. Use
XMoveWindow
after theXMapWindow
call.
这篇关于C ++和Xlib-中心窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!