forked from LEE-CHIEN-AN/BBBLUE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.cpp
More file actions
152 lines (129 loc) · 4.64 KB
/
Copy pathuser.cpp
File metadata and controls
152 lines (129 loc) · 4.64 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wattributes"
#include <windows.h>
#include <iostream>
using namespace std;
int cnt_UpDown = 0;
int cnt_LeftRight = 0;
int cnt_UpDown_player2 = 100;
int cnt_LeftRight_player2 = 100;
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
/// 方框
HDC hdc = GetDC(hwnd);
for(int i = 1 ; i <= 501 ; i += 2)
{
for(int j = 1 ; j <= 501 ; j += 50)
{
RECT rect = { i, j, i+2, j+2 };
FillRect(hdc, &rect, (HBRUSH)GetStockObject(BLACK_BRUSH));
}
}
for(int i = 1 ; i <= 501 ; i += 2)
{
for(int j = 1 ; j <= 501 ; j += 50)
{
RECT rect = { j, i, j+2, i+2 };
FillRect(hdc, &rect, (HBRUSH)GetStockObject(BLACK_BRUSH));
}
}
wchar_t msg[32];
switch (uMsg)
{
case WM_KEYDOWN:
{
swprintf_s(msg, L"WM_KEYDOWN: 0x%x\n", wParam);
OutputDebugStringW(msg);
cout << "WM_KEYDOWN" << endl;
HBRUSH redBrush = CreateSolidBrush(RGB(255, 0, 0));
HBRUSH blueBrush = CreateSolidBrush(RGB(0, 0, 255));
RECT rect;
switch (wParam)
{
case VK_UP:
cout << "up" << endl;
cnt_UpDown -= 50;
rect = { 2 + cnt_LeftRight, 2 + cnt_UpDown, 52 + cnt_LeftRight, 52 + cnt_UpDown }; //紅色方框
FillRect(hdc, &rect, redBrush);
break;
case VK_DOWN:
cout << "down" << endl;
cnt_UpDown += 50;
rect = { 2 + cnt_LeftRight, 2 + cnt_UpDown, 52 + cnt_LeftRight, 52 + cnt_UpDown }; //紅色方框
FillRect(hdc, &rect, redBrush);
break;
case VK_LEFT:
cout << "left" << endl;
cnt_LeftRight -= 50;
rect = { 2 + cnt_LeftRight, 2 + cnt_UpDown, 52 + cnt_LeftRight, 52 + cnt_UpDown }; //紅色方框
FillRect(hdc, &rect, redBrush);
break;
case VK_RIGHT:
cout << "right" << endl;
cnt_LeftRight += 50;
rect = { 2 + cnt_LeftRight, 2 + cnt_UpDown, 52 + cnt_LeftRight, 52 + cnt_UpDown }; //紅色方框
FillRect(hdc, &rect, redBrush);
break;
case 'W':
cout << "W pressed" << endl;
cnt_UpDown_player2 -= 50;
rect = { 2 + cnt_LeftRight_player2, 2 + cnt_UpDown_player2, 52 + cnt_LeftRight_player2, 52 + cnt_UpDown_player2 }; //紅色方框
FillRect(hdc, &rect, blueBrush);
break;
case 'S':
cout << "S pressed" << endl;
cnt_UpDown_player2 += 50;
rect = { 2 + cnt_LeftRight_player2, 2 + cnt_UpDown_player2, 52 + cnt_LeftRight_player2, 52 + cnt_UpDown_player2 }; //紅色方框
FillRect(hdc, &rect, blueBrush);
break;
case 'A':
cout << "A pressed" << endl;
cnt_LeftRight_player2 -= 50;
rect = { 2 + cnt_LeftRight_player2, 2 + cnt_UpDown_player2, 52 + cnt_LeftRight_player2, 52 + cnt_UpDown_player2 }; //紅色方框
FillRect(hdc, &rect, blueBrush);
break;
case 'D':
cout << "D pressed" << endl;
cnt_LeftRight_player2 += 50;
rect = { 2 + cnt_LeftRight_player2, 2 + cnt_UpDown_player2, 52 + cnt_LeftRight_player2, 52 + cnt_UpDown_player2 }; //紅色方框
FillRect(hdc, &rect, blueBrush);
break;
default:
break;
}
ReleaseDC(hwnd, hdc);
break;
}
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
int main()
{
// 註冊視窗
WNDCLASSW wc = {};
wc.lpfnWndProc = WindowProc;
wc.hInstance = GetModuleHandleW(NULL);
wc.lpszClassName = L"SampleWindowClass";
RegisterClassW(&wc);
// 創建視窗
HWND hwnd = CreateWindowExW(0, L"SampleWindowClass", L"Sample Window", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, GetModuleHandleW(NULL), NULL);
if (hwnd == NULL)
{
cerr << "視窗創建失敗!" << endl;
return 1;
}
// 顯示視窗
ShowWindow(hwnd, SW_SHOWDEFAULT);
// 進入消息循環
MSG msg = {};
while (GetMessageW(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// 恢復編譯器警告設定
#pragma GCC diagnostic pop
return 0;
}