Click here to Skip to main content
15,891,902 members
Articles / Desktop Programming / MFC

Fast screen, window, region and print screen capture

Rate me:
Please Sign up or sign in to vote.
1.39/5 (73 votes)
4 Dec 2002CPOL 341.2K   5K   67  
Screen Snaper is an fast screen, window, region and print screen capture
// SnaperTestView.cpp : implementation of the CSnaperTestView class
//

#include "stdafx.h"
#include "SnaperTest.h"

#include "SnaperHelperLib.h"

#include "MainFrm.h"
#include "SnaperTestDoc.h"
#include "SnaperTestView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSnaperTestView

IMPLEMENT_DYNCREATE(CSnaperTestView, CScrollView)

BEGIN_MESSAGE_MAP(CSnaperTestView, CScrollView)
	//{{AFX_MSG_MAP(CSnaperTestView)
	ON_COMMAND(ID_SNAP_DESKTOP, OnSnapDesktop)
	ON_COMMAND(ID_SNAP_WINDOW, OnSnapWindow)
	ON_COMMAND(ID_SNAP_REGION, OnSnapRegion)
	ON_WM_ERASEBKGND()
	ON_WM_DESTROY()
	ON_WM_VSCROLL()
	ON_WM_HSCROLL()
	ON_COMMAND(ID_TRAP_PRINT_SCREEN, OnTrapPrintScreen)
	ON_UPDATE_COMMAND_UI(ID_TRAP_PRINT_SCREEN, OnUpdateTrapPrintScreen)
	//}}AFX_MSG_MAP
    ON_REGISTERED_MESSAGE(UWM_PRINTSCREEN, OnPrintScreen)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSnaperTestView construction/destruction

CSnaperTestView::CSnaperTestView()
{
    pMainFrame = NULL;
    m_bTraped  = TRUE;
}

CSnaperTestView::~CSnaperTestView()
{

}

BOOL CSnaperTestView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	return CScrollView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CSnaperTestView drawing

void CSnaperTestView::OnDraw(CDC* pDC)
{
	CSnaperTestDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

    CDC dc;
    dc.CreateCompatibleDC(pDC);
    HBITMAP hOldBitmap = (HBITMAP)dc.SelectObject(pDoc->GetImage());

    pDC->BitBlt(0, 0, pDoc->GetImageSize().cx,
                      pDoc->GetImageSize().cy,
                      &dc, 0, 0, SRCCOPY);
    
    dc.SelectObject(hOldBitmap);
}



/////////////////////////////////////////////////////////////////////////////
// CSnaperTestView diagnostics

#ifdef _DEBUG
void CSnaperTestView::AssertValid() const
{
	CScrollView::AssertValid();
}

void CSnaperTestView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}

CSnaperTestDoc* CSnaperTestView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSnaperTestDoc)));
	return (CSnaperTestDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CSnaperTestView message handlers
void CSnaperTestView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();
    
    static BOOL bTrapped = FALSE;
    if (!bTrapped) {

        pMainFrame = (CMainFrame*)AfxGetMainWnd();

        TrapPrintScreen(GetSafeHwnd());
        bTrapped = TRUE;
    }

	CSnaperTestDoc* pDoc = GetDocument();
    CSize Size = pDoc->GetImageSize();
	SetScrollSizes(MM_TEXT, Size);

    CString sz;
    sz.Format("%d x %d", Size.cx, Size.cy);
    pMainFrame->SetPaneInfo(1, sz);

}

void CSnaperTestView::OnDestroy() 
{
	CScrollView::OnDestroy();
	
    if (m_bTraped)
        UnTrapPrintScreen(GetSafeHwnd());
}

void CSnaperTestView::OnSnapDesktop() 
{
    HideWindow();
    CSize Size;
    HBITMAP hImage = GetDesktopImage(&Size);
    UpdateImage(hImage, Size);
    HideWindow(FALSE);
}

void CSnaperTestView::OnSnapWindow() 
{
    HideWindow();
    CSize Size;
    HBITMAP hImage = GetWindowImage(&Size);
    UpdateImage(hImage, Size);
    HideWindow(FALSE);
}

void CSnaperTestView::OnSnapRegion() 
{
    HideWindow();
    CSize Size;
    HBITMAP hImage = GetRegionImage(&Size);
    UpdateImage(hImage, Size);
    HideWindow(FALSE);
}

void CSnaperTestView::HideWindow(BOOL bHide)
{
    pMainFrame->ShowWindow((bHide)?SW_HIDE:SW_SHOW);
}


void CSnaperTestView::UpdateImage(HBITMAP hImage, CSize Size)
{
	CSnaperTestDoc* pDoc = GetDocument();
    
    if (pDoc->SetImage(hImage)) {
        pDoc->SetImageSize(Size);
        SetScrollSizes(MM_TEXT, Size);

        CString sz;
        sz.Format("%d x %d", Size.cx, Size.cy);
        pMainFrame->SetPaneInfo(1, sz);

        Invalidate();
    }
}

HRESULT CSnaperTestView::OnPrintScreen(WPARAM wParam, LPARAM lParam)
{
    CSize Size;
    HBITMAP hImage = GetClipboardImage(&Size);
    UpdateImage(hImage, Size);

    if (pMainFrame->IsIconic())
        pMainFrame->ShowWindow(SW_RESTORE);
    else {
        pMainFrame->ShowWindow(SW_MINIMIZE);
        pMainFrame->ShowWindow(SW_RESTORE);
    }
    
    return TRUE;
}


BOOL CSnaperTestView::OnEraseBkgnd(CDC* pDC) 
{
    CBrush br;
    br.CreateHatchBrush(HS_DIAGCROSS, RGB(0, 0, 0));
    
    if (GetDocument()->GetImage())
        FillOutsideRect(pDC, &br);
    else
    {
        CRect rClient;
        GetClientRect(&rClient);
        pDC->FillRect(&rClient, &br);
    }

    br.DeleteObject();
    return TRUE;	
}


void CSnaperTestView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
    Invalidate();
    
	CScrollView::OnVScroll(nSBCode, nPos, pScrollBar);
}

void CSnaperTestView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
    Invalidate();
	
	CScrollView::OnHScroll(nSBCode, nPos, pScrollBar);
}

void CSnaperTestView::OnTrapPrintScreen() 
{
    if (!m_bTraped)
        TrapPrintScreen(GetSafeHwnd());
    else
        UnTrapPrintScreen(GetSafeHwnd());

    m_bTraped = !m_bTraped;
}

void CSnaperTestView::OnUpdateTrapPrintScreen(CCmdUI* pCmdUI) 
{
    pCmdUI->SetCheck(m_bTraped);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO
Canada Canada

Comments and Discussions