Click here to Skip to main content
15,893,923 members
Articles / Programming Languages / Objective C

Running State Machine Based Win32/WinCE Programs

Rate me:
Please Sign up or sign in to vote.
4.37/5 (9 votes)
17 May 20065 min read 70.9K   1.3K   35  
This article describes how to run state machine application framework based Win32/WinCE programs using the window message hooking technology. (Open source project)
// SampleEVCDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SampleEVC.h"
#include "SampleEVCDlg.h"

#include "sme.h"
#include "SrvAgent.h"
#include "PlayerEventId.h"

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

HWND g_hwndMain=NULL;

/////////////////////////////////////////////////////////////////////////////
// CSampleEVCDlg dialog

CSampleEVCDlg::CSampleEVCDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSampleEVCDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSampleEVCDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSampleEVCDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSampleEVCDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSampleEVCDlg, CDialog)
	//{{AFX_MSG_MAP(CSampleEVCDlg)
	ON_BN_CLICKED(IDC_BUTTON_POWER, OnButtonPower)
	ON_BN_CLICKED(IDC_BUTTON_PAUSE, OnButtonPause)
	ON_MESSAGE(WM_STATE_UPDATE, OnStateUpdate)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSampleEVCDlg message handlers

SME_DEC_EXT_APP_VAR(Player);
SME_DEC_EXT_APP_VAR(MediaPlay);

SME_THREAD_CONTEXT_T g_AppThreadContext;
SME_THREAD_CONTEXT_T g_AppChildThreadContext;

/*
DWORD WINAPI AppThreadProc(LPVOID lpParameter)
{
	g_AppThreadContext.nAppThreadID = GetCurrentThreadId();

	SmeInitEngine(&g_AppThreadContext); // Initialize engine

	SmeActivateApp(&SME_GET_APP_VAR(Player),NULL);
	SmeRun();
	ExitThread(0); 

	return 0;
}

DWORD WINAPI AppChildThreadProc(LPVOID lpParameter)
{
	g_AppChildThreadContext.nAppThreadID = GetCurrentThreadId();

	SmeInitEngine(&g_AppChildThreadContext); // Initialize engine

	SmeActivateApp(&SME_GET_APP_VAR(MediaPlay),NULL);
	SmeRun();
	ExitThread(0); 

	return 0;
}
*/

BOOL CSampleEVCDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	// TODO: Add extra initialization here

	g_hwndMain = m_hWnd;

	//m_hAppThread = CreateThread(NULL,0,AppThreadProc,NULL,0, NULL);
	//m_hAppChildThread = CreateThread(NULL,0,AppChildThreadProc,NULL,0, NULL);

	// Initialize engine.
	g_AppThreadContext.nAppThreadID = 0;
	SmeInitEngine(&g_AppThreadContext); 
	// Hook dialog. MfcPostExtPtrEventToWnd() will post an external event to this dialog.
	MfcHookWnd(GetSafeHwnd()); 
	SmeActivateApp(&SME_GET_APP_VAR(Player),NULL);
	SmeActivateApp(&SME_GET_APP_VAR(MediaPlay),NULL);

	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CSampleEVCDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
}


typedef struct MSG_KEY_PRESS_T
{
	unsigned short nKeyCode;
}MSG_KEY_PRESS_T;


void CSampleEVCDlg::OnButtonPower() 
{
	// TODO: Add your control notification handler code here

	/*
	MSG_KEY_PRESS_T KeyPressMsg;
	KeyPressMsg.nKeyCode = 0;
	AgtPostExtEvent(EXT_EVENT_ID_POWER, &KeyPressMsg, sizeof(MSG_KEY_PRESS_T), NULL, &g_AppThreadContext);
	*/

	MfcSendExtIntEventToWnd(EXT_EVENT_ID_POWER, 0, 0, NULL, GetSafeHwnd());

	
}

void CSampleEVCDlg::OnButtonPause() 
{
	// TODO: Add your control notification handler code here
	/*
	MSG_KEY_PRESS_T KeyPressMsg;
	KeyPressMsg.nKeyCode = 1;
	AgtPostExtEvent(EXT_EVENT_ID_PAUSE_RESUME, &KeyPressMsg, sizeof(MSG_KEY_PRESS_T), NULL, &g_AppThreadContext);
	*/
	MfcSendExtIntEventToWnd(EXT_EVENT_ID_PAUSE_RESUME, 0, 0, NULL, GetSafeHwnd());
	
}

extern TCHAR * sStateMapping[]; 

LRESULT CSampleEVCDlg::OnStateUpdate(WPARAM wParam, LPARAM lParam)
{
	SetDlgItemText(IDC_STATIC_STATE, sStateMapping[wParam]);
	return 0;
}


void CSampleEVCDlg::OnOK() 
{
	// TODO: Add extra validation here
	AgtEndAppThread(&g_AppThreadContext,NULL);
	AgtEndAppThread(&g_AppChildThreadContext,NULL);

	WaitForSingleObject(m_hAppThread,INFINITE);
	WaitForSingleObject(m_hAppChildThread,INFINITE);
	
	CDialog::OnOK();
}

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
Web Developer
China China
Jerome. (Free to speak, free to use.)

Comments and Discussions