我已经开始直接研究基于 XLib 的 GUI 应用程序编程,我正在尝试在屏幕上创建一个居中的窗口。我不知道用来实现这一点的常用技术。我的代码(不起作用)是这个(我使用 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
可以解决问题,但似乎我做错了什么,或者它一定是与操作系统相关的问题。
谢谢你的帮助!
更新
我已将 X(左侧位置)更改为 100,将 y(顶部位置)更改为 100,但窗口的位置没有更改。所以,我的问题是窗口的位置不能改变,不管它是什么。