MAIN.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. #include "Win32.h"
  2. #include "ID.h"
  3. #include "Function.h"
  4. #include "SharedValue.h"
  5. HWND hWnd = NULL;
  6. HFONT Font()
  7. {
  8. HFONT hFont = CreateFont(
  9. -40,
  10. 0, 0, 0,
  11. FW_NORMAL, FALSE, FALSE, FALSE,
  12. DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
  13. ANTIALIASED_QUALITY,
  14. FF_ROMAN,
  15. L"Arial"
  16. );
  17. return hFont;
  18. }
  19. //Basic info
  20. static TCHAR szWindowClass[] = _T("ufb");
  21. static TCHAR szTitle[] = TITLE;
  22. HINSTANCE hInst;
  23. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  24. int WINAPI WinMain(
  25. _In_ HINSTANCE hInstance,
  26. _In_opt_ HINSTANCE hPrevInstance,
  27. _In_ LPSTR lpCmdLine,
  28. _In_ int nCmdShow
  29. )
  30. {
  31. // Window config
  32. WNDCLASSEX wcex;
  33. wcex.cbSize = sizeof(WNDCLASSEX);
  34. wcex.style = CS_HREDRAW | CS_VREDRAW;
  35. wcex.lpfnWndProc = WndProc;
  36. wcex.cbClsExtra = 0;
  37. wcex.cbWndExtra = 0;
  38. wcex.hInstance = hInstance;
  39. wcex.hIcon = LoadIcon(wcex.hInstance, IDI_APPLICATION);
  40. wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  41. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  42. wcex.lpszMenuName = NULL;
  43. wcex.lpszClassName = szWindowClass;
  44. wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION);
  45. Err(RegisterClassEx(&wcex),
  46. _T("Call to RegisterClassEx failed!"));
  47. //Create window
  48. hInst = hInstance;
  49. hWnd = CreateWindowEx(
  50. WS_EX_OVERLAPPEDWINDOW,
  51. szWindowClass, szTitle,
  52. WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
  53. CW_USEDEFAULT, CW_USEDEFAULT,
  54. 610, 600, NULL, NULL,
  55. hInstance, NULL
  56. );
  57. Err(hWnd,
  58. _T("Call to CreateWindow failed!"));
  59. //Create controls
  60. HWND hLabel_1 = CreateWindow(
  61. L"STATIC", L"基础操作",
  62. WS_VISIBLE | WS_CHILD,
  63. 30, 55, 90, 30,
  64. hWnd, NULL,
  65. (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), NULL
  66. );
  67. HWND hStart = CreateWindow(
  68. L"BUTTON", L"启动",
  69. WS_VISIBLE | WS_CHILD,
  70. 30, 100, 70, 40,
  71. hWnd, (HMENU)ID_START,
  72. (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), NULL
  73. );
  74. HWND hStop = CreateWindow(
  75. L"BUTTON", L"停止",
  76. WS_VISIBLE | WS_CHILD,
  77. 120, 100, 70, 40,
  78. hWnd, (HMENU)ID_STOP,
  79. (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), NULL
  80. );
  81. HWND hStopForce = CreateWindow(
  82. L"BUTTON", L"强行停止",
  83. WS_VISIBLE | WS_CHILD,
  84. 210, 100, 110, 40,
  85. hWnd, (HMENU)ID_STOP_FORCE,
  86. (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), NULL
  87. );
  88. HWND hLabel_2 = CreateWindow(
  89. L"STATIC", L"命令输入",
  90. WS_VISIBLE | WS_CHILD,
  91. 30, 155, 90, 30,
  92. hWnd, NULL,
  93. (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), NULL
  94. );
  95. HWND hCmdKey = CreateWindow(
  96. L"EDIT", NULL,
  97. WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT | ES_MULTILINE | ES_AUTOHSCROLL,
  98. 30, 200, 350, 40,
  99. hWnd, (HMENU)ID_CMD_KEY,
  100. (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), NULL
  101. );
  102. HWND hCmdSend = CreateWindow(
  103. L"BUTTON", L"执行",
  104. WS_VISIBLE | WS_CHILD,
  105. 400, 200, 70, 40,
  106. hWnd, (HMENU)ID_CMD_OK,
  107. (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), NULL
  108. );
  109. HWND hCmdClear = CreateWindow(
  110. L"BUTTON", L"清空",
  111. WS_VISIBLE | WS_CHILD,
  112. 490, 200, 70, 40,
  113. hWnd, (HMENU)ID_CMD_CLEAR,
  114. (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), NULL
  115. );
  116. HWND hLabel_3 = CreateWindow(
  117. L"STATIC", L"时间和天气",
  118. WS_VISIBLE | WS_CHILD,
  119. 30, 255, 110, 30,
  120. hWnd, NULL,
  121. (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), NULL
  122. );
  123. HWND hFCweather = CreateWindow(
  124. L"BUTTON", L"天气控制",
  125. WS_VISIBLE | WS_CHILD,
  126. 30, 300, 110, 40,
  127. hWnd, (HMENU)ID_FC_Weather,
  128. (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), NULL
  129. );
  130. HWND hFCtime = CreateWindow(
  131. L"BUTTON", L"时间控制",
  132. WS_VISIBLE | WS_CHILD,
  133. 160, 300, 110, 40,
  134. hWnd, (HMENU)ID_FC_Time,
  135. (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), NULL
  136. );
  137. HWND hFSwt = CreateWindow(
  138. L"BUTTON", L"查询时间和天气",
  139. WS_VISIBLE | WS_CHILD,
  140. 290, 300, 180, 40,
  141. hWnd, (HMENU)ID_FS_WT,
  142. (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), NULL
  143. );
  144. HWND hLabel_4 = CreateWindow(
  145. L"STATIC", L"在线玩家操作",
  146. WS_VISIBLE | WS_CHILD,
  147. 30, 365, 130, 30,
  148. hWnd, NULL,
  149. (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), NULL
  150. );
  151. HWND hFClevel = CreateWindow(
  152. L"BUTTON", L"打开面板",
  153. WS_VISIBLE | WS_CHILD,
  154. 30, 410, 110, 40,
  155. hWnd, (HMENU)ID_FC_PLAYER,
  156. (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), NULL
  157. );
  158. HWND hFSlist = CreateWindow(
  159. L"BUTTON", L"查询在线玩家",
  160. WS_VISIBLE | WS_CHILD,
  161. 160, 410, 150, 40,
  162. hWnd, (HMENU)ID_FS_LIST,
  163. (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), NULL
  164. );
  165. //Font
  166. HFONT hFont = CreateFont(
  167. -40,
  168. 0, 0, 0,
  169. FW_NORMAL, FALSE, FALSE, FALSE,
  170. DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
  171. ANTIALIASED_QUALITY,
  172. FF_ROMAN,
  173. L"Arial"
  174. );
  175. SendMessage(hWnd, WM_SETFONT, (WPARAM)hFont, TRUE);
  176. //Visibility & Loop
  177. ShowWindow(hWnd,
  178. nCmdShow);
  179. UpdateWindow(hWnd);
  180. MSG msg;
  181. while (GetMessage(&msg, NULL, 0, 0))
  182. {
  183. TranslateMessage(&msg);
  184. DispatchMessage(&msg);
  185. }
  186. return (int)msg.wParam;
  187. }
  188. //Behavior of controls
  189. InitHW(hTime); InitHW(hWeather); InitHW(hLog); InitHW(hPlayer);
  190. LRESULT CALLBACK TimeProc(HWND hWnd1, UINT msg, WPARAM wParam, LPARAM lParam);
  191. LRESULT CALLBACK WeatherProc(HWND hWnd1, UINT msg, WPARAM wParam, LPARAM lParam);
  192. LRESULT CALLBACK LogProc(HWND hWnd1, UINT msg, WPARAM wParam, LPARAM lParam);
  193. LRESULT CALLBACK PlayerProc(HWND hWnd1, UINT msg, WPARAM wParam, LPARAM lParam);
  194. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  195. {
  196. PAINTSTRUCT ps;
  197. HDC hdc;
  198. TCHAR greeting[] = _T("by xksyu2021\nYou must obey / 您必须遵守 Minecraft EULA.");
  199. switch (message)
  200. {
  201. case WM_CREATE:
  202. {
  203. ClearLog();
  204. //StartBDS();
  205. HINSTANCE HI_Log = NULL;
  206. hLog = CreateLogWindow(
  207. _T("日志"),
  208. 1000, 690,
  209. LogProc, _T("log"), HI_Log);
  210. Log(GetDlgItem(hLog, ID_LOG));
  211. break;
  212. }
  213. case WM_PAINT:
  214. hdc = BeginPaint(hWnd, &ps);
  215. TextOut(hdc,
  216. 5, 5,
  217. greeting, _tcslen(greeting));
  218. EndPaint(hWnd, &ps);
  219. break;
  220. case WM_COMMAND:
  221. {
  222. WORD wmId = LOWORD(wParam);
  223. switch (wmId)
  224. {
  225. case ID_START:
  226. ClearLog();
  227. StartBDS();
  228. break;
  229. case ID_STOP:
  230. StopBDS();
  231. break;
  232. case ID_STOP_FORCE:
  233. if (MessageBox(hWnd,
  234. L"强制关闭服务器是非常危险的操作!\n是否仍要继续?", TITLE,
  235. MB_OKCANCEL | MB_APPLMODAL | MB_ICONWARNING)
  236. == 1)
  237. {
  238. ForceStopBDS();
  239. }
  240. break;
  241. case ID_CMD_OK:
  242. {
  243. if (GetWindowTextLength(GetDlgItem(hWnd, ID_CMD_KEY)) > 0)
  244. {
  245. char Command[1024] = { 0 };
  246. GetWindowTextA(GetDlgItem(hWnd, ID_CMD_KEY), Command, 1024);
  247. std::string temp(Command);
  248. temp += "\n";
  249. SendCommand(temp.c_str());
  250. }
  251. break;
  252. }
  253. case ID_CMD_CLEAR:
  254. SetWindowText(GetDlgItem(hWnd, ID_CMD_KEY), _T(" "));
  255. break;
  256. case ID_FC_Weather:
  257. if (!hWeather)
  258. {
  259. HINSTANCE HI_FC_weather = NULL;
  260. hWeather = CreateChildWindow(hWnd,
  261. _T("天气控制"),
  262. 400, 370,
  263. WeatherProc, _T("fc_wea"), HI_FC_weather);
  264. }
  265. break;
  266. case ID_FC_Time:
  267. if (!hTime)
  268. {
  269. HINSTANCE HI_FC_time = NULL;
  270. hTime = CreateChildWindow(hWnd,
  271. _T("时间控制"),
  272. 400, 415,
  273. TimeProc, _T("fc_time"), HI_FC_time);
  274. }
  275. break;
  276. case ID_FS_WT:
  277. SendCommand(C("time query daytime"));
  278. SendCommand(C("weather query"));
  279. break;
  280. case ID_FC_PLAYER:
  281. if (!hPlayer)
  282. {
  283. HINSTANCE HI_FC_player = NULL;
  284. hPlayer = CreateChildWindow(hWnd,
  285. _T("玩家面板"),
  286. 400, 370,
  287. PlayerProc, _T("fc_player"), HI_FC_player);
  288. }
  289. break;
  290. case ID_FS_LIST:
  291. SendCommand(C("list"));
  292. break;
  293. }
  294. break;
  295. }
  296. case WM_CLOSE:
  297. if (MessageBox(hWnd,
  298. L"服务端若正在运行,将被一并关闭。", TITLE,
  299. MB_OKCANCEL | MB_APPLMODAL | MB_ICONWARNING)
  300. == 1)
  301. {
  302. StopBDS();
  303. PostQuitMessage(0);
  304. }
  305. break;
  306. case WM_DESTROY:
  307. PostQuitMessage(0);
  308. break;
  309. default:
  310. return DefWindowProc(hWnd, message, wParam, lParam);
  311. break;
  312. }
  313. return 0;
  314. }