C++和Xlib - 中心窗口
C++ and Xlib - center window
我已经开始直接研究基于 XLib 的 GUI 应用程序编程,我正在尝试在屏幕上创建一个居中窗口。我不知道用于实现这一目标的常用技术。我的代码(不起作用)是这个(我使用代码块)
#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,但窗口的位置没有改变。所以,我的问题是窗口的位置不能改变,不管它是什么。
- 重力适用于窗口自己的内容和子窗口,而不是其在父窗口中的位置。 窗口
- 管理器可以并且确实覆盖顶级窗口位置请求。在
XMapWindow
呼叫后使用XMoveWindow
。
在创建窗口的函数中,您应该计算屏幕中窗口的起始 x,y 坐标:
XDisplayWidth(dpy, sreen); XDisplayHeight(dpy, sreen);
之后,您应该进行计算(例如.../2 + win.size.x ...)以使窗口居中。
相关文章:
- 如何在Qt窗口小部件中使用QStringView(或QStringRef)
- 问:如何使用C++中的按钮从窗口打开窗口
- SDL 窗口不会弹出
- 在createdialog创建的窗口中捕获用于编辑控件的OnMouseMove消息
- 如何在cpp文件之间切换窗口?在Qt中
- QuadTree只在窗口的右上角绘制
- VS Code "command":"make"与终端窗口中的命令行"make"不同
- 使用窗口中的QSlider更改背景音量
- 如何在C++中找到active directory中禁用和锁定的窗口帐户
- 处理闪烁窗口事件
- 如何通过按下第三个窗口中的按钮,将QString从一个窗口获取到另一个窗口
- C++win32 API创建多个类似视口的窗口
- SFML RenderWindow打开窗口需要很长时间
- 如何将不同的可执行文件合并到一个窗口框架中进行编码?像浏览器一样
- 获取 SFML 窗口的 HWND 和高可用性?
- 如何获取 GLFW 窗口 ID?
- GLEW/GLUT:调用init并创建一个窗口后,取消初始化并重新初始化?
- 如何检查窗口中 Row 的长度或宽度是否大于其父窗口C++的宽度
- Qt:如何使半透明,无窗,无铬窗口可拖动
- 将打开的窗口实例作为参数传递到第二个窗口中