C Programming Column

by Al Stevens





Listing One

//   hellowin.c

#include <windows.h>

#include <Commctrl.h>

#include <Mmsystem.h>

#include "resource.h"



HINSTANCE hInst;



LRESULT CALLBACK WndProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)

{



    switch(iMsg)    {

    case WM_CREATE:

            sndPlaySound(TEXT("hellowin.wav"), SND_ASYNC);

            return 0;

        case WM_PAINT:

        {

            PAINTSTRUCT ps;

            RECT rc;

            HDC hDC = BeginPaint(hWnd, &ps);

            GetClientRect(hWnd, &rc);

            DrawText(hDC, TEXT("Hello, Windows CE"), -1, &rc,

                DT_SINGLELINE | DT_CENTER | DT_VCENTER);

            EndPaint(hWnd, &ps);

            return 0;

        }

        case WM_DESTROY:

            PostQuitMessage(0);

            return 0;

    }

    return DefWindowProc(hWnd, iMsg, wParam, lParam);

}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR szCmdLine, int iCmdShow)

{

    TCHAR szAppName[] = TEXT("HelloWin");

    HWND hWnd;

    MSG msg;

    WNDCLASS wc = {

        CS_HREDRAW | CS_VREDRAW,

        WndProc,

        0,0,

        hInstance,

        LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1)),

        0,

        (HBRUSH) GetStockObject(WHITE_BRUSH),

        0,

        szAppName

    };

    hInst = hInstance;

    RegisterClass(&wc);

    hWnd = CreateWindow(szAppName,TEXT("First Windows CE Application"),

                  WS_VISIBLE | WS_CAPTION,

                  CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,

                  0,0, hInstance, 0);

    while(GetMessage(&msg, 0, 0, 0))    {

        TranslateMessage(&msg);

        DispatchMessage(&msg);

    }

    return msg.wParam;

}





Listing Two

// Metronome.h

#include "resource.h"

class CMetronomeApp : public CWinApp

{

public:

    CMetronomeApp(LPCTSTR lpszAppName);

    //{{AFX_VIRTUAL(CMetronomeApp)

    public:

    virtual BOOL InitInstance();

    //}}AFX_VIRTUAL

    //{{AFX_MSG(CMetronomeApp)

    //}}AFX_MSG

    DECLARE_MESSAGE_MAP()

};



Listing Three

// Metronome.cpp

#include "stdafx.h"

#include "Metronome.h"

#include "MetronomeDlg.h"



BEGIN_MESSAGE_MAP(CMetronomeApp, CWinApp)

    //{{AFX_MSG_MAP(CMetronomeApp)

    //}}AFX_MSG

END_MESSAGE_MAP()

CMetronomeApp::CMetronomeApp(LPCTSTR lpszAppName)

    : CWinApp(lpszAppName)

{

}

CMetronomeApp theApp(_T("Metronome"));

BOOL CMetronomeApp::InitInstance()

{

    CMetronomeDlg dlg;

    m_pMainWnd = &dlg;

    dlg.DoModal();

    return FALSE;

}



Listing Four

// MetronomeDlg.h

class CMetronomeDlg : public CDialog

{

    bool m_bRunning;

    UINT m_nTimer;

    void StartRunning();

    void StopRunning();

public:

    CMetronomeDlg();

    //{{AFX_DATA(CMetronomeDlg)

    CStatic m_Pendulum;

    CEdit   m_Tempo;

    CButton m_StartStop;

    CSpinButtonCtrl m_TempoSpin;

    int     m_nTempo;

    //}}AFX_DATA

    //{{AFX_VIRTUAL(CMetronomeDlg)

    protected:

    virtual void DoDataExchange(CDataExchange* pDX);  // DDX/DDV support

    //}}AFX_VIRTUAL

protected:

    HICON m_hIcon;

    //{{AFX_MSG(CMetronomeDlg)

    virtual BOOL OnInitDialog();

    afx_msg void OnStart();

    //}}AFX_MSG

    DECLARE_MESSAGE_MAP()

};





Listing Five

// MetronomeDlg.cpp

#include "stdafx.h"

#include "Metronome.h"

#include "MetronomeDlg.h"



static HBITMAP hLeft;

static HBITMAP hRight;

static const int LORANGE = 60;

static const int HIRANGE = 280;

static CStatic* m_pPendulum;

static bool Ticktock = true;



CMetronomeDlg::CMetronomeDlg() : CDialog(IDD_METRONOME_DIALOG, 0)

{

   //{{AFX_DATA_INIT(CMetronomeDlg)

    m_nTempo = 120;

    //}}AFX_DATA_INIT

    m_bRunning = false;

    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

    hLeft = LoadBitmap(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDB_LEFT));

    hRight = LoadBitmap(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDB_RIGHT));

}

void CMetronomeDlg::DoDataExchange(CDataExchange* pDX)

{

    CDialog::DoDataExchange(pDX);

    //{{AFX_DATA_MAP(CMetronomeDlg)

    DDX_Control(pDX, IDC_PENDULUM, m_Pendulum);

    DDX_Control(pDX, IDC_TEMPO, m_Tempo);

    DDX_Control(pDX, IDC_START, m_StartStop);

    DDX_Control(pDX, IDC_SPIN1, m_TempoSpin);

    DDX_Text(pDX, IDC_TEMPO, m_nTempo);

    //}}AFX_DATA_MAP

}

BEGIN_MESSAGE_MAP(CMetronomeDlg, CDialog)

    //{{AFX_MSG_MAP(CMetronomeDlg)

    ON_BN_CLICKED(IDC_START, OnStart)

    //}}AFX_MSG_MAP

END_MESSAGE_MAP()



BOOL CMetronomeDlg::OnInitDialog()

{

    CDialog::OnInitDialog();

    SetIcon(m_hIcon, TRUE);             // Set big icon

    SetIcon(m_hIcon, FALSE);            // Set small icon

    CenterWindow(GetDesktopWindow());   // center to the hpc screen

    m_TempoSpin.SetRange(LORANGE,HIRANGE);

    return TRUE;

}

void CMetronomeDlg::OnStart() 

{

    if (m_bRunning)

        StopRunning();

    else

        StartRunning();

}

void CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime) 

{

    sndPlaySound(TEXT("tick.wav"), SND_ASYNC);

    Ticktock ^= true;

    m_pPendulum->SetBitmap(Ticktock ? hLeft : hRight);

}

void CMetronomeDlg::StartRunning()

{

    m_pPendulum = &m_Pendulum;

    UpdateData(true);

    if (m_nTempo >= LORANGE && m_nTempo <= HIRANGE) {

        m_bRunning = true;

        m_StartStop.SetWindowText(TEXT("Stop"));

        m_nTimer = ::SetTimer(0, 0, 60000 / m_nTempo, TimerProc);

   }

    else

        MessageBox(TEXT("Out of range"), 0, MB_ICONSTOP);

}

void CMetronomeDlg::StopRunning()

{

    m_bRunning = false;

    m_StartStop.SetWindowText(TEXT("Start"));

    ::KillTimer(0, m_nTimer);

}






