您的位置:首页 > 其它

修改显示设置的简单封装类

2011-03-26 17:20 267 查看
用类来对系统接口来封装, 使得使用简单,并且包含了常用的操作代码,所以在此留下脚印。

主程序: 使用封装类,代码如下
#include <iostream>
#include <stdlib.h>
#include <conio.h>
using namespace std;
#include "videomod.h"
int main(int argc, char *argv[])/
{
CVideoMode videoMode;
if(CVideoModes::GetCurrentVideoMode(videoMode))
{
cout << videoMode.m_dwBitsPerPixel << endl;
cout << videoMode.m_dwFrequency << endl;
cout << videoMode.m_dwWidth << endl;
cout << videoMode.m_dwHeight << endl;
}
_getch();
return 0;
}


封装类的具体实现。如下:

头文件:

///////////////////// Macros / Structs etc //////////////////////////
#ifndef __VIDEOMOD_H__
#define __VIDEOMOD_H__
#include <windows.h>
/////////////////////////// Classes ///////////////////////////////
//Class wrapper for a Video Mode
class CVideoMode
{
public:
//Constructors / Destructors
CVideoMode();
CVideoMode(DWORD BitsPerPixel, DWORD Width, DWORD Height, DWORD Frequency);
CVideoMode(const CVideoMode& mode);
CVideoMode& operator=(const CVideoMode& mode);
//Public variables
DWORD m_dwBitsPerPixel;
DWORD m_dwWidth;
DWORD m_dwHeight;
DWORD m_dwFrequency;
};
//Defintion of the array which contains all available video modes
//typedef CArray<CVideoMode, CVideoMode&> CAvailableVideoModes;
//Class which contains the actual functions the developer can call
//All functions are static as the class does not contain any instance
//data. Functions which return a LONG use the same return values as from
//the SDK function ChangeDisplaySettings()
class CVideoModes
{
public:
static BOOL GetCurrentVideoMode(CVideoMode& mode);
//  static BOOL GetAvailableVideoModes(CAvailableVideoModes& modes);
static LONG ChangeVideoModePermanently(const CVideoMode& mode);
static LONG ChangeVideoModeTemporarily(const CVideoMode& mode);
static LONG CanChangeVideoMode(const CVideoMode& mode);
static LONG RevertVideoModeToDefault();
protected:
static void ReportChangeVideoErrorValue(LONG lError);
static void CreateCompatibleDEVMODE(DEVMODE* pdm, DWORD BitsPerPixel, DWORD Width, DWORD Height, DWORD Frequency);
};
#endif //__VIDEOMOD_H__


代码文件:

/*
Module : VIDEOMOD.CPP
Purpose: Implementation for some handy classes which wrap access
to the EnumDisplaySettings & ChangeDisplaySettings SDK functions
*/
/////////////////////////////////  Includes  //////////////////////////////////
#include "videomod.h"
//////////////////////////////// Implementation ///////////////////////////////
CVideoMode::CVideoMode()
{
m_dwBitsPerPixel = 0;
m_dwWidth = 0;
m_dwHeight = 0;
m_dwFrequency = 0;
}
CVideoMode::CVideoMode(DWORD BitsPerPixel, DWORD Width, DWORD Height, DWORD Frequency)
{
m_dwBitsPerPixel = BitsPerPixel;
m_dwWidth = Width;
m_dwHeight = Height;
m_dwFrequency = Frequency;
}
CVideoMode::CVideoMode(const CVideoMode& mode)
{
*this = mode;
}
CVideoMode& CVideoMode::operator=(const CVideoMode& mode)
{
m_dwBitsPerPixel = mode.m_dwBitsPerPixel;
m_dwWidth = mode.m_dwWidth;
m_dwHeight = mode.m_dwHeight;
m_dwFrequency = mode.m_dwFrequency;
return *this;
}
BOOL CVideoModes::GetCurrentVideoMode(CVideoMode& mode)
{
HDC hdc = ::GetDC(NULL);  // Screen DC used to get current display settings
if (hdc == NULL)
return FALSE;
CVideoMode m(GetDeviceCaps(hdc, BITSPIXEL), GetDeviceCaps(hdc, HORZRES),
GetDeviceCaps(hdc, VERTRES), GetDeviceCaps(hdc, VREFRESH));
mode = m;
::ReleaseDC(NULL, hdc);
return TRUE;
}
LONG CVideoModes::RevertVideoModeToDefault()
{
LONG rVal = ChangeDisplaySettings(NULL, 0);
#ifdef _DEBUG
ReportChangeVideoErrorValue(rVal);
#endif
return rVal;
}
void CVideoModes::ReportChangeVideoErrorValue(LONG lError)
{
//switch (lError)
//{
//  case DISP_CHANGE_SUCCESSFUL: TRACE(_T("ChangeDisplaySettings: The settings change was successful/n")); break;
//  case DISP_CHANGE_RESTART:	   TRACE(_T("ChangeDisplaySettings: The computer must be restarted in order for the graphics mode to work/n")); break;
//  case DISP_CHANGE_BADFLAGS:	 TRACE(_T("ChangeDisplaySettings: An invalid set of flags was passed in/n")); break;
//  case DISP_CHANGE_FAILED:	   TRACE(_T("ChangeDisplaySettings: The display driver failed the specified graphics mode/n")); break;
//  case DISP_CHANGE_BADMODE:	   TRACE(_T("ChangeDisplaySettings: The graphics mode is not supported/n")); break;
//  case DISP_CHANGE_NOTUPDATED: TRACE(_T("ChangeDisplaySettings: Unable to write settings to the registry/n")); break;
//  default:                     TRACE(_T("ChangeDisplaySettings: Unexpected error value/n")); break;
//}
}
LONG CVideoModes::ChangeVideoModePermanently(const CVideoMode& mode)
{
DEVMODE dm;
CreateCompatibleDEVMODE(&dm, mode.m_dwBitsPerPixel, mode.m_dwWidth, mode.m_dwHeight, mode.m_dwFrequency);
LONG rVal = ChangeDisplaySettings(&dm, CDS_UPDATEREGISTRY);
#ifdef _DEBUG
ReportChangeVideoErrorValue(rVal);
#endif
return rVal;
}
LONG CVideoModes::ChangeVideoModeTemporarily(const CVideoMode& mode)
{
DEVMODE dm;
CreateCompatibleDEVMODE(&dm, mode.m_dwBitsPerPixel, mode.m_dwWidth, mode.m_dwHeight, mode.m_dwFrequency);
LONG rVal = ChangeDisplaySettings(&dm, 0);
#ifdef _DEBUG
ReportChangeVideoErrorValue(rVal);
#endif
return rVal;
}
LONG CVideoModes::CanChangeVideoMode(const CVideoMode& mode)
{
DEVMODE dm;
CreateCompatibleDEVMODE(&dm, mode.m_dwBitsPerPixel, mode.m_dwWidth, mode.m_dwHeight, mode.m_dwFrequency);
LONG rVal = ChangeDisplaySettings(&dm, CDS_TEST);
#ifdef _DEBUG
ReportChangeVideoErrorValue(rVal);
#endif
return rVal;
}
void CVideoModes::CreateCompatibleDEVMODE(DEVMODE* pdm, DWORD BitsPerPixel, DWORD Width, DWORD Height, DWORD Frequency)
{
ZeroMemory(pdm, sizeof(DEVMODE));
pdm->dmSize = sizeof(DEVMODE);
if (BitsPerPixel)
{
pdm->dmBitsPerPel = BitsPerPixel;
pdm->dmFields |= DM_BITSPERPEL;
}
if (Width)
{
pdm->dmPelsWidth = Width;
pdm->dmFields |= DM_PELSWIDTH;
}
if (Height)
{
pdm->dmPelsHeight = Height;
pdm->dmFields |= DM_PELSHEIGHT;
}
if (Frequency)
{
pdm->dmDisplayFrequency = Frequency;
pdm->dmFields |= DM_DISPLAYFREQUENCY;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: