ファイル名:count.dlg
count DIALOG 6, 13, 50, 35
STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "カウンター"
FONT 12, "System"
BEGIN
DEFPUSHBUTTON "カウント", IDCOUNT, 3, 15, 40, 14
END
ファイル名:count.rc
#include <windows.h>
#include "count.h"
#include "count.dlg"
ファイル名:count.h
#define IDCOUNT 101
ファイル名:count.c (第一の方法)
#define STRICT
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "count.h"
void draw(HDC hdc, int counter){
HFONT hfont, hfontOld;
char buf[260];
hfont=GetStockObject(OEM_FIXED_FONT);/*固定ピッチフォント*/
hfontOld=SelectObject(hdc, hfont);
SetTextColor(hdc, RGB(rand()%256, rand()%256, rand()%256)); /*文字色*/
SetBkColor (hdc, RGB(rand()%256, rand()%256, rand()%256)); /*背景色*/
SetBkMode(hdc, OPAQUE); /*背景色を不透明にする*/
//SetBkMode(hdc, TRANSPARENT); /*背景色を透明にする*/
sprintf(buf, "%6d", counter);
TextOut(hdc, 3, 3, buf, strlen(buf));
SelectObject(hdc, hfontOld);
DeleteObject(hfont);
}
#ifdef __cplusplus
extern "C"
#endif
BOOL FAR PASCAL count(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam){
PAINTSTRUCT ps;
HDC hdc;
static int counter=0;
switch(msg){
case WM_COMMAND:
switch((WORD)wParam) {
case IDCOUNT:
counter++;
hdc=GetDC(hDlg);
draw(hdc, counter);
ReleaseDC(hDlg, hdc);
break;
case IDCANCEL: /* システムメニューの「クローズ」 */
EndDialog( hDlg, wParam );
break;
}
break;
case WM_PAINT:
hdc = BeginPaint(hDlg, &ps);
draw(hdc, counter);
EndPaint(hDlg, &ps);
break;
case WM_INITDIALOG:
return TRUE;/* SetFocusしたらfalse */
default:
return FALSE;
}
return TRUE;
}
int PASCAL WinMain(HINSTANCE hinst, HINSTANCE hprev, LPSTR cmdline, int cmdshow){
DLGPROC lpProc = (DLGPROC)MakeProcInstance((FARPROC)count, hinst);
DialogBox(hinst, "count", NULL, lpProc);
FreeProcInstance((FARPROC)lpProc);
return 0;
}
ファイル名:count.c (第二の方法)
#define STRICT
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "count.h"
#ifdef __cplusplus
extern "C"
#endif
BOOL FAR PASCAL count(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam){
PAINTSTRUCT ps;
HDC hdc;
HFONT hfont, hfontOld;
static int counter=0;
char buf[260];
switch(msg){
case WM_COMMAND:
switch((WORD)wParam) {
case IDCOUNT:
counter++;
InvalidateRect(hDlg, NULL, TRUE);
break;
case IDCANCEL: /* システムメニューの「クローズ」 */
EndDialog( hDlg, wParam );
break;
}
break;
case WM_PAINT:
hdc = BeginPaint(hDlg, &ps);
hfont=GetStockObject(OEM_FIXED_FONT);/*固定ピッチフォント*/
hfontOld=SelectObject(hdc, hfont);
SetTextColor(hdc, RGB(rand()%256, rand()%256, rand()%256)); /*文字色*/
SetBkColor (hdc, RGB(rand()%256, rand()%256, rand()%256)); /*背景色*/
SetBkMode(hdc, OPAQUE); /*背景色を不透明にする*/
//SetBkMode(hdc, TRANSPARENT); /*背景色を透明にする*/
sprintf(buf, "%6d", counter);
TextOut(hdc, 3, 3, buf, strlen(buf));
SelectObject(hdc, hfontOld);
DeleteObject(hfont);
EndPaint(hDlg, &ps);
break;
case WM_INITDIALOG:
return TRUE;/* SetFocusしたらfalse */
default:
return FALSE;
}
return TRUE;
}
int PASCAL WinMain(HINSTANCE hinst, HINSTANCE hprev, LPSTR cmdline, int cmdshow){
DLGPROC lpProc = (DLGPROC)MakeProcInstance((FARPROC)count, hinst);
DialogBox(hinst, "count", NULL, lpProc);
FreeProcInstance((FARPROC)lpProc);
return 0;
}
ファイル名:count.def
NAME COUNT
DESCRIPTION 'COUNTER'
EXETYPE WINDOWS
STUB 'WINSTUB.EXE'
CODE PRELOAD MOVEABLE
DATA MOVEABLE PRELOAD MULTIPLE
HEAPSIZE 4096
STACKSIZE 8192
EXPORTS
COUNT @1
実行結果
戻る