Advertisement
Val_Kir

Лекция 6

Apr 21st, 2021
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.35 KB | None | 0 0
  1. Lec6Pg3
  2. basewin.h
  3. #pragma once
  4. template <class DERIVED_TYPE>
  5. class BaseWindow
  6. {
  7. public:
  8. static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  9. {
  10. DERIVED_TYPE *pThis = NULL;
  11. if (uMsg == WM_NCCREATE)
  12. {
  13. CREATESTRUCT* pCreate = (CREATESTRUCT*)lParam;
  14. pThis = (DERIVED_TYPE*)pCreate->lpCreateParams;
  15. SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)pThis);
  16. pThis->m_hwnd = hwnd;
  17. }
  18. else
  19. {
  20. pThis = (DERIVED_TYPE*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
  21. }
  22. if (pThis)
  23. {
  24. return pThis->HandleMessage(uMsg, wParam, lParam);
  25. }
  26. else
  27. {
  28. return DefWindowProc(hwnd, uMsg, wParam, lParam);
  29. }
  30. }
  31. BaseWindow() : m_hwnd(NULL) { }
  32. BOOL Create(
  33. PCWSTR lpWindowName,
  34. DWORD dwStyle,
  35. DWORD dwExStyle = 0,
  36. int x = CW_USEDEFAULT,
  37. int y = CW_USEDEFAULT,
  38. int nWidth = CW_USEDEFAULT,
  39. int nHeight = CW_USEDEFAULT,
  40. HWND hWndParent = 0,
  41. HMENU hMenu = 0
  42. )
  43. {
  44. WNDCLASS wc = { 0 };
  45. wc.lpfnWndProc = DERIVED_TYPE::WindowProc;
  46. wc.hInstance = GetModuleHandle(NULL);
  47. wc.lpszClassName = ClassName();
  48. //wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  49. //wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
  50. RegisterClass(&wc);
  51. m_hwnd = CreateWindowEx(
  52. dwExStyle, ClassName(), lpWindowName, dwStyle, x, y,
  53. nWidth, nHeight, hWndParent, hMenu, GetModuleHandle(NULL), this
  54. );
  55. return (m_hwnd ? TRUE : FALSE);
  56. }
  57. HWND Window() const { return m_hwnd; }
  58. protected:
  59. virtual PCWSTR ClassName() const = 0;
  60. HWND m_hwnd;
  61. };
  62. MainWindow.cpp
  63. #include <Windows.h>
  64. #include "MainWindow.h"
  65. struct
  66. {
  67. int iStyle;
  68. const TCHAR * szText;
  69. }
  70. but[] =
  71. {
  72. BS_PUSHBUTTON, TEXT("PUSHBUTTON"),
  73. BS_DEFPUSHBUTTON, TEXT("DEFPUSHBUTTON"),
  74. BS_CHECKBOX, TEXT("CHECKBOX"),
  75. BS_AUTOCHECKBOX, TEXT("AUTOCHECKBOX"),
  76. BS_RADIOBUTTON, TEXT("RADIOBUTTON"),
  77. BS_3STATE, TEXT("3STATE"),
  78. BS_AUTO3STATE, TEXT("AUTO3STATE"),
  79. BS_GROUPBOX, TEXT("GROUPBOX"),
  80. BS_AUTORADIOBUTTON, TEXT("AUTORADIO"),
  81. BS_OWNERDRAW, TEXT("OWNERDRAW")
  82. };
  83. #define NUM (sizeof but / sizeof but[0])
  84. bool MainWindow::OnCreate(LPARAM lParam)
  85. {
  86. cxChar = LOWORD(GetDialogBaseUnits());
  87. cyChar = HIWORD(GetDialogBaseUnits());
  88. for (int i = 0; i < NUM; i++)
  89. CreateWindow(TEXT("button"),
  90. but[i].szText,
  91. WS_CHILD | WS_VISIBLE | but[i].iStyle,
  92. cxChar, cyChar * (1 + 2 * i),
  93. 20 * cxChar, 7 * cyChar / 4,
  94. m_hwnd, (HMENU)i,
  95. ((LPCREATESTRUCT)lParam)->hInstance, NULL);
  96. hwndList = CreateWindow(TEXT("listbox"), NULL,
  97. WS_CHILD | WS_VISIBLE,
  98. 0, 0,
  99. 0,
  100. 0,
  101. m_hwnd, (HMENU)NUM,
  102. (HINSTANCE)GetWindowLong(m_hwnd, GWL_HINSTANCE),
  103. NULL);
  104. return true;
  105. }
  106. LRESULT MainWindow::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
  107. {
  108. switch (uMsg)
  109. {
  110. case WM_CREATE:
  111. if (!OnCreate(lParam))
  112. {
  113. return false; // Неудача CreateWindowEx.
  114. }
  115. return 0;
  116. case WM_SIZE:
  117. int cxClient, cyClient;
  118. cxClient = LOWORD(lParam);
  119. cyClient = HIWORD(lParam);
  120. MoveWindow(hwndList, 24 * cxChar, 0, cxClient - (24 * cxChar), cyClient, TRUE);
  121. return 0;
  122. case WM_DRAWITEM:
  123. case WM_COMMAND:
  124. {
  125. int wmId = LOWORD(wParam);
  126. if (wmId >= 0 && wmId < NUM)
  127. {
  128. TCHAR szFormat[] = TEXT("%-16s%04X-%04X %04X-%04X"),
  129. szBuffer[50];
  130. wsprintf(szBuffer, szFormat,
  131. uMsg == WM_DRAWITEM ? TEXT("WM_DRAWITEM") :
  132. TEXT("WM_COMMAND"),
  133. HIWORD(wParam), LOWORD(wParam),
  134. HIWORD(lParam), LOWORD(lParam));
  135. SendMessage(hwndList, LB_ADDSTRING, 0, (LPARAM)szBuffer);
  136. return 0;
  137. }
  138. }
  139. case WM_DESTROY:
  140. PostQuitMessage(0);
  141. return 0;
  142. }
  143. return DefWindowProc(m_hwnd, uMsg, wParam, lParam);
  144. }
  145. MainWindow.h
  146. #pragma once
  147. #include "basewin.h"
  148. class MainWindow : public BaseWindow<MainWindow> {
  149. int cxChar, cyChar;
  150. HWND hwndList;
  151. public:
  152. bool OnCreate(LPARAM lParam);
  153. PCWSTR ClassName() const { return L"BtnLook Window Class"; }
  154. LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);
  155. };
  156. Source.cpp
  157. #include <windows.h>
  158. #include "MainWindow.h"
  159. int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
  160. {
  161. MainWindow win;
  162. if (!win.Create(L"Кнопки", WS_OVERLAPPEDWINDOW))
  163. {
  164. return 0;
  165. }
  166. ShowWindow(win.Window(), nCmdShow);
  167. // Run the message loop.
  168. MSG msg = {};
  169. while (GetMessage(&msg, NULL, 0, 0))
  170. {
  171. TranslateMessage(&msg);
  172. DispatchMessage(&msg);
  173. }
  174. return 0;
  175. }
  176. Lec6Pg3a
  177. basewin.h
  178. #pragma once
  179. template <class DERIVED_TYPE>
  180. class BaseWindow
  181. {
  182. public:
  183. static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  184. {
  185. DERIVED_TYPE *pThis = NULL;
  186. if (uMsg == WM_NCCREATE)
  187. {
  188. CREATESTRUCT* pCreate = (CREATESTRUCT*)lParam;
  189. pThis = (DERIVED_TYPE*)pCreate->lpCreateParams;
  190. SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)pThis);
  191. pThis->m_hwnd = hwnd;
  192. }
  193. else
  194. {
  195. pThis = (DERIVED_TYPE*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
  196. }
  197. if (pThis)
  198. {
  199. return pThis->HandleMessage(uMsg, wParam, lParam);
  200. }
  201. else
  202. {
  203. return DefWindowProc(hwnd, uMsg, wParam, lParam);
  204. }
  205. }
  206. BaseWindow() : m_hwnd(NULL) { }
  207. BOOL Create(
  208. PCWSTR lpWindowName,
  209. DWORD dwStyle,
  210. DWORD dwExStyle = 0,
  211. int x = CW_USEDEFAULT,
  212. int y = CW_USEDEFAULT,
  213. int nWidth = CW_USEDEFAULT,
  214. int nHeight = CW_USEDEFAULT,
  215. HWND hWndParent = 0,
  216. HMENU hMenu = 0
  217. )
  218. {
  219. WNDCLASS wc = { 0 };
  220. wc.lpfnWndProc = DERIVED_TYPE::WindowProc;
  221. wc.hInstance = GetModuleHandle(NULL);
  222. wc.lpszClassName = ClassName();
  223. //wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  224. wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
  225. RegisterClass(&wc);
  226. m_hwnd = CreateWindowEx(
  227. dwExStyle, ClassName(), lpWindowName, dwStyle, x, y,
  228. nWidth, nHeight, hWndParent, hMenu, GetModuleHandle(NULL), this
  229. );
  230. return (m_hwnd ? TRUE : FALSE);
  231. }
  232. HWND Window() const { return m_hwnd; }
  233. protected:
  234. virtual PCWSTR ClassName() const = 0;
  235. HWND m_hwnd;
  236. };
  237. MainWindow.cpp
  238. #include <Windows.h>
  239. #include "MainWindow.h"
  240. struct
  241. {
  242. int iStyle;
  243. const TCHAR * szText;
  244. }
  245. but[] =
  246. {
  247. BS_PUSHBUTTON, TEXT("PUSHBUTTON"),
  248. BS_DEFPUSHBUTTON, TEXT("DEFPUSHBUTTON"),
  249. BS_CHECKBOX, TEXT("CHECKBOX"),
  250. BS_AUTOCHECKBOX, TEXT("AUTOCHECKBOX"),
  251. BS_RADIOBUTTON, TEXT("RADIOBUTTON"),
  252. BS_3STATE, TEXT("3STATE"),
  253. BS_AUTO3STATE, TEXT("AUTO3STATE"),
  254. BS_GROUPBOX, TEXT("GROUPBOX"),
  255. BS_AUTORADIOBUTTON, TEXT("AUTORADIO"),
  256. BS_OWNERDRAW, TEXT("OWNERDRAW")
  257. };
  258. #define NUM (sizeof but / sizeof but[0])
  259. bool MainWindow::OnCreate(LPARAM lParam)
  260. {
  261. cxChar = LOWORD(GetDialogBaseUnits());
  262. cyChar = HIWORD(GetDialogBaseUnits());
  263. for (int i = 0; i < NUM; i++)
  264. CreateWindow(TEXT("button"),
  265. but[i].szText,
  266. WS_CHILD | WS_VISIBLE | but[i].iStyle,
  267. cxChar, cyChar * (1 + 2 * i),
  268. 20 * cxChar, 7 * cyChar / 4,
  269. m_hwnd, (HMENU)i,
  270. ((LPCREATESTRUCT)lParam)->hInstance, NULL);
  271. hwndList = CreateWindow(TEXT("listbox"), NULL,
  272. WS_CHILD | WS_VISIBLE,
  273. 0, 0,
  274. 0,
  275. 0,
  276. m_hwnd, (HMENU)NUM,
  277. (HINSTANCE)GetWindowLong(m_hwnd, GWL_HINSTANCE),
  278. NULL);
  279. return true;
  280. }
  281. LRESULT MainWindow::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
  282. {
  283. switch (uMsg)
  284. {
  285. case WM_CREATE:
  286. if (!OnCreate(lParam))
  287. {
  288. return false; // Неудача CreateWindowEx.
  289. }
  290. return 0;
  291. case WM_SIZE:
  292. int cxClient, cyClient;
  293. cxClient = LOWORD(lParam);
  294. cyClient = HIWORD(lParam);
  295. MoveWindow(hwndList, 24 * cxChar, 0, cxClient - (24 * cxChar), cyClient, TRUE);
  296. return 0;
  297. case WM_DRAWITEM:
  298. case WM_COMMAND:
  299. {
  300. int wmId = LOWORD(wParam);
  301. if (wmId >= 0 && wmId < NUM)
  302. {
  303. TCHAR szFormat[] = TEXT("%-16s%04X-%04X %04X-%04X"),
  304. szBuffer[50];
  305. wsprintf(szBuffer, szFormat,
  306. uMsg == WM_DRAWITEM ? TEXT("WM_DRAWITEM") :
  307. TEXT("WM_COMMAND"),
  308. HIWORD(wParam), LOWORD(wParam),
  309. HIWORD(lParam), LOWORD(lParam));
  310. SendMessage(hwndList, LB_ADDSTRING, 0, (LPARAM)szBuffer);
  311. return 0;
  312. }
  313. }
  314. case WM_DESTROY:
  315. PostQuitMessage(0);
  316. return 0;
  317. }
  318. return DefWindowProc(m_hwnd, uMsg, wParam, lParam);
  319. }
  320. MainWindow.h
  321. #pragma once
  322. #include "basewin.h"
  323. class MainWindow : public BaseWindow<MainWindow> {
  324. int cxChar, cyChar;
  325. HWND hwndList;
  326. public:
  327. bool OnCreate(LPARAM lParam);
  328. PCWSTR ClassName() const { return L"BtnLook Window Class"; }
  329. LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);
  330. };
  331. Source.cpp
  332. #include <windows.h>
  333. #include "MainWindow.h"
  334. int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
  335. {
  336. MainWindow win;
  337. if (!win.Create(L"Кнопки", WS_OVERLAPPEDWINDOW))
  338. {
  339. return 0;
  340. }
  341. ShowWindow(win.Window(), nCmdShow);
  342. // Run the message loop.
  343. MSG msg = {};
  344. while (GetMessage(&msg, NULL, 0, 0))
  345. {
  346. TranslateMessage(&msg);
  347. DispatchMessage(&msg);
  348. }
  349. return 0;
  350. }
  351. Lec6Pg19
  352. basewin.h
  353. #pragma once
  354. template <class DERIVED_TYPE>
  355. class BaseWindow
  356. {
  357. public:
  358. static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  359. {
  360. DERIVED_TYPE *pThis = NULL;
  361. if (uMsg == WM_NCCREATE)
  362. {
  363. CREATESTRUCT* pCreate = (CREATESTRUCT*)lParam;
  364. pThis = (DERIVED_TYPE*)pCreate->lpCreateParams;
  365. SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)pThis);
  366. pThis->m_hwnd = hwnd;
  367. }
  368. else
  369. {
  370. pThis = (DERIVED_TYPE*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
  371. }
  372. if (pThis)
  373. {
  374. return pThis->HandleMessage(uMsg, wParam, lParam);
  375. }
  376. else
  377. {
  378. return DefWindowProc(hwnd, uMsg, wParam, lParam);
  379. }
  380. }
  381. BaseWindow() : m_hwnd(NULL) { }
  382. BOOL Create(
  383. PCWSTR lpWindowName,
  384. DWORD dwStyle,
  385. DWORD dwExStyle = 0,
  386. int x = CW_USEDEFAULT,
  387. int y = CW_USEDEFAULT,
  388. int nWidth = CW_USEDEFAULT,
  389. int nHeight = CW_USEDEFAULT,
  390. HWND hWndParent = 0,
  391. HMENU hMenu = 0
  392. )
  393. {
  394. WNDCLASS wc = { 0 };
  395. wc.lpfnWndProc = DERIVED_TYPE::WindowProc;
  396. wc.hInstance = GetModuleHandle(NULL);
  397. wc.lpszClassName = ClassName();
  398. wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  399. wc.lpszMenuName = MAKEINTRESOURCE(IDC_MENUDEMO);
  400. RegisterClass(&wc);
  401. m_hwnd = CreateWindowEx(
  402. dwExStyle, ClassName(), lpWindowName, dwStyle, x, y,
  403. nWidth, nHeight, hWndParent, hMenu, GetModuleHandle(NULL), this
  404. );
  405. return (m_hwnd ? TRUE : FALSE);
  406. }
  407. HWND Window() const { return m_hwnd; }
  408. protected:
  409. virtual PCWSTR ClassName() const = 0;
  410. HWND m_hwnd;
  411. };
  412. MainWindow.cpp
  413. #include <Windows.h>
  414. #include "MainWindow.h"
  415. #define ID_TIMER 1
  416. static int idColor[5] = { WHITE_BRUSH, LTGRAY_BRUSH, GRAY_BRUSH,
  417. DKGRAY_BRUSH, BLACK_BRUSH };
  418. static int iSelection = ID_BACKGROUND_WHITE;
  419. LRESULT MainWindow::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
  420. {
  421. switch (uMsg)
  422. {
  423. case WM_TIMER:
  424. MessageBeep(0);
  425. return 0;
  426. case WM_COMMAND:
  427. {
  428. HMENU hMenu = GetMenu(m_hwnd);
  429. int wmId = LOWORD(wParam);
  430. switch (wmId)
  431. {
  432. case ID_FILE_NEW:
  433. case ID_FILE_OPEN:
  434. case ID_FILE_SAVE:
  435. case ID_FILE_SAVEAS:
  436. MessageBeep(0);
  437. break;
  438. case IDM_EXIT:
  439. SendMessage(m_hwnd, WM_CLOSE, 0, 0);
  440. break;
  441. case ID_EDIT_UNDO:
  442. case ID_EDIT_CUT:
  443. case ID_EDIT_COPY:
  444. case ID_EDIT_PASTE:
  445. case ID_EDIT_DELETE:
  446. MessageBeep(0);
  447. break;
  448. case ID_BACKGROUND_WHITE: // Note: Logic below
  449. case ID_BACKGROUND_LIGHTGREY: // assumes that IDM_WHITE
  450. case ID_BACKGROUND_GRAY: // through IDM_BLACK are
  451. case ID_BACKGROUND_DARKGRAY: // consecutive numbers in
  452. case ID_BACKGROUND_BLACK: // the order shown here.
  453. CheckMenuItem(hMenu, iSelection, MF_UNCHECKED);
  454. iSelection = wmId;
  455. CheckMenuItem(hMenu, iSelection, MF_CHECKED);
  456. SetClassLong(m_hwnd, GCL_HBRBACKGROUND, (LONG)
  457. GetStockObject
  458. (idColor[wmId - ID_BACKGROUND_WHITE]));
  459. InvalidateRect(m_hwnd, NULL, TRUE);
  460. break;
  461. case ID_TIMER_START:
  462. if (SetTimer(m_hwnd, ID_TIMER, 1000, NULL))
  463. {
  464. EnableMenuItem(hMenu, ID_TIMER_START, MF_GRAYED);
  465. EnableMenuItem(hMenu, ID_TIMER_STOP, MF_ENABLED);
  466. }
  467. break;
  468. case ID_TIMER_STOP:
  469. KillTimer(m_hwnd, ID_TIMER);
  470. EnableMenuItem(hMenu, ID_TIMER_START, MF_ENABLED);
  471. EnableMenuItem(hMenu, ID_TIMER_STOP, MF_GRAYED);
  472. break;
  473. case ID_HELP_HELP:
  474. MessageBox(m_hwnd, TEXT("Help not yet implemented!"),
  475. TEXT("Меню демо"), MB_ICONEXCLAMATION | MB_OK);
  476. break;
  477. default: return DefWindowProc(m_hwnd, uMsg, wParam, lParam);
  478. }
  479. return 0;
  480. }
  481. case WM_DESTROY:
  482. PostQuitMessage(0);
  483. return 0;
  484. }
  485. return DefWindowProc(m_hwnd, uMsg, wParam, lParam);
  486. }
  487. MainWindow.h
  488. #pragma once
  489. #include "Resource.h"
  490. #include "basewin.h"
  491. class MainWindow : public BaseWindow<MainWindow> {
  492. public:
  493. PCWSTR ClassName() const { return L"MenuDemo Window Class"; }
  494. LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);
  495. };
  496. MenuDemo.rc
  497. // Microsoft Visual C++ generated resource script.
  498. //
  499. #include "resource.h"
  500. /////////////////////////////////////////////////////////////////////////////
  501. //
  502. // Menu
  503. //
  504. IDC_MENUDEMO MENU
  505. BEGIN
  506. POPUP "&File"
  507. BEGIN
  508. MENUITEM "&New", ID_FILE_NEW
  509. MENUITEM "&Open", ID_FILE_OPEN
  510. MENUITEM "&Save", ID_FILE_SAVE
  511. MENUITEM "Save &As...", ID_FILE_SAVEAS
  512. MENUITEM SEPARATOR
  513. MENUITEM "E&xit", IDM_EXIT
  514. END
  515. POPUP "&Edit"
  516. BEGIN
  517. MENUITEM "&Undo", ID_EDIT_UNDO
  518. MENUITEM SEPARATOR
  519. MENUITEM "C&ut", ID_EDIT_CUT
  520. MENUITEM "&Copy", ID_EDIT_COPY
  521. MENUITEM "&Paste", ID_EDIT_PASTE
  522. MENUITEM "De&lete", ID_EDIT_DELETE
  523. END
  524. POPUP "&Background"
  525. BEGIN
  526. MENUITEM "&White", ID_BACKGROUND_WHITE, CHECKED
  527. MENUITEM "&Light gray", ID_BACKGROUND_LIGHTGREY
  528. MENUITEM "&Gray", ID_BACKGROUND_GRAY
  529. MENUITEM "&Dark gray", ID_BACKGROUND_DARKGRAY
  530. MENUITEM "&Black", ID_BACKGROUND_BLACK
  531. END
  532. POPUP "&Timer"
  533. BEGIN
  534. MENUITEM "&Start", ID_TIMER_START
  535. MENUITEM "S&top", ID_TIMER_STOP, INACTIVE
  536. END
  537. POPUP "&Help"
  538. BEGIN
  539. MENUITEM "&Help...", ID_HELP_HELP
  540. END
  541. END
  542. Resource.h
  543. //{{NO_DEPENDENCIES}}
  544. // Microsoft Visual C++ generated include file.
  545. // Used by MenuDemo.rc
  546. //
  547. #define IDC_MYICON 2
  548. #define IDD_MENUDEMO_DIALOG 102
  549. #define IDS_APP_TITLE 103
  550. #define IDD_ABOUTBOX 103
  551. #define IDM_ABOUT 104
  552. #define IDM_EXIT 105
  553. #define IDI_MENUDEMO 107
  554. #define IDI_SMALL 108
  555. #define IDC_MENUDEMO 109
  556. #define IDR_MAINFRAME 128
  557. #define ID_FILE_NEW 32771
  558. #define ID_FILE_OPEN 32772
  559. #define ID_FILE_SAVE 32773
  560. #define ID_FILE_SAVEAS 32774
  561. #define ID_EDIT_UNDO 32775
  562. #define ID_EDIT_CUT 32776
  563. #define ID_EDIT_COPY 32777
  564. #define ID_EDIT_PASTE 32778
  565. #define ID_EDIT_DELETE 32779
  566. #define ID_BACKGROUND_WHITE 32780
  567. #define ID_BACKGROUND_LIGHTGREY 32781
  568. #define ID_BACKGROUND_GRAY 32782
  569. #define ID_BACKGROUND_DARKGRAY 32783
  570. #define ID_BACKGROUND_BLACK 32784
  571. #define ID_TIMER_START 32785
  572. #define ID_TIMER_STOP 32786
  573. #define ID_HELP_HELP 32787
  574. #define IDC_STATIC -1
  575. // Next default values for new objects
  576. //
  577. #ifdef APSTUDIO_INVOKED
  578. #ifndef APSTUDIO_READONLY_SYMBOLS
  579. #define _APS_NO_MFC 1
  580. #define _APS_NEXT_RESOURCE_VALUE 129
  581. #define _APS_NEXT_COMMAND_VALUE 32788
  582. #define _APS_NEXT_CONTROL_VALUE 1000
  583. #define _APS_NEXT_SYMED_VALUE 110
  584. #endif
  585. #endif
  586. Source.cpp
  587. #include <windows.h>
  588. #include "MainWindow.h"
  589. int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
  590. {
  591. MainWindow win;
  592. if (!win.Create(L"Меню демо", WS_OVERLAPPEDWINDOW))
  593. {
  594. return 0;
  595. }
  596. ShowWindow(win.Window(), nCmdShow);
  597. // Run the message loop.
  598. MSG msg = {};
  599. while (GetMessage(&msg, NULL, 0, 0))
  600. {
  601. TranslateMessage(&msg);
  602. DispatchMessage(&msg);
  603. }
  604. return 0;
  605. }
  606. Lec6Pg22
  607. basewin.h
  608. #pragma once
  609. template <class DERIVED_TYPE>
  610. class BaseWindow
  611. {
  612. public:
  613. static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  614. {
  615. DERIVED_TYPE *pThis = NULL;
  616. if (uMsg == WM_NCCREATE)
  617. {
  618. CREATESTRUCT* pCreate = (CREATESTRUCT*)lParam;
  619. pThis = (DERIVED_TYPE*)pCreate->lpCreateParams;
  620. SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)pThis);
  621. pThis->m_hwnd = hwnd;
  622. }
  623. else
  624. {
  625. pThis = (DERIVED_TYPE*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
  626. }
  627. if (pThis)
  628. {
  629. return pThis->HandleMessage(uMsg, wParam, lParam);
  630. }
  631. else
  632. {
  633. return DefWindowProc(hwnd, uMsg, wParam, lParam);
  634. }
  635. }
  636. BaseWindow() : m_hwnd(NULL) { }
  637. BOOL Create(
  638. PCWSTR lpWindowName,
  639. DWORD dwStyle,
  640. DWORD dwExStyle = 0,
  641. int x = CW_USEDEFAULT,
  642. int y = CW_USEDEFAULT,
  643. int nWidth = CW_USEDEFAULT,
  644. int nHeight = CW_USEDEFAULT,
  645. HWND hWndParent = 0,
  646. HMENU hMenu = 0
  647. )
  648. {
  649. WNDCLASS wc = { 0 };
  650. wc.lpfnWndProc = DERIVED_TYPE::WindowProc;
  651. wc.hInstance = GetModuleHandle(NULL);
  652. wc.lpszClassName = ClassName();
  653. wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  654. //wc.lpszMenuName = MAKEINTRESOURCE(IDC_MENUDEMO);
  655. RegisterClass(&wc);
  656. m_hwnd = CreateWindowEx(
  657. dwExStyle, ClassName(), lpWindowName, dwStyle, x, y,
  658. nWidth, nHeight, hWndParent, hMenu, GetModuleHandle(NULL), this
  659. );
  660. return (m_hwnd ? TRUE : FALSE);
  661. }
  662. HWND Window() const { return m_hwnd; }
  663. protected:
  664. virtual PCWSTR ClassName() const = 0;
  665. HWND m_hwnd;
  666. };
  667. MainWindow.cpp
  668. #include <Windows.h>
  669. #include "MainWindow.h"
  670. #define ID_TIMER 1
  671. static int idColor[5] = { WHITE_BRUSH, LTGRAY_BRUSH, GRAY_BRUSH,
  672. DKGRAY_BRUSH, BLACK_BRUSH };
  673. static int iSelection = ID_BACKGROUND_WHITE;
  674. LRESULT MainWindow::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
  675. {
  676. switch (uMsg)
  677. {
  678. case WM_CREATE:
  679. hMenu = LoadMenu(((LPCREATESTRUCT)lParam)->hInstance, MAKEINTRESOURCE(IDC_POPMENU));
  680. hMenu = GetSubMenu(hMenu, 0);
  681. break;
  682. case WM_RBUTTONUP:
  683. POINT point;
  684. point.x = LOWORD(lParam);
  685. point.y = HIWORD(lParam);
  686. ClientToScreen(m_hwnd, &point);
  687. TrackPopupMenu(hMenu, TPM_RIGHTBUTTON, point.x, point.y,
  688. 0, m_hwnd, NULL);
  689. break;
  690. case WM_TIMER:
  691. MessageBeep(0);
  692. return 0;
  693. case WM_COMMAND:
  694. {
  695. int wmId = LOWORD(wParam);
  696. switch (wmId)
  697. {
  698. case ID_FILE_NEW:
  699. case ID_FILE_OPEN:
  700. case ID_FILE_SAVE:
  701. case ID_FILE_SAVEAS:
  702. MessageBeep(0);
  703. break;
  704. case IDM_EXIT:
  705. SendMessage(m_hwnd, WM_CLOSE, 0, 0);
  706. break;
  707. case ID_EDIT_UNDO:
  708. case ID_EDIT_CUT:
  709. case ID_EDIT_COPY:
  710. case ID_EDIT_PASTE:
  711. case ID_EDIT_DELETE:
  712. MessageBeep(0);
  713. break;
  714. case ID_BACKGROUND_WHITE: // Note: Logic below
  715. case ID_BACKGROUND_LIGHTGREY: // assumes that IDM_WHITE
  716. case ID_BACKGROUND_GRAY: // through IDM_BLACK are
  717. case ID_BACKGROUND_DARKGRAY: // consecutive numbers in
  718. case ID_BACKGROUND_BLACK: // the order shown here.
  719. CheckMenuItem(hMenu, iSelection, MF_UNCHECKED);
  720. iSelection = wmId;
  721. CheckMenuItem(hMenu, iSelection, MF_CHECKED);
  722. SetClassLong(m_hwnd, GCL_HBRBACKGROUND, (LONG)
  723. GetStockObject
  724. (idColor[wmId - ID_BACKGROUND_WHITE]));
  725. InvalidateRect(m_hwnd, NULL, TRUE);
  726. break;
  727. case ID_TIMER_START:
  728. if (SetTimer(m_hwnd, ID_TIMER, 1000, NULL))
  729. {
  730. EnableMenuItem(hMenu, ID_TIMER_START, MF_GRAYED);
  731. EnableMenuItem(hMenu, ID_TIMER_STOP, MF_ENABLED);
  732. }
  733. break;
  734. case ID_TIMER_STOP:
  735. KillTimer(m_hwnd, ID_TIMER);
  736. EnableMenuItem(hMenu, ID_TIMER_START, MF_ENABLED);
  737. EnableMenuItem(hMenu, ID_TIMER_STOP, MF_GRAYED);
  738. break;
  739. case ID_HELP_HELP:
  740. MessageBox(m_hwnd, TEXT("Help not yet implemented!"),
  741. TEXT("Всплывающее меню"), MB_ICONEXCLAMATION | MB_OK);
  742. break;
  743. default: return DefWindowProc(m_hwnd, uMsg, wParam, lParam);
  744. }
  745. return 0;
  746. }
  747. case WM_DESTROY:
  748. PostQuitMessage(0);
  749. return 0;
  750. }
  751. return DefWindowProc(m_hwnd, uMsg, wParam, lParam);
  752. }
  753. MainWindow.h
  754. #pragma once
  755. #include "Resource.h"
  756. #include "basewin.h"
  757. class MainWindow : public BaseWindow<MainWindow> {
  758. HMENU hMenu;
  759. public:
  760. PCWSTR ClassName() const { return L"Popmenu Window Class"; }
  761. LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);
  762. };
  763. MenuDemo.rc
  764. // Microsoft Visual C++ generated resource script.
  765. //
  766. #include "resource.h"
  767. /////////////////////////////////////////////////////////////////////////////
  768. //
  769. // Menu
  770. //
  771. IDC_POPMENU MENU
  772. BEGIN
  773. POPUP "MyMenu"
  774. BEGIN
  775. POPUP "&File"
  776. BEGIN
  777. MENUITEM "&New", ID_FILE_NEW
  778. MENUITEM "&Open", ID_FILE_OPEN
  779. MENUITEM "&Save", ID_FILE_SAVE
  780. MENUITEM "Save &As...", ID_FILE_SAVEAS
  781. MENUITEM SEPARATOR
  782. MENUITEM "E&xit", IDM_EXIT
  783. END
  784. POPUP "&Edit"
  785. BEGIN
  786. MENUITEM "&Undo", ID_EDIT_UNDO
  787. MENUITEM SEPARATOR
  788. MENUITEM "C&ut", ID_EDIT_CUT
  789. MENUITEM "&Copy", ID_EDIT_COPY
  790. MENUITEM "&Paste", ID_EDIT_PASTE
  791. MENUITEM "De&lete", ID_EDIT_DELETE
  792. END
  793. POPUP "&Background"
  794. BEGIN
  795. MENUITEM "&White", ID_BACKGROUND_WHITE, CHECKED
  796. MENUITEM "&Light gray", ID_BACKGROUND_LIGHTGREY
  797. MENUITEM "&Gray", ID_BACKGROUND_GRAY
  798. MENUITEM "&Dark gray", ID_BACKGROUND_DARKGRAY
  799. MENUITEM "&Black", ID_BACKGROUND_BLACK
  800. END
  801. POPUP "&Timer"
  802. BEGIN
  803. MENUITEM "&Start", ID_TIMER_START
  804. MENUITEM "S&top", ID_TIMER_STOP, INACTIVE
  805. END
  806. POPUP "&Help"
  807. BEGIN
  808. MENUITEM "&Help...", ID_HELP_HELP
  809. END
  810. END
  811. END
  812. Resource.h
  813. //{{NO_DEPENDENCIES}}
  814. // Microsoft Visual C++ generated include file.
  815. // Used by PopMenu.rc
  816. //
  817. #define IDC_MYICON 2
  818. #define IDD_POPMENU_DIALOG 102
  819. #define IDS_APP_TITLE 103
  820. #define IDD_ABOUTBOX 103
  821. #define IDM_ABOUT 104
  822. #define IDM_EXIT 105
  823. #define IDI_POPMENU 107
  824. #define IDI_SMALL 108
  825. #define IDC_POPMENU 109
  826. #define IDR_MAINFRAME 128
  827. #define ID_FILE_NEW 32771
  828. #define ID_FILE_OPEN 32772
  829. #define ID_FILE_SAVE 32773
  830. #define ID_FILE_SAVEAS 32774
  831. #define ID_EDIT_UNDO 32775
  832. #define ID_EDIT_CUT 32776
  833. #define ID_EDIT_COPY 32777
  834. #define ID_EDIT_PASTE 32778
  835. #define ID_EDIT_DELETE 32779
  836. #define ID_BACKGROUND_WHITE 32780
  837. #define ID_BACKGROUND_LIGHTGREY 32781
  838. #define ID_BACKGROUND_GRAY 32782
  839. #define ID_BACKGROUND_DARKGRAY 32783
  840. #define ID_BACKGROUND_BLACK 32784
  841. #define ID_TIMER_START 32785
  842. #define ID_TIMER_STOP 32786
  843. #define ID_HELP_HELP 32787
  844. #define IDC_STATIC -1
  845. // Next default values for new objects
  846. //
  847. #ifdef APSTUDIO_INVOKED
  848. #ifndef APSTUDIO_READONLY_SYMBOLS
  849. #define _APS_NO_MFC 1
  850. #define _APS_NEXT_RESOURCE_VALUE 129
  851. #define _APS_NEXT_COMMAND_VALUE 32788
  852. #define _APS_NEXT_CONTROL_VALUE 1000
  853. #define _APS_NEXT_SYMED_VALUE 110
  854. #endif
  855. #endif
  856. Source.cpp
  857. #include <windows.h>
  858. #include "MainWindow.h"
  859. int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
  860. {
  861. MainWindow win;
  862. if (!win.Create(L"Всплывающее меню", WS_OVERLAPPEDWINDOW))
  863. {
  864. return 0;
  865. }
  866. ShowWindow(win.Window(), nCmdShow);
  867. // Run the message loop.
  868. MSG msg = {};
  869. while (GetMessage(&msg, NULL, 0, 0))
  870. {
  871. TranslateMessage(&msg);
  872. DispatchMessage(&msg);
  873. }
  874. return 0;
  875. }
  876. Lec6Pg23
  877. basewin.h
  878. #pragma once
  879. template <class DERIVED_TYPE>
  880. class BaseWindow
  881. {
  882. public:
  883. static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  884. {
  885. DERIVED_TYPE *pThis = NULL;
  886. if (uMsg == WM_NCCREATE)
  887. {
  888. CREATESTRUCT* pCreate = (CREATESTRUCT*)lParam;
  889. pThis = (DERIVED_TYPE*)pCreate->lpCreateParams;
  890. SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)pThis);
  891. pThis->m_hwnd = hwnd;
  892. }
  893. else
  894. {
  895. pThis = (DERIVED_TYPE*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
  896. }
  897. if (pThis)
  898. {
  899. return pThis->HandleMessage(uMsg, wParam, lParam);
  900. }
  901. else
  902. {
  903. return DefWindowProc(hwnd, uMsg, wParam, lParam);
  904. }
  905. }
  906. BaseWindow() : m_hwnd(NULL) { }
  907. BOOL Create(
  908. PCWSTR lpWindowName,
  909. DWORD dwStyle,
  910. DWORD dwExStyle = 0,
  911. int x = CW_USEDEFAULT,
  912. int y = CW_USEDEFAULT,
  913. int nWidth = CW_USEDEFAULT,
  914. int nHeight = CW_USEDEFAULT,
  915. HWND hWndParent = 0,
  916. HMENU hMenu = 0
  917. )
  918. {
  919. WNDCLASS wc = { 0 };
  920. wc.lpfnWndProc = DERIVED_TYPE::WindowProc;
  921. wc.hInstance = GetModuleHandle(NULL);
  922. wc.lpszClassName = ClassName();
  923. wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  924. //wc.lpszMenuName = MAKEINTRESOURCE(IDC_MENUDEMO);
  925. RegisterClass(&wc);
  926. m_hwnd = CreateWindowEx(
  927. dwExStyle, ClassName(), lpWindowName, dwStyle, x, y,
  928. nWidth, nHeight, hWndParent, hMenu, GetModuleHandle(NULL), this
  929. );
  930. return (m_hwnd ? TRUE : FALSE);
  931. }
  932. HWND Window() const { return m_hwnd; }
  933. protected:
  934. virtual PCWSTR ClassName() const = 0;
  935. HWND m_hwnd;
  936. };
  937. MainWindow.cpp
  938. #include <Windows.h>
  939. #include "MainWindow.h"
  940. #define IDM_SYS_ABOUT 1
  941. #define IDM_SYS_HELP 2
  942. #define IDM_SYS_REMOVE 3
  943. LRESULT MainWindow::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
  944. {
  945. switch (uMsg)
  946. {
  947. case WM_CREATE:
  948. HMENU hMenu;
  949. hMenu = GetSystemMenu(m_hwnd, FALSE);
  950. AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
  951. AppendMenu(hMenu, MF_STRING, IDM_SYS_ABOUT, TEXT("About..."));
  952. AppendMenu(hMenu, MF_STRING, IDM_SYS_HELP, TEXT("Help..."));
  953. AppendMenu(hMenu, MF_STRING, IDM_SYS_REMOVE, TEXT("Remove Additions"));
  954. return 0;
  955. case WM_DESTROY:
  956. PostQuitMessage(0);
  957. return 0;
  958. case WM_SYSCOMMAND:
  959. switch (LOWORD(wParam))
  960. {
  961. case IDM_SYS_ABOUT:
  962. MessageBox(m_hwnd, TEXT("A Poor-Person's Menu Program\n")
  963. TEXT("(c) TSU, 2019"),
  964. L"Системное меню", MB_OK | MB_ICONINFORMATION);
  965. return 0;
  966. case IDM_SYS_HELP:
  967. MessageBox(m_hwnd, TEXT("Help not yet implemented!"),
  968. L"Системное меню", MB_OK | MB_ICONEXCLAMATION);
  969. return 0;
  970. case IDM_SYS_REMOVE:
  971. GetSystemMenu(m_hwnd, TRUE);
  972. return 0;
  973. }
  974. }
  975. return DefWindowProc(m_hwnd, uMsg, wParam, lParam);
  976. }
  977. MainWindow.h
  978. #pragma once
  979. #include "basewin.h"
  980. class MainWindow : public BaseWindow<MainWindow> {
  981. public:
  982. PCWSTR ClassName() const { return L"Sysmenu Window Class"; }
  983. LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);
  984. };
  985. Source.cpp
  986. #include <windows.h>
  987. #include "MainWindow.h"
  988. int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
  989. {
  990. MainWindow win;
  991. if (!win.Create(L"Системное меню", WS_OVERLAPPEDWINDOW))
  992. {
  993. return 0;
  994. }
  995. ShowWindow(win.Window(), nCmdShow);
  996. // Run the message loop.
  997. MSG msg = {};
  998. while (GetMessage(&msg, NULL, 0, 0))
  999. {
  1000. TranslateMessage(&msg);
  1001. DispatchMessage(&msg);
  1002. }
  1003. return 0;
  1004. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement