// replayView.cpp : implementation of the CReplayView class
#include "stdafx.h"
#include "replay.h"
#include "replayDoc.h"
#include "replayView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
LRESULT CALLBACK RecHook(int code,WPARAM wParam,LPARAM lParam);
LRESULT CALLBACK PlayHook(int code,WPARAM wParam,LPARAM lParam);
HHOOK recHook,playHook;
EVENTMSG EventArray[1000];
int recordedEvent=0;
int playedEvent=0;
/////////////////////////////////////////////////////////////////////////////
// CReplayView
IMPLEMENT_DYNCREATE(CReplayView, CEditView)
BEGIN_MESSAGE_MAP(CReplayView, CEditView)
//{{AFX_MSG_MAP(CReplayView)
ON_COMMAND(ID_FUNCTION_START, OnFunctionStart)
ON_COMMAND(ID_FUNCTION_STOP, OnFunctionStop)
ON_COMMAND(ID_FUNCTION_REPLAY, OnFunctionReplay)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CEditView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CEditView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CEditView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CReplayView construction/destruction
CReplayView::CReplayView()
{
// TODO: add construction code here
}
CReplayView::~CReplayView()
{
}
BOOL CReplayView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
BOOL bPreCreated = CEditView::PreCreateWindow(cs);
cs.style &= ~(ES_AUTOHSCROLL|WS_HSCROLL); // Enable word-wrapping
return bPreCreated;
}
/////////////////////////////////////////////////////////////////////////////
// CReplayView drawing
void CReplayView::OnDraw(CDC* pDC)
{
CReplayDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CReplayView printing
BOOL CReplayView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default CEditView preparation
return CEditView::OnPreparePrinting(pInfo);
}
void CReplayView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
{
// Default CEditView begin printing.
CEditView::OnBeginPrinting(pDC, pInfo);
}
void CReplayView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
{
// Default CEditView end printing
CEditView::OnEndPrinting(pDC, pInfo);
}
/////////////////////////////////////////////////////////////////////////////
// CReplayView diagnostics
#ifdef _DEBUG
void CReplayView::AssertValid() const
{
CEditView::AssertValid();
}
void CReplayView::Dump(CDumpContext& dc) const
{
CEditView::Dump(dc);
}
CReplayDoc* CReplayView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CReplayDoc)));
return (CReplayDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CReplayView message handlers
void CReplayView::OnFunctionStart()
{
// TODO: Add your command handler code here
recordedEvent=0;
recHook=SetWindowsHookEx(WH_JOURNALRECORD,(HOOKPROC)RecHook,(HINSTANCE)AfxGetApp()->m_hInstance,0);
}
void CReplayView::OnFunctionStop()
{
// TODO: Add your command handler code here
UnhookWindowsHookEx(recHook);
}
void CReplayView::OnFunctionReplay()
{
// TODO: Add your command handler code here
playedEvent=0;
playHook=SetWindowsHookEx(WH_JOURNALPLAYBACK,(HOOKPROC)PlayHook,(HINSTANCE)AfxGetApp()->m_hInstance,0);
}
LRESULT CALLBACK RecHook(int code,WPARAM wParam,LPARAM lParam)
{
static int recOK=1;
if(code<0)
return CallNextHookEx(recHook,code,wParam,lParam);
else if(code==HC_SYSMODALON)
recOK=0;
else if(code==HC_SYSMODALOFF)
recOK=1;
else if(recOK && (code==HC_ACTION))
{
EventArray[recordedEvent]= *((PEVENTMSG)lParam);
recordedEvent++;
if(recordedEvent==1000)
{
UnhookWindowsHookEx(recHook);
}
}
return 0;
}
LRESULT CALLBACK PlayHook(int code,WPARAM wParam,LPARAM lParam)
{
static BOOL fDelay;
static int playOK=1;
if(code<0)
return CallNextHookEx(playHook,code,wParam,lParam);
else if(code==HC_SYSMODALON)
playOK=0;
else if(code==HC_SYSMODALOFF)
{
playOK=1;
}
else if(playOK && (code==HC_GETNEXT))
{
if(fDelay)
{
fDelay=FALSE;
return 50;
}
*((PEVENTMSG)lParam)=EventArray[playedEvent];
}
else if(playOK && (code==HC_SKIP))
{
fDelay=TRUE;
playedEvent++;
}
if(playedEvent>=recordedEvent)
{
UnhookWindowsHookEx(playHook);
}
return 0;
}