忍者ブログ
2008/08/31開設。 主に自転車生活の記事を書いております。
カレンダー
03 2024/04 05
S M T W T F S
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
最新コメント
[08/13 wtr]
[08/06 Mr.J]
[05/30 wtr]
[05/26 Mr.J]
[05/22 wtr]
最新トラックバック
プロフィール
HN:
wtr
性別:
男性
趣味:
自転車・プログラミング・釣り
ブログ内検索
アクセス解析
24
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

暇があったのでWin32APIの勉強をしました。

YSATC(Perl/tk)の事も思い出しながら、タイマーを利用したプログラムを作ってみる。
timer.c

/*
C言語によるWind32APIの学習

タイマーを利用したストップウォッチプログラム
*/

#include
#define ID_ti 100
#define ID_rs 101
#define ID_ib 102
#define ID_sb 104
#define ID_cb 103

LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
HWND hrs;

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR CmdLine, int CmdShow)
{
MSG msg;
HWND hWnd;
WNDCLASSEX wndclass;

wndclass.style = 0;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.cbSize = sizeof(WNDCLASSEX);
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wndclass.hIconSm = LoadIcon(NULL,IDI_WINLOGO);
wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = "wh";

if( !RegisterClassEx(&wndclass) ) return 0;

hWnd=CreateWindow( "wh",
"Windows example",
WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT,
CW_USEDEFAULT,
300, 200,
HWND_DESKTOP,
NULL,
hInstance,
NULL);

hrs = CreateWindow( "STATIC",
"0",
SS_RIGHT | WS_CHILD | WS_VISIBLE,
30, 60,
100, 30,
hWnd,
(HMENU)ID_rs,
hInstance,
NULL);

CreateWindow( "BUTTON",
"START",
BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE,
180, 20,
60, 30,
hWnd,
(HMENU)ID_ib,
hInstance,
NULL);
CreateWindow( "BUTTON",
"STOP",
BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE,
180, 60,
60, 30,
hWnd,
(HMENU)ID_sb,
hInstance,
NULL);
CreateWindow( "BUTTON",
"CLEAR",
BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE,
180, 100,
60, 30,
hWnd,
(HMENU)ID_cb,
hInstance,
NULL);

SetTimer(hWnd, ID_ti, 1000, NULL);

while( GetMessage(&msg, NULL, 0, 0) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

KillTimer(hWnd, ID_ti);

return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
static int cn = 0;
static int sw = 0;
char cbuf[50];

switch(iMessage)
{
case WM_COMMAND:
switch(wParam)
{
case ID_ib:
sw =1;
break;

case ID_sb:
sw=0;
break;

case ID_cb:
sw = cn = 0;
wsprintf(cbuf, "%d", cn);
SetWindowText(hrs, cbuf);
break;
}
break;

case WM_TIMER:
if(sw)
{
cn++;
wsprintf(cbuf, "%d", cn);
SetWindowText(hrs, cbuf);
}
break;

case WM_DESTROY:
PostQuitMessage(0);
break;

default:
return DefWindowProc(hWnd, iMessage, wParam, lParam);
}

return 0;
}
PR
お名前
タイトル
文字色
URL
コメント
パスワード
Vodafone絵文字 i-mode絵文字 Ezweb絵文字
Trackback URL
Copyright c ごろつきわんちゃん All Rights Reserved
Powered by ニンジャブログ  Designed by ピンキー・ローン・ピッグ
忍者ブログ / [PR]