C++ 获取X11中距离鼠标光标最近的监视器的信息
在多监视器系统中,我需要获取距离鼠标光标位置最近的监视器的大小和坐标等信息。我以前在Windows中做过,我想知道如何在Linux X11中做 使用下面的代码,我可以测量整个屏幕大小的总和,但不能单独测量每个监视器C++ 获取X11中距离鼠标光标最近的监视器的信息,c++,x11,C++,X11,在多监视器系统中,我需要获取距离鼠标光标位置最近的监视器的大小和坐标等信息。我以前在Windows中做过,我想知道如何在Linux X11中做 使用下面的代码,我可以测量整个屏幕大小的总和,但不能单独测量每个监视器 Screen *screen = DefaultScreenOfDisplay(DisplayHandle); int xx = screen->width / 2 - Settings::WindowWidth / 2; int yy = screen->height
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] %d\n", 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);
}
}
#包括
//默认情况下,全屏显示
m_winWidth=DisplayWidth(m_display,m_screenNo);
m_winHeight=DisplayHeight(m_display,m_screenNo);
//但是,Xinerama使用最大的物理屏幕
中频(XineramaIsActive(MU显示))
{
int m=0;
整数像素=0;
XineramaScreenInfo*xs=xineramasqueryscreens(m_显示,&m);
如果(0!=xs&&m>0)
{
for(int i=0;i像素)
{
m_xineramaScreen=xs[i]。屏幕编号;//拾取屏幕
像素=xs[i]。宽度*xs[i]。高度;
m_winWidth=xs[i]。宽度;
m_winHeight=xs[i]。高度;
}
}
XFree(xs);
}
}
这里有一个答案不同的例子(我的)。