⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 resolutiondlg.cpp

📁 visual c++ 实例编程
💻 CPP
字号:
// resolutionDlg.cpp : implementation file
//

#include "stdafx.h"
#include "resolution.h"
#include "resolutionDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CResolutionDlg dialog

CResolutionDlg::CResolutionDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CResolutionDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CResolutionDlg)
	m_edit1 = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CResolutionDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CResolutionDlg)
	DDX_Control(pDX, IDC_EDIT1, m_edctrl);
	DDX_Text(pDX, IDC_EDIT1, m_edit1);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CResolutionDlg, CDialog)
	//{{AFX_MSG_MAP(CResolutionDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BNGET, OnBnget)
	ON_BN_CLICKED(IDC_BNSET, OnBnset)
	ON_BN_CLICKED(IDC_BN, OnBn)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CResolutionDlg message handlers

BOOL CResolutionDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CResolutionDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CResolutionDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CResolutionDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

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;
}


BOOL CVideoModes::GetAvailableVideoModes(CAvailableVideoModes& modes)
{
  modes.SetSize(0, 5);
  int i=0;
  DEVMODE dm;
  
  while (EnumDisplaySettings(NULL, i, &dm))
  {
    CVideoMode thismode(dm.dmBitsPerPel, dm.dmPelsWidth, 
                        dm.dmPelsHeight, dm.dmDisplayFrequency);
    modes.SetAtGrow(i, thismode);
    ++i;
  }

  modes.FreeExtra();

  return (i>0);
}


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;
  }
}


void CResolutionDlg::OnBnget() 
{
	// TODO: Add your control notification handler code here
	CAvailableVideoModes cmodes;
	CVideoModes::GetAvailableVideoModes(cmodes );
	int i,j;
	CString csRes,csTemp;
	j=cmodes.GetSize();
	for(i=0;i<j;i++)
	{
		csTemp.Format("%d",cmodes.GetAt(i).m_dwWidth);
		csRes+=csTemp+" * ";
		csTemp.Format("%d",cmodes.GetAt(i).m_dwHeight);
		csRes+=csTemp;
		csRes+=" bits ";
		csTemp.Format("%d",cmodes.GetAt(i).m_dwBitsPerPixel);
		csRes+=csTemp;
		csTemp.Format("%d",cmodes.GetAt(i).m_dwFrequency);
		csRes+=" fre ";
		csRes+=csTemp;
		csRes+="\r\n";
	}
	m_edctrl.SetWindowText(LPCTSTR(csRes));
}
CVideoMode oldmode;
void CResolutionDlg::OnBnset() 
{
	// TODO: Add your control notification handler code here
	CVideoMode mode;
	CVideoModes::GetCurrentVideoMode(oldmode);
	mode.m_dwBitsPerPixel=16;
	mode.m_dwFrequency=60;
	mode.m_dwHeight=480;
	mode.m_dwWidth=640;
	AfxMessageBox("设置为640*480 模式,5秒钟后 返回原来模式");
	CVideoModes::ChangeVideoModeTemporarily(mode);
	::Sleep(5000);
	CVideoModes::ChangeVideoModePermanently(oldmode);

}

void CResolutionDlg::OnBn() 
{
	// TODO: Add your control notification handler code here
	CVideoMode mode;
	CVideoModes::GetCurrentVideoMode(mode );
	CString csRes,csTemp;
	csTemp.Format("%d",mode.m_dwWidth);
	csRes+=csTemp+" * ";
	csTemp.Format("%d",mode.m_dwHeight);
	csRes+=csTemp;
	csRes+=" bits ";
	csTemp.Format("%d",mode.m_dwBitsPerPixel);
	csRes+=csTemp;
	csTemp.Format("%d",mode.m_dwFrequency);
	csRes+=" fre ";
	csRes+=csTemp;
	m_edctrl.SetWindowText(LPCTSTR(csRes));
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -