ホームに戻る
Win32APIでタイマーを作る
/*
* タイマー
*/
#include <windows.h>
#include <stdio.h>
#define ID_B1 1000
#define ID_EDIT1 2000
#define ID_MYTIMER 3000
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
int APIENTRY WinMain(
HINSTANCE hInstance,
HINSTANCE hPreInstance,
LPSTR cmdLine,
int cmdShow)
{
WNDCLASS wd;
char *wdName = "nmtimer00";
if(!hPreInstance){
wd.style = CS_HREDRAW | CS_VREDRAW; //幅、高さの変化に対し再描画する
wd.lpfnWndProc = (WNDPROC)WndProc;
wd.cbClsExtra = 0;
wd.cbWndExtra = 0;
wd.hInstance = hInstance;
wd.hIcon = NULL;
wd.hCursor = LoadCursor(NULL, IDC_ARROW);
wd.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wd.lpszMenuName = NULL;
wd.lpszClassName = wdName;
if(!RegisterClass(&wd)){
return FALSE;
}
}
RECT rect;
rect.left = 0;
rect.top = 0;
rect.right = 300;
rect.bottom = 200;
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
HWND form00 = CreateWindowEx(
NULL,
wdName,
"nmTimer v1.00",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
rect.right - rect.left,
rect.bottom - rect.top,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(form00, cmdShow);
UpdateWindow(form00);
MSG msg;
while(GetMessage(&msg, NULL, NULL, NULL)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
// ウィンドウプロシージャ
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
// 0:停止 1:起動
static int state = 0;
// カウント 秒
static int count = 0;
static HWND hButton1;
static HWND hEdit1;
static HFONT hFont;
HINSTANCE hInst;
hInst = (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE);
switch(message){
case WM_CREATE:
{
hButton1 = CreateWindow("BUTTON", "START", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 0, 0, 0, 0, hwnd, (HMENU)ID_B1, hInst, NULL);
hEdit1 = CreateWindow("EDIT", "3:00", WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, hwnd, (HMENU)ID_EDIT1, hInst, NULL);
}
break;
case WM_SIZE:
{
RECT rect;
GetClientRect(hwnd, &rect);
int h1 = rect.bottom / 3 * 2;
int h2 = rect.bottom - h1;
hFont = CreateFont(h1,0,0,0,FW_REGULAR,FALSE,FALSE,FALSE,SHIFTJIS_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,FIXED_PITCH | FF_MODERN,(LPCTSTR)"MS ゴシック");
MoveWindow(hEdit1
,rect.left
,rect.top
,rect.right
,h1
,TRUE);
MoveWindow(hButton1
,rect.left
,h1
,rect.right
,h2
,TRUE);
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, (SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE));
}
break;
case WM_TIMER:
{
if(count > 0){
count--;
}
if(count == 0){
MessageBeep(-1);
state = 0;
SetWindowText(hEdit1, (LPTSTR)"3:00");
ShowWindow(hEdit1, TRUE);
KillTimer(hwnd, ID_MYTIMER);
SetWindowText(hButton1, (LPCTSTR)"START");
}
InvalidateRect(hwnd, NULL, FALSE);
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
if(state == 1){
RECT rect;
GetClientRect(hwnd, &rect);
HPEN hPen = CreatePen(PS_SOLID, 2, RGB(255, 255, 255));
HPEN hOldPen = (HPEN)SelectObject(hdc, (HPEN)hPen);
SelectObject(hdc, GetStockObject(WHITE_BRUSH));
Rectangle(hdc, 0, 0, rect.right, rect.bottom / 3 * 2);
SelectObject(hdc, hOldPen);
DeleteObject(hPen);
char buf[256];
sprintf(buf, "%d:%02d", count / 60, count % 60);
SelectObject(hdc, hFont);
TextOut(hdc, 0, 0, buf, strlen(buf));
}
EndPaint(hwnd, &ps);
}
break;
case WM_COMMAND:
switch(LOWORD(wParam)){
case ID_B1:
{
char buf[256];
GetWindowText(hEdit1, (LPTSTR)buf, 64);
int num1, num2;
sscanf(buf, "%d:%d", &num1, &num2);
int res = num1 * 60 + num2;
if(state == 0){
if(res >= 0){
state = 1;
count = res;
ShowWindow(hEdit1, FALSE);
SetTimer(hwnd, ID_MYTIMER, 1000, NULL);
SetWindowText(hButton1, (LPCTSTR)"STOP");
}
}
else{
state = 0;
char buf[256];
sprintf(buf, "%d:%02d", count / 60, count % 60);
SetWindowText(hEdit1, (LPTSTR)buf);
ShowWindow(hEdit1, TRUE);
KillTimer(hwnd, ID_MYTIMER);
SetWindowText(hButton1, (LPCTSTR)"START");
}
}
break;
default:
return(DefWindowProc(hwnd, message, wParam, lParam));
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return NULL;
}