windows 截屏api

/*----------------------------------------
BRICKS1.C -- LoadBitmap Demonstration
(c) Charles Petzold, 1998
----------------------------------------*/

#include 
#include 
#include 
#include 
//#include "MyBitmap.h"

#include 
#include 
#include 
#include 

#include 
#include 
using namespace std;



//
class Intercept
{
public:
	//初始化 传入窗口句柄
	Intercept(HWND hwnd1,HWND hwnd2)
		:m_hSrcWnd(hwnd1),m_hDestWnd(hwnd2)
	{
	}

	//根据句柄获得窗口DC
	HDC   GetWindowsDC(HWND hwnd)
	{
		return ::GetDC(hwnd);
	}

	//获得源窗口的宽
	void GetWindowsWidth()
	{
		::RECT rect;
		::GetWindowRect(m_hSrcWnd,&rect);
		m_nCxWnd = rect.right - rect.left;

	}

	//获得源窗口的高
	void  GetWindowHeigh()
	{
		::RECT rect;
		::GetWindowRect(m_hSrcWnd,&rect);
		m_nCyWnd = rect.bottom - rect.top;

	}

	//将源窗口画到目标窗口
	void BitBltPicture()
	{
		if(m_hSrcWnd == m_hDestWnd)
		{			
			m_hScrDC  = GetWindowsDC(m_hSrcWnd);
			m_hDestDC = m_hScrDC;

//			TCHAR szBuf[MAX_PATH] = {0};
//			wsprintf(szBuf,L"if src hWnd = 0x%x\r\n", m_srchwnd );
//			OutputDebugString(szBuf);

//			memset( szBuf,0,sizeof(TCHAR)*MAX_PATH );
//			wsprintf(szBuf,L"if dest hWnd = 0x%x", m_desthwnd );
//			OutputDebugString(szBuf);

		}
		else
		{
			m_hScrDC  = GetWindowsDC(m_hSrcWnd);
			m_hDestDC = GetWindowsDC(m_hDestWnd);
		}

		GetWindowsWidth();
		GetWindowHeigh();
		m_hdcMem = ::CreateCompatibleDC(m_hScrDC);
		m_hBitmap   = ::CreateCompatibleBitmap(m_hScrDC,m_nCxWnd,m_nCyWnd); 
		::SelectObject(m_hdcMem,m_hBitmap);
		::BitBlt(m_hdcMem,0,0,m_nCxWnd,m_nCyWnd,m_hScrDC,0,0,SRCCOPY);
		::BitBlt(m_hDestDC,0,0,m_nCxWnd,m_nCyWnd,m_hdcMem,0,0,SRCCOPY);

	}

	~Intercept()
	{
		::DeleteDC(m_hdcMem);
		::DeleteDC(m_hScrDC);
		::DeleteDC(m_hDestDC);
		::DeleteObject(m_hBitmap);
	}


private:
	HWND        m_hSrcWnd;
	HWND        m_hDestWnd;
	int         m_nCxWnd;
	int         m_nCyWnd;
	HDC         m_hdcMem;
	HDC         m_hScrDC;
	HDC         m_hDestDC;
	HBITMAP     m_hBitmap;


};

/



LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
					PSTR szCmdLine, int iCmdShow)
{
	static TCHAR szAppName [] = TEXT ("Bricks1") ;
	HWND         hwnd ;
	MSG          msg ;
	WNDCLASS     wndclass ;

	wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
	wndclass.lpfnWndProc   = WndProc ;
	wndclass.cbClsExtra    = 0 ;
	wndclass.cbWndExtra    = 0 ;
	wndclass.hInstance     = hInstance ;
	wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
	wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
	//wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
	wndclass.hbrBackground = NULL ;
	wndclass.lpszMenuName  = NULL ;
	wndclass.lpszClassName = szAppName ;

	if (!RegisterClass (&wndclass))
	{
		MessageBox (NULL, TEXT ("This program requires Windows NT!"),
			szAppName, MB_ICONERROR) ;
		return 0 ;
	}

	hwnd = CreateWindow (szAppName, 
		TEXT ("LoadBitmap Demo"), 
		WS_OVERLAPPEDWINDOW, 
		CW_USEDEFAULT, CW_USEDEFAULT,
		CW_USEDEFAULT, CW_USEDEFAULT,
		NULL, 
		NULL,
		hInstance, 
		NULL
		) ;
	

	ShowWindow (hwnd, iCmdShow) ;
	UpdateWindow (hwnd) ;

	while (GetMessage (&msg, NULL, 0, 0))
	{
		TranslateMessage (&msg) ;
		DispatchMessage (&msg) ;
	}
	return msg.wParam ;
}


LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{

	static HDC            hdc;
	static HINSTANCE      hInstance ;
	PAINTSTRUCT    ps ;
	HWND hwndsrc = ::GetDesktopWindow();
	Intercept aa(hwndsrc,hwnd);


	switch (message)
	{
	case WM_CREATE:
		hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
	//	aa.BitBltPicture();
		return 0 ;

	case WM_SIZE:

		aa.BitBltPicture();
		return 0;
	case WM_PAINT:
		hdc = BeginPaint (hwnd, &ps) ;	

	//	aa.BitBltPicture();

	//	Sleep(100);
		EndPaint (hwnd, &ps) ;
		return 0;


	case WM_DESTROY:
		PostQuitMessage (0) ;
		return 0 ;
	}

	return DefWindowProc (hwnd, message, wParam, lParam) ;
}

你可能感兴趣的:(windows编程)