📄 screenshotview.cpp
字号:
// ScreenShotView.cpp : implementation of the CScreenShotView class
//
#include "stdafx.h"
#include "ScreenShot.h"
#include "ScreenShotDoc.h"
#include "ScreenShotView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
HHOOK hHook=NULL;
HINSTANCE hInstance=NULL;
void SaveScreen(const char *filename)
{
HDC hScrDC, hMemDC;
int width, height;
//the pointer will save all pixel point's color value
BYTE *lpBitmapBits = NULL;
//creates a device context for the screen device
hScrDC = CreateDC(_T("DISPLAY"), NULL, NULL, NULL);
//get the screen point size
width = GetDeviceCaps(hScrDC, HORZRES);
height = GetDeviceCaps(hScrDC, VERTRES);
//creates a memory device context (DC) compatible with the screen device(hScrDC)
hMemDC = CreateCompatibleDC(hScrDC);
//initialise the struct BITMAPINFO for the bimap infomation,
//in order to use the function CreateDIBSection
//on wince os, each pixel stored by 24 bits(biBitCount=24)
//and no compressing(biCompression=0)
BITMAPINFO RGB24BitsBITMAPINFO;
ZeroMemory(&RGB24BitsBITMAPINFO, sizeof(BITMAPINFO));
RGB24BitsBITMAPINFO.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
RGB24BitsBITMAPINFO.bmiHeader.biWidth = width;
RGB24BitsBITMAPINFO.bmiHeader.biHeight = height;
RGB24BitsBITMAPINFO.bmiHeader.biPlanes = 1;
RGB24BitsBITMAPINFO.bmiHeader.biBitCount = 24;
//use the function CreateDIBSection and SelectObject
//in order to get the bimap pointer : lpBitmapBits
HBITMAP directBmp = CreateDIBSection(hMemDC, (BITMAPINFO*)&RGB24BitsBITMAPINFO,
DIB_RGB_COLORS, (void **)&lpBitmapBits, NULL, 0);
HGDIOBJ previousObject = SelectObject(hMemDC, directBmp);
// copy the screen dc to the memory dc
BitBlt(hMemDC, 0, 0, width, height, hScrDC, 0, 0, SRCCOPY);
//if you only want to get the every pixel color value,
//you can begin here and the following part of this function will be unuseful;
//the following part is in order to write file;
//bimap file header in order to write bmp file
BITMAPFILEHEADER bmBITMAPFILEHEADER;
ZeroMemory(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER));
bmBITMAPFILEHEADER.bfType = 0x4d42; //bmp
bmBITMAPFILEHEADER.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmBITMAPFILEHEADER.bfSize = bmBITMAPFILEHEADER.bfOffBits + ((width*height)*3); ///3=(24 / 8)
//write into file
FILE *mStream = NULL;
if((mStream = fopen(filename, "wb")))
{
//write bitmap file header
fwrite(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER), 1, mStream);
//write bitmap info
fwrite(&(RGB24BitsBITMAPINFO.bmiHeader), sizeof(BITMAPINFOHEADER), 1, mStream);
//write bitmap pixels data
fwrite(lpBitmapBits, 3*width*height, 1, mStream);
//close file
fclose(mStream);
}
//delete
DeleteObject(hMemDC);
DeleteObject(hScrDC);
DeleteObject(directBmp);
DeleteObject(previousObject);
}
LRESULT CALLBACK LLKeyboardHookCallbackFunction(
int nCode, WPARAM wParam, LPARAM lParam)
{
if(((((KBDLLHOOKSTRUCT*)lParam)->vkCode) == VK_UP))
{
const char filename[] = "\\screen.bmp";
SaveScreen(filename);
}
//let default processing take place
return CallNextHookEx(hHook, nCode,wParam, lParam);
}
/////////////////////////////////////////////////////////////////////////////
// CScreenShotView
IMPLEMENT_DYNCREATE(CScreenShotView, CFormView)
BEGIN_MESSAGE_MAP(CScreenShotView, CFormView)
//{{AFX_MSG_MAP(CScreenShotView)
ON_BN_CLICKED(IDC_ENABLE, OnEnable)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CScreenShotView construction/destruction
CScreenShotView::CScreenShotView()
: CFormView(CScreenShotView::IDD)
{
//{{AFX_DATA_INIT(CScreenShotView)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// TODO: add construction code here
m_bEnable=FALSE;
hInstance=(HINSTANCE)GetCurrentProcess();
}
CScreenShotView::~CScreenShotView()
{
}
void CScreenShotView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CScreenShotView)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BOOL CScreenShotView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CFormView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CScreenShotView diagnostics
#ifdef _DEBUG
void CScreenShotView::AssertValid() const
{
CFormView::AssertValid();
}
void CScreenShotView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
CScreenShotDoc* CScreenShotView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CScreenShotDoc)));
return (CScreenShotDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CScreenShotView message handlers
void CScreenShotView::OnEnable()
{
if(!m_bEnable)
{
ActivateKBHook(hInstance, LLKeyboardHookCallbackFunction);
m_bEnable=TRUE;
}
}
void CScreenShotView::OnOK()
{
DeactivateKBHook();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -