#include <windows.h>
#include "timer.h"
class timer1 : public mlLib::Timer
{
private:
HWND hwnd_;
int count_;
public:
void SetTextWindow(HWND hwnd){hwnd_=hwnd;}
void OnTimer()
{
char buf[16];
itoa(count_,buf,10);
SetWindowText(hwnd_,buf);
++count_;
}
};
class timer2 : public mlLib::Timer
{
private:
HWND hwnd_;
int count_;
public:
void SetTextWindow(HWND hwnd){hwnd_=hwnd;}
void OnTimer()
{
char buf[16];
itoa(count_,buf,10);
SetWindowText(hwnd_,buf);
++count_;
}
};
extern HINSTANCE _hInstance;
const int TEXT1 = 1;
const int TEXT2 = 2;
LRESULT CALLBACK MainProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HWND text1;
static HWND text2;
static timer1 t1;
static timer2 t2;
switch (msg)
{
case WM_CREATE :
{
text1=CreateWindow("STATIC","timer 1",WS_CHILD | WS_VISIBLE,
10,10,150,19,hwnd,
(HMENU)TEXT1,_hInstance,NULL);
text2=CreateWindow("STATIC","timer 2",WS_CHILD | WS_VISIBLE,
10,50,150,19,hwnd,
(HMENU)TEXT2,_hInstance,NULL);
t1.SetTextWindow(text1);
t2.SetTextWindow(text2);
t1.Interval=1000;
t2.Interval=500;
t1.Start();
t2.Start();
return 0;
}
case WM_COMMAND :
break;
case WM_PAINT :
{
PAINTSTRUCT ps;
BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
return 0;
}
case WM_CLOSE :
{
DestroyWindow(hwnd);
break;
}
case WM_DESTROY :
{
PostQuitMessage(0);
return 0;
}
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
HWND Init(const char* Caption,int Width, int Height)
{
WNDCLASS wc;
HWND WinHwnd;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = MainProc;
wc.cbClsExtra = wc.cbWndExtra = 0;
wc.hInstance = _hInstance;
wc.hIcon = LoadIcon(_hInstance, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) COLOR_WINDOW;
wc.lpszMenuName = NULL;
wc.lpszClassName = Caption;
if (RegisterClass(&wc))
{
WinHwnd = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,
Caption, Caption,
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
CW_USEDEFAULT,CW_USEDEFAULT,
Width, Height,
HWND_DESKTOP, NULL, _hInstance, NULL);
}
else
{
WinHwnd = NULL;
}
return WinHwnd;
}
#pragma argsused
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
LPSTR lpCmdLine,int nCmdShow)
{
MSG msg;
HWND hwnd = Init("Timer test",200,140);
if (hwnd)
{
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.message;
}