现在的位置: 首页 > 自动控制 > 工业·编程 > 正文

VC得到屏幕的当前分辨率方法(5种)

2012-08-19 22:46 工业·编程 ⁄ 共 1324字 ⁄ 字号 暂无评论

1.Windows API调用
int width = GetSystemMetrics ( SM_CXSCREEN );
int height= GetSystemMetrics ( SM_CYSCREEN );
如果想动态自适应分辨率的变化,处理WM_DISPLAYCHANGE消息.

2.获得分辨率
BOOL EnumDisplaySettings(
LPCTSTR lpszDeviceName, // display device
DWORD iModeNum, // graphics mode
LPDEVMODE lpDevMode // graphics mode settings
);

改变分辨率
LONG ChangeDisplaySettings(
LPDEVMODE lpDevMode, // graphics mode
DWORD dwflags // graphics mode options
);

3.用GetDeviceCaps

int GetDeviceCaps(
HDC hdc, // handle to the device context
int nIndex // index of capability to query
);

4.用GetDeviceCaps
HDC hdcScreen = GetDC( NULL );
int cx = GetDeviceCaps( hdcScreen, HORZRES );
int cy = GetDeviceCaps( hdcScreen, VERTRES );
DeleteObject( hdcScreen );
其中nIndex可以取:
HORZRES VERTRES 或
分别表示:
HORZRES :Width, in pixels, of the screen.
VERTRES :Height, in raster lines, of the screen.

也就是设备的分辨率了

5.得到桌面客户区的大小

(1) 最简单,使用API
RECT rc;
SystemParametersInfo(SPI_GETWORKAREA, 0, (PVOID) &rc, 0);
str.Format("%d*%d",rc.right-rc.left,rc.bottom-rc.top);
(2)间接计算得到:先用上面的3种方法得到这个屏幕的大小,再减去任务栏的大小(注意:要考虑任务栏是否隐藏的情况)
int cx = GetSystemMetrics(SM_CXSCREEN);
int cy = GetSystemMetrics(SM_CYSCREEN);

HDC hdcScreen = GetDC( NULL );
int cx = GetDeviceCaps( hdcScreen, HORZRES );
int cy = GetDeviceCaps( hdcScreen, VERTRES );
DeleteObject( hdcScreen );

任务栏:
CWnd* pTaskWnd = FindWindow( _T("Shell_TrayWnd"), NULL );
if( pTaskWnd ) pTaskWnd->ShowWindow( FALSE );
任务栏是否隐藏:
LONG lStyle = GetWindowLong( pTaskWnd->GetSafeHwnd(), GWL_STYLE );
if( lStyle & WS_VISIBLE )
{
//可见
}
else
{
//隐藏
}

给我留言

留言无头像?