५✍
if안에 안들어감;;ㅜ 본문
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109 |
#include <windows.h>
#include <time.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HINSTANCE g_hInst;
HWND hWndMain;
LPCTSTR lpszClass = TEXT("PROG");
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance
, LPSTR lpszCmdParam, int nCmdShow)
{
HWND hWnd;
MSG Message;
WNDCLASS WndClass;
g_hInst = hInstance;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hInstance = hInstance;
WndClass.lpfnWndProc = (WNDPROC)WndProc;
WndClass.lpszClassName = lpszClass;
WndClass.lpszMenuName = NULL;
WndClass.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&WndClass);
hWnd = CreateWindow(lpszClass, lpszClass, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, (HMENU)NULL, hInstance, NULL);
ShowWindow(hWnd, nCmdShow);
hWndMain = hWnd;
while (GetMessage(&Message, 0, 0, 0)) {
TranslateMessage(&Message);
DispatchMessage(&Message);
}
return Message.wParam;
}
int x, y;
TCHAR str[26];
LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
SYSTEMTIME st;
static TCHAR random[10];//
static TCHAR RandomChar;//랜덤문자
static int true_cnt;//
static int total_cnt;//
static RECT rect;//rect구조체는 (right, bottom)
switch (iMessage) {
case WM_CREATE:
srand((unsigned int)time(NULL)); //seed 조정
GetClientRect(hWnd, &rect);//윈도우의 크기를 얻어냄
SetTimer(hWnd, 1, 100, NULL);
// SendMessage(hWnd, WM_TIMER, 1, 0);
random[0] = rand() % 26 + 97;
x = rand() % rect.right;//윈도우 범위안 가로 랜덤
y = 0;//세로는 항상 0에서 떨어지는 걸로
return 0;
case WM_SIZE://사용자가 실행중 창의 크기를 바꿨을 때
GetClientRect(hWnd, &rect);
return 0;
case WM_TIMER:
y += 20; //좌표 20을 더해준다. //떨어지는 속도
if (y >= rect.bottom)//밑으로 다 내려가면
{
//아스키코드 97번 'a' //총 알파벳 수 26개
RandomChar = rand() % 26 + 97; //str에 0부터 랜덤으로 넣음
x = rand() % rect.right;
y = 0;
wsprintf(random, TEXT("%c"), RandomChar); //랜덤된 문자가 출력되어 보임
}
InvalidateRect(hWnd, NULL, TRUE); //배경을 무효한다,TRUE -> 문자사라짐
return 0;
case WM_CHAR:
if (RandomChar == wParam)
{
true_cnt++;
}
total_cnt++;
wsprintf(str, TEXT("Total: %d, True: %d"),total_cnt, true_cnt);
SendMessage(hWnd, WM_CREATE, 1, 0); //WM_CREATE로 호출
return 0;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
TextOut(hdc, x, y, random, lstrlen(random)); //랜덤한 화면에 랜덤한 알파벳을 뿌림
TextOut(hdc, 200, 500, str, lstrlen(str)); //스코어 출력
EndPaint(hWnd, &ps);
return 0;
case WM_DESTROY:
KillTimer(hWnd, 1);//SetTimer로 인한 시간 죽이기
PostQuitMessage(0);
return 0;
}
return(DefWindowProc(hWnd, iMessage, wParam, lParam));
}
|
cs |