Click here to Skip to main content
15,884,836 members
Articles / Desktop Programming / MFC

Barry's Screen Capture

Rate me:
Please Sign up or sign in to vote.
3.74/5 (26 votes)
11 Jan 20034 min read 271.5K   14.4K   96  
An article showing methods of screen capture
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "CapIT.h"

#include "MainFrm.h"

#include "CapITView.h"

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

#define WM_MY_TRAY_NOTIFICATION WM_USER+0

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
		ON_WM_CREATE()
	ON_MESSAGE(WM_MY_TRAY_NOTIFICATION, OnTrayNotification)

	ON_COMMAND(ID_WINDOW , OnCaptureWindow)
	ON_COMMAND(ID_DESKTOP, OnCaptureDesktop)
	ON_COMMAND(ID_CONTROL, OnCaptureControl)
	ON_COMMAND(ID_CLIENT,  OnCaptureClient)
	ON_COMMAND(ID_CLEAR_CAPTURE , OnClearCapture)
	ON_UPDATE_COMMAND_UI(ID_CLEAR_CAPTURE, OnUpdateClearCapture)
	ON_UPDATE_COMMAND_UI_RANGE(ID_DESKTOP, ID_CONTROL, OnUpdateCapture)

	ON_UPDATE_COMMAND_UI_RANGE(ID_RED, ID_POSY, OnShowCodes)


	ON_COMMAND(ID_SHOW_NORMAL, OnAppOpen)
	ON_COMMAND(ID_CLOSE, OnAppSuspend)

	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

static UINT indicators[] =
{
//	ID_SEPARATOR,           // status line indicator
	ID_RED,
	ID_GREEN,
	ID_BLUE,
	ID_POSX,
	ID_POSY,
//	ID_INDICATOR_CAPS,
//	ID_INDICATOR_NUM,
//	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	g_mbImageCaptured = FALSE;
	m_iPosX = m_iPosY = m_iRedCode = m_iGreenCode = m_iBlueCode = -1;	
	m_bShowPosition = m_bShowCodes = FALSE;
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
//	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
//		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
//		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
//	{
//		TRACE0("Failed to create toolbar\n");
//		return -1;      // fail to create
//	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
//		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
//	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
//	EnableDocking(CBRS_ALIGN_ANY);
//	DockControlBar(&m_wndToolBar);

	// Set up tray icon
	m_trayIcon.SetNotificationWnd(this, WM_MY_TRAY_NOTIFICATION);
	m_iWhichIcon = 1;
	m_trayIcon.SetIcon(IDI_CAMERA);

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers

void CMainFrame::OnShowCodes(CCmdUI *pCmdUI) 
{
	CString text;
	text = "";
	if(m_bShowCodes)
	{
		if(pCmdUI->m_nID == ID_RED)
			text.Format("Red : %d ", m_iRedCode);

		if(pCmdUI->m_nID == ID_GREEN)
			text.Format("Green : %d ", m_iGreenCode);
	
		if(pCmdUI->m_nID == ID_BLUE)
			text.Format("Blue : %d ", m_iBlueCode);
	}

	if(m_bShowPosition)
	{
		if(pCmdUI->m_nID == ID_POSX)
			text.Format("Pos-X : %d", m_iPosX);

		if(pCmdUI->m_nID == ID_POSY)
			text.Format("Pos-Y : %d", m_iPosY);
	}

	pCmdUI->SetText(text);
}


LRESULT CMainFrame::OnTrayNotification(WPARAM uID, LPARAM lEvent)
{
	if (m_bShowTrayNotifications) 
	{
		static LPCSTR MouseMessages[] = { "WM_MOUSEMOVE",
			"WM_LBUTTONDOWN", "WM_LBUTTONUP", "WM_LBUTTONDBLCLK",
			"WM_RBUTTONDOWN", "WM_RBUTTONUP", "WM_RBUTTONDBLCLK",
			"WM_MBUTTONDOWN", "WM_MBUTTONUP", "WM_MBUTTONDBLCLK" };

		CString s;
		s.Format("Tray notification: ID=%d, lEvent=0x%04x %s\r\n", 
			uID, lEvent, WM_MOUSEFIRST<=lEvent && lEvent<=WM_MOUSELAST ? 
			MouseMessages[lEvent-WM_MOUSEFIRST] : "(Unknown)");

//		m_wndEdit.SetSel(-1, -1);		// end of edit text
//		m_wndEdit.ReplaceSel(s);		// append string..
//		m_wndEdit.SendMessage(EM_SCROLLCARET); // ..and make visible
	}

	// let tray icon do default stuff
	return m_trayIcon.OnTrayNotification(uID, lEvent);
}

void CMainFrame::OnClose() 
{
//	if (m_bShutdown)
		CFrameWnd::OnClose();
//	else
//		ShowWindow(SW_HIDE);

}

void CMainFrame::OnAppOpen() 
{
	ShowWindow(SW_NORMAL);	
	SetForegroundWindow();
}

void CMainFrame::OnAppSuspend() 
{
	m_bShutdown = TRUE;		// really exit
	SendMessage(WM_CLOSE);	
}

void CMainFrame::OnCaptureWindow()
{
	CCapITView* pView = (CCapITView*)GetActiveView();
	pView->CaptureWindow();
}

void CMainFrame::OnCaptureDesktop()
{
	CCapITView* pView = (CCapITView*)GetActiveView();
	pView->CaptureDesktop();
}

void CMainFrame::OnCaptureControl()
{
	CCapITView* pView = (CCapITView*)GetActiveView();
	pView->CaptureControl();
}

void CMainFrame::OnCaptureClient()
{
	CCapITView* pView = (CCapITView*)GetActiveView();
	pView->CaptureClient();
}

void CMainFrame::OnClearCapture()
{
	CCapITView* pView = (CCapITView*)GetActiveView();
	pView->ClearCapture();
}

void CMainFrame::OnUpdateClearCapture(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(g_mbImageCaptured);	
}

void CMainFrame::OnUpdateCapture(CCmdUI* pCmdUI)
{
	pCmdUI->Enable(!g_mbImageCaptured);	
}

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.


Written By
India India
Nothing to boast about

Comments and Discussions