修改显示设置的简单封装类
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; } }
相关文章推荐
- asp.net MVC+ LINQ to Entities简单添加,修改,删除,显示,子查询
- 关于修改spinner显示文字的颜色设置或者其他设置
- eclipse中svn项目文件显示修改人修改日期设置
- ECShop后台修改站内快讯数量及首页精品推荐等显示个数设置
- Android系统修改 设置->关于平板电脑(手机)->状态信息中显示的SN序列号
- 设置文本框获取焦点后清空默认值,焦点移开后若值为空则显示默认值,否则显示修改的值
- Linux命令之stty - 显示和修改终端行设置
- asp.net MVC+ LINQ to Entities简单添加,修改,删除,显示,子查询
- jsp单页面数据库查询模板:支持分页、排序、简单搜索、设置每页显示页数功能
- 如何把mysql的列修改成行显示数据简单实现
- 隐藏文件夹修改设置仍旧无法显示时的办法
- 简单设置OUTLOOK2007最小化到托盘显示
- 修改手机主题背景和强调色设置后,UI显示发生改变的问题
- Word2013中设置显示修改痕迹 便于知道错因
- 【简单修改Win XP系统网络设置技巧】
- cmd设置环境变量的、查看、修改(覆盖与添加)简单说明。
- 我使用过的Linux命令之stty - 显示和修改终端行设置
- jsp单页面数据库查询模板:支持分页、排序、简单搜索、设置每页显示页数功能
- Flex TextArea组件简单应用:文字链接,图片显示,字体设置
- 设置按修改时间显示