获取有关 X11 中离鼠标光标最近的显示器的信息
Getting information about nearest monitor to mouse cursor in X11
我需要获取有关多显示器系统中离鼠标光标位置最近的显示器的大小和坐标等信息。我以前在Windows中做过,我想知道如何在Linux X11中做到这一点。
使用下面的代码,我可以测量整个屏幕尺寸的总和,但不能单独测量每个显示器。
Screen *screen = DefaultScreenOfDisplay(DisplayHandle);
int xx = screen->width / 2 - Settings::WindowWidth / 2;
int yy = screen->height / 2 - Settings::WindowHeight / 2;
我以前的代码:
POINT mouse_position;
GetCursorPos(&mouse_position);
HMONITOR hMonitor = MonitorFromPoint(mouse_position, MONITOR_DEFAULTTOPRIMARY);
MONITORINFOEX monitor_info;
memset(&monitor_info, 0, sizeof(MONITORINFOEX));
monitor_info.cbSize = sizeof(MONITORINFOEX);
GetMonitorInfo(hMonitor, &monitor_info);
// CREATE WINDOW IN CENTER OF MONITOR //
int edge = GetSystemMetrics(SM_CXEDGE);
int fixed_frame = GetSystemMetrics(SM_CXFIXEDFRAME);
int monitor_width = monitor_info.rcMonitor.right - monitor_info.rcMonitor.left;
int monitor_height = monitor_info.rcMonitor.bottom - monitor_info.rcMonitor.top;
int xx = monitor_width / 2 - Settings::WindowWidth / 2;
int yy = monitor_height / 2 - Settings::WindowHeight / 2;
int win_x = xx - edge + monitor_info.rcMonitor.left;
int win_y = yy - fixed_frame + monitor_info.rcMonitor.top;
谢谢
如果您有两台显示器组成一个桌面,请使用 Xinerama 扩展。下面的代码选择了可用显示器中最大的屏幕,但你会明白这个想法。
#include <X11/extensions/Xinerama.h>
// By default go fullscreen
m_winWidth = DisplayWidth (m_display, m_screenNo);
m_winHeight = DisplayHeight (m_display, m_screenNo);
// But, with Xinerama, use the largest physical screen
if (XineramaIsActive (m_display))
{
int m = 0;
int pixels = 0;
XineramaScreenInfo *xs = XineramaQueryScreens (m_display, &m);
if (0 != xs && m > 0)
{
for (int i = 0; i < m; i++)
{
//printf ("%dx%d, [%d, %d] %dn", xs[i].width, xs[i].height, xs[i].x_org, xs[i].y_org, xs[i].screen_number);
if (xs[i].width * xs[i].height > pixels)
{
m_xineramaScreen = xs[i].screen_number; // pick screen
pixels = xs[i].width * xs[i].height;
m_winWidth = xs[i].width;
m_winHeight = xs[i].height;
}
}
XFree (xs);
}
}
相关文章:
- 如何在Qt C++中更改光标
- 查找最近配对时的OpenMP竞赛条件
- 如何获取pcl迭代最近点(ICP)的迭代点数?
- Win32 API 控制台光标在 WriteConsole 后不移动
- 在 macOS 中使用 C/C++ 设置鼠标光标图像
- 如何找到给定点的最近平方
- 如何更改输出的位置或光标在输出处,以便在"Code Blocks"中C++
- 将前向声明的结构替换为最近定义的结构
- 使用 C++将文本光标移动到屏幕中不可见的行
- 在 C# 中更改鼠标光标图标
- ASCII 旋转光标 (TUI) 动画出现问题
- 将鼠标悬停在图片框上时,如何显示带有 x-y 坐标的十字准线光标?
- 将光标单独移动到当前位置
- 如何获得鼠标光标连接时按下左键单击和在C++中释放左键单击时?
- 应该如何编写用于计算最近点距离的C++函数?
- 最近的邻居用nanoflann搜索
- GCC:--静态链接到pthread的整个存档配方在最近的GCC版本中停止工作
- 由于选项卡顺序的原因,鼠标光标总是得到错误的hwnd-MFC应用程序
- 向下四舍五入到五个c++的最近倍数
- 获取有关 X11 中离鼠标光标最近的显示器的信息