ScreenBase实现屏幕任意区域的截取
2007-01-16 21:00
423 查看
//========================================================================
//TITLE:
// ScreenBase实现屏幕任意区域的截取
//AUTHOR:
// norains
//DATE:
// Tuesday 16-January -2007
//Environment:
// EVC4.0 + Standard SDK
//========================================================================
1.ScreenBase 源码
ScreenBase为自己写代码方便而封装的一个API类,该类的的主要功能是截取屏幕任意区域的图像,并将之保存为BMP文件.
源代码如下:
// ScreenBase.h: interface for the CScreenBase class.
#ifndef SCREENBASE_H
#define SCREENBASE_H
//-------------------------------------------------------------------
class CScreenBase
//--------------------------------------------------------------------------
#endif // !defined SCREENBASE_H
// ScreenBase.cpp: implementation of the CScreenBase class.
#include "stdafx.h"
#include "ScreenBase.h"
// Construction/Destruction
CScreenBase::CScreenBase()
CScreenBase::~CScreenBase()
//----------------------------------------------------------------------
//Description:
// Destroy the save DC
//--------------------------------------------------------------------
void CScreenBase::DestroySaveDC(LPSAVEDCINFO lpSaveDCInfo, LPSAVEBITMAPINFO lpSaveBitmapInfo)
//----------------------------------------------------------------------
//Description:
// Initialize the save DC
//--------------------------------------------------------------------
void CScreenBase::InitializeSaveDC(LPSAVEDCINFO lpSaveDCInfo, LPSAVEBITMAPINFO lpSaveBitmapInfo,const RECT *prcSave)
//----------------------------------------------------------------------
//Description:
// Save the bitmap of save dc to the file
//--------------------------------------------------------------------
void CScreenBase::SaveFile(LPSAVEBITMAPINFO lpSaveBitmapInfo, TCHAR *pszPath)
//----------------------------------------------------------------------
//Description:
// Copy the screen DC to the dest DC
//--------------------------------------------------------------------
void CScreenBase::CopyScreenDC(HDC *pHdcDest,const LPSAVEBITMAPINFO lpSaveBitmapInfo,const RECT *prcSrc)
//----------------------------------------------------------------------
//Description:
// Initialize the instance
//--------------------------------------------------------------------
void CScreenBase::Initialize(const RECT *prcScr)
//----------------------------------------------------------------------
//Description:
// Destroy all the resource
//--------------------------------------------------------------------
void CScreenBase::Destroy()
//----------------------------------------------------------------------
//Description:
// Save the bitmap of screen to the file
//--------------------------------------------------------------------
void CScreenBase::SaveFile(TCHAR *pszPath)
//----------------------------------------------------------------------
//Description:
// Copy the screen to the save DC
//--------------------------------------------------------------------
void CScreenBase::CopyScreen()
2.如何截取屏幕
采用CScreenBase类截取全屏非常简单:
首先声明一个CScreenBase类对象:
CScreenBase scrBase;
接着便是初始化:
scrBase.Initialize();
拷贝屏幕图像:
scrBase.CopyScreen();
最后,把截屏保存到文件中:
scrBase.SaveFile(TEXT("Capture.bmp"));
如果是截取屏幕的某个区域,需要在初始化的时候确定区域.
例如,我们需要截取屏幕上以(20,20)为起点,对角为(50,60)的区域,只需更改初始化代码即可:
scrBase.Initialize(&rcWnd);
3.进阶用法:将截取的区域直接显示到屏幕
因为CScreenBase类本意是作为一个截图的基本类,所以没有封装直接显示到屏幕的方法.不过,我们可以继承该类,然后返回保存区域图片的句柄,最后将其显示于屏幕即可.
派生一个ScreenPro类,用来返回所需的句柄.
class ScreenPro: public CScreenBase
//----------------------------------------------------------------------
//Description:
// On message WM_PAINT
//--------------------------------------------------------------------
void OnPaint(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(m_bScrCapture == TRUE)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//如果已经截屏,则将其显示于屏幕上
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
PAINTSTRUCT ps;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HDC hdc = BeginPaint(hWnd,&ps);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//获取截屏的区域
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
RECT rcWnd ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
rcWnd = m_ScrCapture.GetSaveRect();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//获取截屏数据保存的DC
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HDC hSaveDC = NULL;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hSaveDC = m_ScrCapture.GetSaveDC();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//绘制到屏幕
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BitBlt(hdc,rcWnd.left,rcWnd.top,rcWnd.right,rcWnd.bottom,hSaveDC,0,0,SRCCOPY);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EndPaint(hWnd,&ps);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DefWindowProc(hWnd,wMsg,wParam,lParam);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
//TITLE:
// ScreenBase实现屏幕任意区域的截取
//AUTHOR:
// norains
//DATE:
// Tuesday 16-January -2007
//Environment:
// EVC4.0 + Standard SDK
//========================================================================
1.ScreenBase 源码
ScreenBase为自己写代码方便而封装的一个API类,该类的的主要功能是截取屏幕任意区域的图像,并将之保存为BMP文件.
源代码如下:
// ScreenBase.h: interface for the CScreenBase class.
#ifndef SCREENBASE_H
#define SCREENBASE_H
//-------------------------------------------------------------------
class CScreenBase
//--------------------------------------------------------------------------
#endif // !defined SCREENBASE_H
// ScreenBase.cpp: implementation of the CScreenBase class.
#include "stdafx.h"
#include "ScreenBase.h"
// Construction/Destruction
CScreenBase::CScreenBase()
CScreenBase::~CScreenBase()
//----------------------------------------------------------------------
//Description:
// Destroy the save DC
//--------------------------------------------------------------------
void CScreenBase::DestroySaveDC(LPSAVEDCINFO lpSaveDCInfo, LPSAVEBITMAPINFO lpSaveBitmapInfo)
//----------------------------------------------------------------------
//Description:
// Initialize the save DC
//--------------------------------------------------------------------
void CScreenBase::InitializeSaveDC(LPSAVEDCINFO lpSaveDCInfo, LPSAVEBITMAPINFO lpSaveBitmapInfo,const RECT *prcSave)
//----------------------------------------------------------------------
//Description:
// Save the bitmap of save dc to the file
//--------------------------------------------------------------------
void CScreenBase::SaveFile(LPSAVEBITMAPINFO lpSaveBitmapInfo, TCHAR *pszPath)
//----------------------------------------------------------------------
//Description:
// Copy the screen DC to the dest DC
//--------------------------------------------------------------------
void CScreenBase::CopyScreenDC(HDC *pHdcDest,const LPSAVEBITMAPINFO lpSaveBitmapInfo,const RECT *prcSrc)
//----------------------------------------------------------------------
//Description:
// Initialize the instance
//--------------------------------------------------------------------
void CScreenBase::Initialize(const RECT *prcScr)
//----------------------------------------------------------------------
//Description:
// Destroy all the resource
//--------------------------------------------------------------------
void CScreenBase::Destroy()
//----------------------------------------------------------------------
//Description:
// Save the bitmap of screen to the file
//--------------------------------------------------------------------
void CScreenBase::SaveFile(TCHAR *pszPath)
//----------------------------------------------------------------------
//Description:
// Copy the screen to the save DC
//--------------------------------------------------------------------
void CScreenBase::CopyScreen()
2.如何截取屏幕
采用CScreenBase类截取全屏非常简单:
首先声明一个CScreenBase类对象:
CScreenBase scrBase;
接着便是初始化:
scrBase.Initialize();
拷贝屏幕图像:
scrBase.CopyScreen();
最后,把截屏保存到文件中:
scrBase.SaveFile(TEXT("Capture.bmp"));
如果是截取屏幕的某个区域,需要在初始化的时候确定区域.
例如,我们需要截取屏幕上以(20,20)为起点,对角为(50,60)的区域,只需更改初始化代码即可:
scrBase.Initialize(&rcWnd);
3.进阶用法:将截取的区域直接显示到屏幕
因为CScreenBase类本意是作为一个截图的基本类,所以没有封装直接显示到屏幕的方法.不过,我们可以继承该类,然后返回保存区域图片的句柄,最后将其显示于屏幕即可.
派生一个ScreenPro类,用来返回所需的句柄.
class ScreenPro: public CScreenBase
//----------------------------------------------------------------------
//Description:
// On message WM_PAINT
//--------------------------------------------------------------------
void OnPaint(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(m_bScrCapture == TRUE)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//如果已经截屏,则将其显示于屏幕上
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
PAINTSTRUCT ps;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HDC hdc = BeginPaint(hWnd,&ps);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//获取截屏的区域
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
RECT rcWnd ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
rcWnd = m_ScrCapture.GetSaveRect();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//获取截屏数据保存的DC
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HDC hSaveDC = NULL;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hSaveDC = m_ScrCapture.GetSaveDC();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//绘制到屏幕
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BitBlt(hdc,rcWnd.left,rcWnd.top,rcWnd.right,rcWnd.bottom,hSaveDC,0,0,SRCCOPY);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EndPaint(hWnd,&ps);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DefWindowProc(hWnd,wMsg,wParam,lParam);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
相关文章推荐
- ScreenBase实现屏幕任意区域的截取
- ScreenBase实现屏幕任意区域的截取
- ScreenBase实现屏幕任意区域的截取
- 屏幕区域截取图片的实现
- 用纯C#实现截取屏幕指定区域
- 用纯C#实现截取屏幕指定区域
- Unity--截取屏幕任意区域
- Unity--截取屏幕任意区域
- 用纯C#实现截取屏幕指定区域
- 使用ReadPixels方法,实现对游戏截取任意位置的屏幕,以及保存和显示截取结果
- 用纯C#实现截取屏幕指定区域
- Unity--截取屏幕任意区域
- 用纯C#实现截取屏幕指定区域
- Unity--截取屏幕任意区域
- 用Java Robot实现屏幕截取、远程监控
- 基于C#实现的屏幕指定区域截屏代码
- 基于visual c++之windows核心编程代码分析(40)实现屏幕截取
- Swift实现封装PopMenu菜单,可在屏幕任意位置弹出
- iOS截取特定区域的图片,然后拼接起来,可在图片上定制任意控件
- js点击任意区域弹出层消失实现代码