windows全屏问题
Windows下如何全屏? 需注意几点:
1. 在CreateWindowEx中窗口风格设为WS_POPUP|WS_VISIBLE, 其它跟窗口模式一样;
2. 更改分辨率: 需定义一个函数:
bool ChangeScreenResolution (int width, int height, int bitsPerPixel)
{
DEVMODE dmScreenSettings;
ZeroMemory (&dmScreenSettings, sizeof (DEVMODE));
dmScreenSettings.dmSize = sizeof (DEVMODE);
dmScreenSettings.dmPelsWidth = width;
dmScreenSettings.dmPelsHeight = height;
dmScreenSettings.dmBitsPerPel = bitsPerPixel;
dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
if (ChangeDisplaySettings (&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
return false;
return true;
}
{
DEVMODE dmScreenSettings;
ZeroMemory (&dmScreenSettings, sizeof (DEVMODE));
dmScreenSettings.dmSize = sizeof (DEVMODE);
dmScreenSettings.dmPelsWidth = width;
dmScreenSettings.dmPelsHeight = height;
dmScreenSettings.dmBitsPerPel = bitsPerPixel;
dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
if (ChangeDisplaySettings (&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
return false;
return true;
}
3. 从全屏中切换到桌面其它程序(eg. 按Alt+Tab)
因为切换前后分辨率不一样,需要改分辨率;
那程序怎样知道何时切换呢? 这里必须用到WM_ACTIVE消息,代码如下:
case WM_ACTIVATE:
if (LOWORD (wParam) == WA_INACTIVE)
ChangeDisplaySettings (NULL, 0); // 如果离开本窗口,恢复原来的分辨率
else ChangeScreenResolution (640,480,16); // ...
break;
if (LOWORD (wParam) == WA_INACTIVE)
ChangeDisplaySettings (NULL, 0); // 如果离开本窗口,恢复原来的分辨率
else ChangeScreenResolution (640,480,16); // ...
break;
4. 全屏与窗口如何实时切换? 下面是我的代码(按Alt+Enter切换),但是有个问题:如果以全屏初始化程序的话,第一次按Alt+Enter看到的窗口不带标题栏和边框. 再切换的话就正常了,并且窗口模式只能出现在屏幕(0,0)位置... 不知道咋回事,也许是xp的问题?
if (fulscr == TRUE) {
/* 这些都没用~ */
int left= GetSystemMetrics(SM_CXSCREEN)/2-scr_w/2;
int top = GetSystemMetrics(SM_CYSCREEN)/2-scr_h/2;
SetWindowLong(hwindow,GWL_STYLE,STYLEW);
SetWindowPos (hwindow, HWND_NOTOPMOST,
left, top, scr_w, scr_h,
SWP_SHOWWINDOW);
ChangeDisplaySettings (NULL, 0);
fulscr = FALSE;
}
else {
SetWindowLong(hwindow,GWL_STYLE,STYLEFS);
SetWindowPos (hwindow, HWND_TOPMOST, 0, 0, scr_w, scr_h,SWP_SHOWWINDOW);
ChangeScreenResolution (scr_w, scr_h, scr_bpp);
fulscr = TRUE;
}
/* 这些都没用~ */
int left= GetSystemMetrics(SM_CXSCREEN)/2-scr_w/2;
int top = GetSystemMetrics(SM_CYSCREEN)/2-scr_h/2;
SetWindowLong(hwindow,GWL_STYLE,STYLEW);
SetWindowPos (hwindow, HWND_NOTOPMOST,
left, top, scr_w, scr_h,
SWP_SHOWWINDOW);
ChangeDisplaySettings (NULL, 0);
fulscr = FALSE;
}
else {
SetWindowLong(hwindow,GWL_STYLE,STYLEFS);
SetWindowPos (hwindow, HWND_TOPMOST, 0, 0, scr_w, scr_h,SWP_SHOWWINDOW);
ChangeScreenResolution (scr_w, scr_h, scr_bpp);
fulscr = TRUE;
}
* 如果你像我一样还碰到一个问题: 切换到桌面以后,桌面还有一个黑色的影子挥之不去怎么办? 这八成是因为窗口处于TOPMOST模式下,只要在ChangeDisplaySettings (NULL, 0)后面加一句,
SetWindowPos (hwindow, HWND_NOTOPMOST,
0, 0, 0, 0,
SWP_SHOWWINDOW);
即可.