wzdbtmap.cpp

来自「MFC扩展编程实例」· C++ 代码 · 共 85 行

CPP
85
字号
// WzdBtmap.cpp : implementation of the CWzdBitmap class
//

#include "stdafx.h"
#include "WzdBtmap.h"

/////////////////////////////////////////////////////////////////////////////
// CWzdBitmap

IMPLEMENT_DYNAMIC(CWzdBitmap, CBitmap)


CWzdBitmap::CWzdBitmap()
{
	m_pPalette=NULL;
}

CWzdBitmap::~CWzdBitmap()
{
	if (m_pPalette)
	{
		delete m_pPalette;
	}
}

void CWzdBitmap::Capture(CRect &rect)
{
// cleanup old captures
	if (m_pPalette)
	{
		DeleteObject();
		delete m_pPalette;
	}

// save width and height
	m_nWidth=rect.Width();
	m_nHeight=rect.Height();

////////////////////////////////////////
// copy screen image into a bitmap object
////////////////////////////////////////

	// create a device context that accesses the whole screen
	CDC dcScreen;
    dcScreen.CreateDC("DISPLAY", NULL, NULL, NULL);

	// create an empty bitmap in memory
	CDC dcMem;
    dcMem.CreateCompatibleDC(&dcScreen);
    CreateCompatibleBitmap(&dcScreen, m_nWidth, m_nHeight);
    dcMem.SelectObject(this);

    // copy screen into empty bitmap
    dcMem.BitBlt(0,0,m_nWidth,m_nHeight,&dcScreen,rect.left,rect.top,SRCCOPY);

// this bitmap is worthless without the current system palette, so...

///////////////////////////////////////////
// save system palette in this bitmap's palette
///////////////////////////////////////////

	// create an empty logical palette that's big enough to hold all the colors
    int nColors = (1 << (dcScreen.GetDeviceCaps(BITSPIXEL) * 
    			dcScreen.GetDeviceCaps(PLANES)));
	LOGPALETTE *pLogPal = (LOGPALETTE *)new BYTE[
		sizeof(LOGPALETTE) + (nColors * sizeof(PALETTEENTRY))];

	// initialize this empty palette's header
	pLogPal->palVersion    = 0x300;
	pLogPal->palNumEntries = nColors;

	// load this empty palette with the system palette's colors
    ::GetSystemPaletteEntries(dcScreen.m_hDC, 0, nColors,
            (LPPALETTEENTRY)(pLogPal->palPalEntry));

	// create the palette with this logical palette
	m_pPalette=new CPalette;
	m_pPalette->CreatePalette(pLogPal);

    // clean up
	delete []pLogPal;
	dcMem.DeleteDC();
	dcScreen.DeleteDC();
}

⌨️ 快捷键说明

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