11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 13. Adding Special Features to Application<br />

……<br />

if(code >= 0)<br />

{<br />

lpEvent=(LPEVENTMSG)lParam;<br />

if<br />

(<br />

lpEvent->message == WM_KEYDOWN &&<br />

LOBYTE(lpEvent->paramL) == VK_F3 &&<br />

HIBYTE(GetKeyState(VK_CONTROL))<br />

)<br />

{<br />

UnsetJournalRecordHook();<br />

PostMessage(g_hWnd, uMsgFinishJournal, (WPARAM)FALSE, (LPARAM)NULL);<br />

return FALSE;<br />

}<br />

In the above code fragment, we check if the event is CTRL+F3 key stroking. If so, we remove the<br />

hook and send a message to the application’s mainframe window indicating that the recording is finished.<br />

……<br />

……<br />

if((lpEventNode=(LPEVENTNODE)malloc(sizeof(EVENTNODE))) == NULL)<br />

{<br />

UnsetJournalRecordHook();<br />

PostMessage(g_hWnd, uMsgFinishJournal, (WPARAM)FALSE, (LPARAM)NULL);<br />

return FALSE;<br />

}<br />

In the above code fragment, we try to allocate buffers for recording the event. If this is not successful,<br />

we also need to finish the recording.<br />

……<br />

……<br />

if(lpEventTail == NULL)<br />

{<br />

dwStartRecordTime=(DWORD)GetTickCount();<br />

lpEventHead=lpEventNode;<br />

}<br />

In the above code fragment, if lpeventTail is NULL, this is the first event we will record after the<br />

journal record hook has been installed. We must record the current time so that we can play back the<br />

recorded events at the rates they were generated.<br />

……<br />

else<br />

{<br />

lpEventTail->lpNextEvent=lpEventNode;<br />

}<br />

lpEventTail=lpEventNode;<br />

lpEventTail->lpNextEvent=NULL;<br />

lpEventTail->Event.message=lpEvent->message;<br />

lpEventTail->Event.paramL=lpEvent->paramL;<br />

lpEventTail->Event.paramH=lpEvent->paramH;<br />

lpEventTail->Event.time=lpEvent->time;<br />

}<br />

return FALSE;<br />

}<br />

return CallNextHookEx(g_hHookRec, code, wParam, lParam);<br />

The above code fragment shows how the event is recorded.<br />

Playing back the Recorded Events<br />

The playback is just the reverse procedure. Instead of allocating buffers and recording events, we need<br />

to analyze the recorded singly linked list and playback every event then free the buffers. When<br />

421

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!