我很头疼这个问题。我正通过以下方式程序地改变屏幕的分辨率:
现在的问题是,窗口的大小被设置为桌面分辨率的大小,而不是在进行编程分辨率更改时设置的新分辨率大小。我期望的,也确实是我想要的,是窗口的大小与新分辨率相同。
我猜测我可能只是对这个问题有些简单的误解,但我非常感谢对此的任何想法。我不想使用像SDL这样的外部库。
谢谢!
int FindBestVideoMode(int screen, unsigned int &width, unsigned int &height)
{
int modeCount;
XF86VidModeModeInfo** modes;
if (XF86VidModeGetAllModeLines(display, screen, &modeCount, &modes))
{
int bestMode = -1;
int bestMatch = INT_MAX;
for(int i = 0; i < modeCount; i ++)
{
int match = (width - modes[i]->hdisplay) *
(width - modes[i]->hdisplay) +
(height - modes[i]->vdisplay) *
(height - modes[i]->vdisplay);
if(match < bestMatch)
{
bestMatch = match;
bestMode = i;
}
}
width = modes[bestMode]->hdisplay;
height = modes[bestMode]->vdisplay;
XFree(modes);
return bestMode;
}
return -1;
}
void SwitchVideoMode(int screen, int mode)
{
if (mode >= 0)
{
int modeCount;
XF86VidModeModeInfo** modes;
if (XF86VidModeGetAllModeLines(display, screen, &modeCount, &modes))
{
if (mode < modeCount)
{
XF86VidModeSwitchToMode(display, screen, modes[mode]);
XF86VidModeSetViewPort(display, screen, 0, 0);
XFlush(display);
}
XFree(modes);
}
}
}
void SwitchToBestVideoMode(int screen, unsigned int &width, unsigned int &height)
{
SwitchVideoMode(screen, FindBestVideoMode(screen, width, height));
}
void RestoreVideoMode(int screen)
{
auto iVideoMode = DefaultVideoModes.Find(screen);
if (iVideoMode != nullptr)
{
XF86VidModeSwitchToMode(display, screen, &iVideoMode->value);
XF86VidModeSetViewPort(display, screen, 0, 0);
XFlush(display);
}
}
这部分工作很好。然后我会使用以下方法将窗口切换到全屏模式:
XEvent e;
e.xclient.type = ClientMessage;
e.xclient.window = window;
e.xclient.message_type = _NET_WM_STATE;
e.xclient.format = 32;
e.xclient.data.l[0] = 2; // _NET_WM_STATE_TOGGLE
e.xclient.data.l[1] = XInternAtom(display, "_NET_WM_STATE_FULLSCREEN", True);
e.xclient.data.l[2] = 0; // no second property to toggle
e.xclient.data.l[3] = 1;
e.xclient.data.l[4] = 0;
XSendEvent(display, DefaultRootWindow(display), False, SubstructureRedirectMask | SubstructureNotifyMask, &e);
XMoveResizeWindow(display, window, 0, 0, width, height);
现在的问题是,窗口的大小被设置为桌面分辨率的大小,而不是在进行编程分辨率更改时设置的新分辨率大小。我期望的,也确实是我想要的,是窗口的大小与新分辨率相同。
我猜测我可能只是对这个问题有些简单的误解,但我非常感谢对此的任何想法。我不想使用像SDL这样的外部库。
谢谢!