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

📄 dxmodedlg.cpp

📁 C:Documents and SettingsAdministrator桌面VC++多媒体特效制作百例CHAR22DirectXMode
💻 CPP
字号:
// DXModeDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DXMode.h"
#include "DXModeDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CDXModeDlg dialog

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

void CDXModeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDXModeDlg)
	DDX_Control(pDX, IDC_DEVICES, m_CDevices);
	DDX_Control(pDX, IDC_RESMODES, m_CModes);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDXModeDlg, CDialog)
	//{{AFX_MSG_MAP(CDXModeDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_SELMODE, OnSelMode)
	ON_CBN_SELCHANGE(IDC_DEVICES, OnSelchangeDevices)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDXModeDlg DirectDraw helpers

BOOL WINAPI EnumDDrawDevices( GUID FAR *lpGUID,           
                              LPSTR lpDriverDescription,  
                              LPSTR lpDriverName,         
                              LPVOID lpContext ) {
	// Cast our pointer
	CDXModeDlg* pDialog = reinterpret_cast<CDXModeDlg*>(lpContext);
	int iIndex = pDialog->m_CDevices.AddString(lpDriverDescription);

	// Add the device to the devices combo box
	if ( iIndex != LB_ERR ) {
		// We've added the string representing the device,
		// so we now create a GUID and store it with the
		// item.
		GUID* guidDevice = NULL;
		if ( lpGUID != NULL ) {
			// If we have a device GUID, create storage
			// for a copy, then copy.
			guidDevice = new GUID;
			*guidDevice = *lpGUID; // copy
		} // if

		// Set the item's data
		pDialog->m_CDevices.SetItemData(iIndex,
					reinterpret_cast<DWORD>(guidDevice));
	} // if
	else {
		// Return an error
		return DDENUMRET_CANCEL;
	}

	return DDENUMRET_OK;
}

BOOL WINAPI EnumDeviceModes( LPDDSURFACEDESC lpDDSurfaceDesc,  
                              LPVOID lpContext )
{
	// Cast our pointer
	CDXModeDlg* pDialog = reinterpret_cast<CDXModeDlg*>(lpContext);

	// Add this mode to the listbox
	CString strMode;
	strMode.Format("%dx%dx%d, refresh %d",
				lpDDSurfaceDesc->dwWidth,
				lpDDSurfaceDesc->dwHeight,
				lpDDSurfaceDesc->ddpfPixelFormat.dwRGBBitCount,
				lpDDSurfaceDesc->dwRefreshRate);
					
	int iIndex = pDialog->m_CModes.AddString(strMode);
	if ( iIndex != LB_ERR ) {
		// Add the surface descriptor to the item's data
		LPDDSURFACEDESC lpDesc = new DDSURFACEDESC;
		memcpy(lpDesc,lpDDSurfaceDesc,sizeof(DDSURFACEDESC));
		pDialog->m_CModes.SetItemData(iIndex,
			reinterpret_cast<DWORD>(lpDesc));
	} // if
	else  {
		// Return an error
		return DDENUMRET_CANCEL;
	} // else

    return DDENUMRET_OK;
}

/////////////////////////////////////////////////////////////////////////////
// CDXModeDlg message handlers

BOOL CDXModeDlg::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
	
	// Enumerate the DirectX devices and insert into
	// the device combo box
	if ( FAILED(DirectDrawEnumerate((LPDDENUMCALLBACK)EnumDDrawDevices,  
									(LPVOID)this))) {
		AfxMessageBox("Unable to enumerate DirectX devices.",
			MB_OK|MB_ICONERROR);
		EndDialog(-1);
		return TRUE;
	}
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CDXModeDlg::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 CDXModeDlg::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 CDXModeDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CDXModeDlg::OnSelMode() 
{
	// Retrieve the surface description stored
	// in the listbox.
	int iIndex = m_CModes.GetCurSel();
	LPDDSURFACEDESC lpDesc = reinterpret_cast<LPDDSURFACEDESC>(m_CModes.GetItemData(iIndex));

	// Set the new display mode.
	ASSERT(m_pIDirectDraw2.p != NULL);
	if ( FAILED(m_pIDirectDraw2->SetDisplayMode(lpDesc->dwWidth, 
				lpDesc->dwHeight,       
				lpDesc->ddpfPixelFormat.dwRGBBitCount,
				lpDesc->dwRefreshRate,0)) ) {
		AfxMessageBox("Error setting the mode",
				MB_OK | MB_ICONERROR);
	}
}

void CDXModeDlg::OnSelchangeDevices() 
{
	// Bring up an hourglass
	CWaitCursor wc;

	// User selected a device, so enumerate the supported
	// modes for the particular device.
	int iIndex = m_CDevices.GetCurSel();
	GUID* lpGUID = reinterpret_cast<GUID*>(m_CDevices.GetItemData(iIndex));
	CComPtr<IDirectDraw> pIDirectDraw = NULL;
	if ( FAILED(DirectDrawCreate(lpGUID,&pIDirectDraw,NULL)) ) {
		AfxMessageBox("Error creating DirectDraw object",
				MB_OK | MB_ICONERROR);
		return;
	} // if

	// If we have a IDirectDraw2 interface active,
	// release it...
	m_pIDirectDraw2 = NULL;

	// We need the IDirectDraw2 interface to set the
	// mode with a refresh rate, so we must QI...
	CComQIPtr<IDirectDraw2,&IID_IDirectDraw2> pIDirectDraw2(pIDirectDraw);
	if ( pIDirectDraw2.p == NULL ) {
		// Couldn't obtain the pointer
		AfxMessageBox("Error querying for DirectDraw2 object",
				MB_OK | MB_ICONERROR);
		return;
	} // if

	// Establsh the new interface
	m_pIDirectDraw2 = pIDirectDraw2;

	// Set our cooperation mode
	if ( FAILED(m_pIDirectDraw2->SetCooperativeLevel(m_hWnd,
									DDSCL_FULLSCREEN |
									DDSCL_EXCLUSIVE |
									DDSCL_NOWINDOWCHANGES)) ) {
		AfxMessageBox("Error setting cooperative level",
				MB_OK | MB_ICONERROR);
		m_pIDirectDraw2 = NULL;
		return;
	} // if

	// Delete the DDSURFACEDESC we may have previously created
	// (associated with the modes).
	for ( iIndex = 0; iIndex < m_CModes.GetCount(); iIndex++ ) {
		// Delete the item data
		LPDDSURFACEDESC lpDesc = reinterpret_cast<LPDDSURFACEDESC>(m_CModes.GetItemData(iIndex));
		if ( lpDesc != NULL ) {
			// If we allocated memory, delete it...
			delete lpDesc;
		} // if
	} // for

	// Clear the list control
	m_CModes.ResetContent();

	// Enumerate the available modes.
	if ( FAILED(m_pIDirectDraw2->EnumDisplayModes(0,NULL,
		reinterpret_cast<LPVOID>(this),
		(LPDDENUMMODESCALLBACK)EnumDeviceModes)) ) {
		AfxMessageBox("Error enumerating modes",
				MB_OK | MB_ICONERROR);
		m_pIDirectDraw2 = NULL;
		return;
	} // if

	// Set the initial selection and enable
	// the "set" button
	m_CModes.SetCurSel(0);
	CWnd* pButton = GetDlgItem(IDC_SELMODE);
	pButton->EnableWindow(TRUE);
}

void CDXModeDlg::OnDestroy() 
{
	// Delete the GUIDs we created (associated with
	// the devices).
	for ( int iIndex = 0; iIndex < m_CDevices.GetCount(); iIndex++ ) {
		// Delete the item data
		GUID* pGUID = reinterpret_cast<GUID*>(m_CDevices.GetItemData(iIndex));
		if ( pGUID != NULL ) {
			// If we allocated memory, delete it...
			delete pGUID;
		} // if
	} // for

	// Delete the DDSURFACEDESC we created (associated with
	// the modes).
	for ( iIndex = 0; iIndex < m_CModes.GetCount(); iIndex++ ) {
		// Delete the item data
		LPDDSURFACEDESC lpDesc = reinterpret_cast<LPDDSURFACEDESC>(m_CModes.GetItemData(iIndex));
		if ( lpDesc != NULL ) {
			// If we allocated memory, delete it...
			delete lpDesc;
		} // if
	} // for

	// If we have a IDirectDraw interface active,
	// force its release...
	m_pIDirectDraw2 = NULL;

	CDialog::OnDestroy();
}

⌨️ 快捷键说明

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