Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Lec6Pg3
- basewin.h
- #pragma once
- template <class DERIVED_TYPE>
- class BaseWindow
- {
- public:
- static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- DERIVED_TYPE *pThis = NULL;
- if (uMsg == WM_NCCREATE)
- {
- CREATESTRUCT* pCreate = (CREATESTRUCT*)lParam;
- pThis = (DERIVED_TYPE*)pCreate->lpCreateParams;
- SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)pThis);
- pThis->m_hwnd = hwnd;
- }
- else
- {
- pThis = (DERIVED_TYPE*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
- }
- if (pThis)
- {
- return pThis->HandleMessage(uMsg, wParam, lParam);
- }
- else
- {
- return DefWindowProc(hwnd, uMsg, wParam, lParam);
- }
- }
- BaseWindow() : m_hwnd(NULL) { }
- BOOL Create(
- PCWSTR lpWindowName,
- DWORD dwStyle,
- DWORD dwExStyle = 0,
- int x = CW_USEDEFAULT,
- int y = CW_USEDEFAULT,
- int nWidth = CW_USEDEFAULT,
- int nHeight = CW_USEDEFAULT,
- HWND hWndParent = 0,
- HMENU hMenu = 0
- )
- {
- WNDCLASS wc = { 0 };
- wc.lpfnWndProc = DERIVED_TYPE::WindowProc;
- wc.hInstance = GetModuleHandle(NULL);
- wc.lpszClassName = ClassName();
- //wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- //wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
- RegisterClass(&wc);
- m_hwnd = CreateWindowEx(
- dwExStyle, ClassName(), lpWindowName, dwStyle, x, y,
- nWidth, nHeight, hWndParent, hMenu, GetModuleHandle(NULL), this
- );
- return (m_hwnd ? TRUE : FALSE);
- }
- HWND Window() const { return m_hwnd; }
- protected:
- virtual PCWSTR ClassName() const = 0;
- HWND m_hwnd;
- };
- MainWindow.cpp
- #include <Windows.h>
- #include "MainWindow.h"
- struct
- {
- int iStyle;
- const TCHAR * szText;
- }
- but[] =
- {
- BS_PUSHBUTTON, TEXT("PUSHBUTTON"),
- BS_DEFPUSHBUTTON, TEXT("DEFPUSHBUTTON"),
- BS_CHECKBOX, TEXT("CHECKBOX"),
- BS_AUTOCHECKBOX, TEXT("AUTOCHECKBOX"),
- BS_RADIOBUTTON, TEXT("RADIOBUTTON"),
- BS_3STATE, TEXT("3STATE"),
- BS_AUTO3STATE, TEXT("AUTO3STATE"),
- BS_GROUPBOX, TEXT("GROUPBOX"),
- BS_AUTORADIOBUTTON, TEXT("AUTORADIO"),
- BS_OWNERDRAW, TEXT("OWNERDRAW")
- };
- #define NUM (sizeof but / sizeof but[0])
- bool MainWindow::OnCreate(LPARAM lParam)
- {
- cxChar = LOWORD(GetDialogBaseUnits());
- cyChar = HIWORD(GetDialogBaseUnits());
- for (int i = 0; i < NUM; i++)
- CreateWindow(TEXT("button"),
- but[i].szText,
- WS_CHILD | WS_VISIBLE | but[i].iStyle,
- cxChar, cyChar * (1 + 2 * i),
- 20 * cxChar, 7 * cyChar / 4,
- m_hwnd, (HMENU)i,
- ((LPCREATESTRUCT)lParam)->hInstance, NULL);
- hwndList = CreateWindow(TEXT("listbox"), NULL,
- WS_CHILD | WS_VISIBLE,
- 0, 0,
- 0,
- 0,
- m_hwnd, (HMENU)NUM,
- (HINSTANCE)GetWindowLong(m_hwnd, GWL_HINSTANCE),
- NULL);
- return true;
- }
- LRESULT MainWindow::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- switch (uMsg)
- {
- case WM_CREATE:
- if (!OnCreate(lParam))
- {
- return false; // Неудача CreateWindowEx.
- }
- return 0;
- case WM_SIZE:
- int cxClient, cyClient;
- cxClient = LOWORD(lParam);
- cyClient = HIWORD(lParam);
- MoveWindow(hwndList, 24 * cxChar, 0, cxClient - (24 * cxChar), cyClient, TRUE);
- return 0;
- case WM_DRAWITEM:
- case WM_COMMAND:
- {
- int wmId = LOWORD(wParam);
- if (wmId >= 0 && wmId < NUM)
- {
- TCHAR szFormat[] = TEXT("%-16s%04X-%04X %04X-%04X"),
- szBuffer[50];
- wsprintf(szBuffer, szFormat,
- uMsg == WM_DRAWITEM ? TEXT("WM_DRAWITEM") :
- TEXT("WM_COMMAND"),
- HIWORD(wParam), LOWORD(wParam),
- HIWORD(lParam), LOWORD(lParam));
- SendMessage(hwndList, LB_ADDSTRING, 0, (LPARAM)szBuffer);
- return 0;
- }
- }
- case WM_DESTROY:
- PostQuitMessage(0);
- return 0;
- }
- return DefWindowProc(m_hwnd, uMsg, wParam, lParam);
- }
- MainWindow.h
- #pragma once
- #include "basewin.h"
- class MainWindow : public BaseWindow<MainWindow> {
- int cxChar, cyChar;
- HWND hwndList;
- public:
- bool OnCreate(LPARAM lParam);
- PCWSTR ClassName() const { return L"BtnLook Window Class"; }
- LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);
- };
- Source.cpp
- #include <windows.h>
- #include "MainWindow.h"
- int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
- {
- MainWindow win;
- if (!win.Create(L"Кнопки", WS_OVERLAPPEDWINDOW))
- {
- return 0;
- }
- ShowWindow(win.Window(), nCmdShow);
- // Run the message loop.
- MSG msg = {};
- while (GetMessage(&msg, NULL, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return 0;
- }
- Lec6Pg3a
- basewin.h
- #pragma once
- template <class DERIVED_TYPE>
- class BaseWindow
- {
- public:
- static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- DERIVED_TYPE *pThis = NULL;
- if (uMsg == WM_NCCREATE)
- {
- CREATESTRUCT* pCreate = (CREATESTRUCT*)lParam;
- pThis = (DERIVED_TYPE*)pCreate->lpCreateParams;
- SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)pThis);
- pThis->m_hwnd = hwnd;
- }
- else
- {
- pThis = (DERIVED_TYPE*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
- }
- if (pThis)
- {
- return pThis->HandleMessage(uMsg, wParam, lParam);
- }
- else
- {
- return DefWindowProc(hwnd, uMsg, wParam, lParam);
- }
- }
- BaseWindow() : m_hwnd(NULL) { }
- BOOL Create(
- PCWSTR lpWindowName,
- DWORD dwStyle,
- DWORD dwExStyle = 0,
- int x = CW_USEDEFAULT,
- int y = CW_USEDEFAULT,
- int nWidth = CW_USEDEFAULT,
- int nHeight = CW_USEDEFAULT,
- HWND hWndParent = 0,
- HMENU hMenu = 0
- )
- {
- WNDCLASS wc = { 0 };
- wc.lpfnWndProc = DERIVED_TYPE::WindowProc;
- wc.hInstance = GetModuleHandle(NULL);
- wc.lpszClassName = ClassName();
- //wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
- RegisterClass(&wc);
- m_hwnd = CreateWindowEx(
- dwExStyle, ClassName(), lpWindowName, dwStyle, x, y,
- nWidth, nHeight, hWndParent, hMenu, GetModuleHandle(NULL), this
- );
- return (m_hwnd ? TRUE : FALSE);
- }
- HWND Window() const { return m_hwnd; }
- protected:
- virtual PCWSTR ClassName() const = 0;
- HWND m_hwnd;
- };
- MainWindow.cpp
- #include <Windows.h>
- #include "MainWindow.h"
- struct
- {
- int iStyle;
- const TCHAR * szText;
- }
- but[] =
- {
- BS_PUSHBUTTON, TEXT("PUSHBUTTON"),
- BS_DEFPUSHBUTTON, TEXT("DEFPUSHBUTTON"),
- BS_CHECKBOX, TEXT("CHECKBOX"),
- BS_AUTOCHECKBOX, TEXT("AUTOCHECKBOX"),
- BS_RADIOBUTTON, TEXT("RADIOBUTTON"),
- BS_3STATE, TEXT("3STATE"),
- BS_AUTO3STATE, TEXT("AUTO3STATE"),
- BS_GROUPBOX, TEXT("GROUPBOX"),
- BS_AUTORADIOBUTTON, TEXT("AUTORADIO"),
- BS_OWNERDRAW, TEXT("OWNERDRAW")
- };
- #define NUM (sizeof but / sizeof but[0])
- bool MainWindow::OnCreate(LPARAM lParam)
- {
- cxChar = LOWORD(GetDialogBaseUnits());
- cyChar = HIWORD(GetDialogBaseUnits());
- for (int i = 0; i < NUM; i++)
- CreateWindow(TEXT("button"),
- but[i].szText,
- WS_CHILD | WS_VISIBLE | but[i].iStyle,
- cxChar, cyChar * (1 + 2 * i),
- 20 * cxChar, 7 * cyChar / 4,
- m_hwnd, (HMENU)i,
- ((LPCREATESTRUCT)lParam)->hInstance, NULL);
- hwndList = CreateWindow(TEXT("listbox"), NULL,
- WS_CHILD | WS_VISIBLE,
- 0, 0,
- 0,
- 0,
- m_hwnd, (HMENU)NUM,
- (HINSTANCE)GetWindowLong(m_hwnd, GWL_HINSTANCE),
- NULL);
- return true;
- }
- LRESULT MainWindow::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- switch (uMsg)
- {
- case WM_CREATE:
- if (!OnCreate(lParam))
- {
- return false; // Неудача CreateWindowEx.
- }
- return 0;
- case WM_SIZE:
- int cxClient, cyClient;
- cxClient = LOWORD(lParam);
- cyClient = HIWORD(lParam);
- MoveWindow(hwndList, 24 * cxChar, 0, cxClient - (24 * cxChar), cyClient, TRUE);
- return 0;
- case WM_DRAWITEM:
- case WM_COMMAND:
- {
- int wmId = LOWORD(wParam);
- if (wmId >= 0 && wmId < NUM)
- {
- TCHAR szFormat[] = TEXT("%-16s%04X-%04X %04X-%04X"),
- szBuffer[50];
- wsprintf(szBuffer, szFormat,
- uMsg == WM_DRAWITEM ? TEXT("WM_DRAWITEM") :
- TEXT("WM_COMMAND"),
- HIWORD(wParam), LOWORD(wParam),
- HIWORD(lParam), LOWORD(lParam));
- SendMessage(hwndList, LB_ADDSTRING, 0, (LPARAM)szBuffer);
- return 0;
- }
- }
- case WM_DESTROY:
- PostQuitMessage(0);
- return 0;
- }
- return DefWindowProc(m_hwnd, uMsg, wParam, lParam);
- }
- MainWindow.h
- #pragma once
- #include "basewin.h"
- class MainWindow : public BaseWindow<MainWindow> {
- int cxChar, cyChar;
- HWND hwndList;
- public:
- bool OnCreate(LPARAM lParam);
- PCWSTR ClassName() const { return L"BtnLook Window Class"; }
- LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);
- };
- Source.cpp
- #include <windows.h>
- #include "MainWindow.h"
- int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
- {
- MainWindow win;
- if (!win.Create(L"Кнопки", WS_OVERLAPPEDWINDOW))
- {
- return 0;
- }
- ShowWindow(win.Window(), nCmdShow);
- // Run the message loop.
- MSG msg = {};
- while (GetMessage(&msg, NULL, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return 0;
- }
- Lec6Pg19
- basewin.h
- #pragma once
- template <class DERIVED_TYPE>
- class BaseWindow
- {
- public:
- static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- DERIVED_TYPE *pThis = NULL;
- if (uMsg == WM_NCCREATE)
- {
- CREATESTRUCT* pCreate = (CREATESTRUCT*)lParam;
- pThis = (DERIVED_TYPE*)pCreate->lpCreateParams;
- SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)pThis);
- pThis->m_hwnd = hwnd;
- }
- else
- {
- pThis = (DERIVED_TYPE*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
- }
- if (pThis)
- {
- return pThis->HandleMessage(uMsg, wParam, lParam);
- }
- else
- {
- return DefWindowProc(hwnd, uMsg, wParam, lParam);
- }
- }
- BaseWindow() : m_hwnd(NULL) { }
- BOOL Create(
- PCWSTR lpWindowName,
- DWORD dwStyle,
- DWORD dwExStyle = 0,
- int x = CW_USEDEFAULT,
- int y = CW_USEDEFAULT,
- int nWidth = CW_USEDEFAULT,
- int nHeight = CW_USEDEFAULT,
- HWND hWndParent = 0,
- HMENU hMenu = 0
- )
- {
- WNDCLASS wc = { 0 };
- wc.lpfnWndProc = DERIVED_TYPE::WindowProc;
- wc.hInstance = GetModuleHandle(NULL);
- wc.lpszClassName = ClassName();
- wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
- wc.lpszMenuName = MAKEINTRESOURCE(IDC_MENUDEMO);
- RegisterClass(&wc);
- m_hwnd = CreateWindowEx(
- dwExStyle, ClassName(), lpWindowName, dwStyle, x, y,
- nWidth, nHeight, hWndParent, hMenu, GetModuleHandle(NULL), this
- );
- return (m_hwnd ? TRUE : FALSE);
- }
- HWND Window() const { return m_hwnd; }
- protected:
- virtual PCWSTR ClassName() const = 0;
- HWND m_hwnd;
- };
- MainWindow.cpp
- #include <Windows.h>
- #include "MainWindow.h"
- #define ID_TIMER 1
- static int idColor[5] = { WHITE_BRUSH, LTGRAY_BRUSH, GRAY_BRUSH,
- DKGRAY_BRUSH, BLACK_BRUSH };
- static int iSelection = ID_BACKGROUND_WHITE;
- LRESULT MainWindow::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- switch (uMsg)
- {
- case WM_TIMER:
- MessageBeep(0);
- return 0;
- case WM_COMMAND:
- {
- HMENU hMenu = GetMenu(m_hwnd);
- int wmId = LOWORD(wParam);
- switch (wmId)
- {
- case ID_FILE_NEW:
- case ID_FILE_OPEN:
- case ID_FILE_SAVE:
- case ID_FILE_SAVEAS:
- MessageBeep(0);
- break;
- case IDM_EXIT:
- SendMessage(m_hwnd, WM_CLOSE, 0, 0);
- break;
- case ID_EDIT_UNDO:
- case ID_EDIT_CUT:
- case ID_EDIT_COPY:
- case ID_EDIT_PASTE:
- case ID_EDIT_DELETE:
- MessageBeep(0);
- break;
- case ID_BACKGROUND_WHITE: // Note: Logic below
- case ID_BACKGROUND_LIGHTGREY: // assumes that IDM_WHITE
- case ID_BACKGROUND_GRAY: // through IDM_BLACK are
- case ID_BACKGROUND_DARKGRAY: // consecutive numbers in
- case ID_BACKGROUND_BLACK: // the order shown here.
- CheckMenuItem(hMenu, iSelection, MF_UNCHECKED);
- iSelection = wmId;
- CheckMenuItem(hMenu, iSelection, MF_CHECKED);
- SetClassLong(m_hwnd, GCL_HBRBACKGROUND, (LONG)
- GetStockObject
- (idColor[wmId - ID_BACKGROUND_WHITE]));
- InvalidateRect(m_hwnd, NULL, TRUE);
- break;
- case ID_TIMER_START:
- if (SetTimer(m_hwnd, ID_TIMER, 1000, NULL))
- {
- EnableMenuItem(hMenu, ID_TIMER_START, MF_GRAYED);
- EnableMenuItem(hMenu, ID_TIMER_STOP, MF_ENABLED);
- }
- break;
- case ID_TIMER_STOP:
- KillTimer(m_hwnd, ID_TIMER);
- EnableMenuItem(hMenu, ID_TIMER_START, MF_ENABLED);
- EnableMenuItem(hMenu, ID_TIMER_STOP, MF_GRAYED);
- break;
- case ID_HELP_HELP:
- MessageBox(m_hwnd, TEXT("Help not yet implemented!"),
- TEXT("Меню демо"), MB_ICONEXCLAMATION | MB_OK);
- break;
- default: return DefWindowProc(m_hwnd, uMsg, wParam, lParam);
- }
- return 0;
- }
- case WM_DESTROY:
- PostQuitMessage(0);
- return 0;
- }
- return DefWindowProc(m_hwnd, uMsg, wParam, lParam);
- }
- MainWindow.h
- #pragma once
- #include "Resource.h"
- #include "basewin.h"
- class MainWindow : public BaseWindow<MainWindow> {
- public:
- PCWSTR ClassName() const { return L"MenuDemo Window Class"; }
- LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);
- };
- MenuDemo.rc
- // Microsoft Visual C++ generated resource script.
- //
- #include "resource.h"
- /////////////////////////////////////////////////////////////////////////////
- //
- // Menu
- //
- IDC_MENUDEMO MENU
- BEGIN
- POPUP "&File"
- BEGIN
- MENUITEM "&New", ID_FILE_NEW
- MENUITEM "&Open", ID_FILE_OPEN
- MENUITEM "&Save", ID_FILE_SAVE
- MENUITEM "Save &As...", ID_FILE_SAVEAS
- MENUITEM SEPARATOR
- MENUITEM "E&xit", IDM_EXIT
- END
- POPUP "&Edit"
- BEGIN
- MENUITEM "&Undo", ID_EDIT_UNDO
- MENUITEM SEPARATOR
- MENUITEM "C&ut", ID_EDIT_CUT
- MENUITEM "&Copy", ID_EDIT_COPY
- MENUITEM "&Paste", ID_EDIT_PASTE
- MENUITEM "De&lete", ID_EDIT_DELETE
- END
- POPUP "&Background"
- BEGIN
- MENUITEM "&White", ID_BACKGROUND_WHITE, CHECKED
- MENUITEM "&Light gray", ID_BACKGROUND_LIGHTGREY
- MENUITEM "&Gray", ID_BACKGROUND_GRAY
- MENUITEM "&Dark gray", ID_BACKGROUND_DARKGRAY
- MENUITEM "&Black", ID_BACKGROUND_BLACK
- END
- POPUP "&Timer"
- BEGIN
- MENUITEM "&Start", ID_TIMER_START
- MENUITEM "S&top", ID_TIMER_STOP, INACTIVE
- END
- POPUP "&Help"
- BEGIN
- MENUITEM "&Help...", ID_HELP_HELP
- END
- END
- Resource.h
- //{{NO_DEPENDENCIES}}
- // Microsoft Visual C++ generated include file.
- // Used by MenuDemo.rc
- //
- #define IDC_MYICON 2
- #define IDD_MENUDEMO_DIALOG 102
- #define IDS_APP_TITLE 103
- #define IDD_ABOUTBOX 103
- #define IDM_ABOUT 104
- #define IDM_EXIT 105
- #define IDI_MENUDEMO 107
- #define IDI_SMALL 108
- #define IDC_MENUDEMO 109
- #define IDR_MAINFRAME 128
- #define ID_FILE_NEW 32771
- #define ID_FILE_OPEN 32772
- #define ID_FILE_SAVE 32773
- #define ID_FILE_SAVEAS 32774
- #define ID_EDIT_UNDO 32775
- #define ID_EDIT_CUT 32776
- #define ID_EDIT_COPY 32777
- #define ID_EDIT_PASTE 32778
- #define ID_EDIT_DELETE 32779
- #define ID_BACKGROUND_WHITE 32780
- #define ID_BACKGROUND_LIGHTGREY 32781
- #define ID_BACKGROUND_GRAY 32782
- #define ID_BACKGROUND_DARKGRAY 32783
- #define ID_BACKGROUND_BLACK 32784
- #define ID_TIMER_START 32785
- #define ID_TIMER_STOP 32786
- #define ID_HELP_HELP 32787
- #define IDC_STATIC -1
- // Next default values for new objects
- //
- #ifdef APSTUDIO_INVOKED
- #ifndef APSTUDIO_READONLY_SYMBOLS
- #define _APS_NO_MFC 1
- #define _APS_NEXT_RESOURCE_VALUE 129
- #define _APS_NEXT_COMMAND_VALUE 32788
- #define _APS_NEXT_CONTROL_VALUE 1000
- #define _APS_NEXT_SYMED_VALUE 110
- #endif
- #endif
- Source.cpp
- #include <windows.h>
- #include "MainWindow.h"
- int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
- {
- MainWindow win;
- if (!win.Create(L"Меню демо", WS_OVERLAPPEDWINDOW))
- {
- return 0;
- }
- ShowWindow(win.Window(), nCmdShow);
- // Run the message loop.
- MSG msg = {};
- while (GetMessage(&msg, NULL, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return 0;
- }
- Lec6Pg22
- basewin.h
- #pragma once
- template <class DERIVED_TYPE>
- class BaseWindow
- {
- public:
- static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- DERIVED_TYPE *pThis = NULL;
- if (uMsg == WM_NCCREATE)
- {
- CREATESTRUCT* pCreate = (CREATESTRUCT*)lParam;
- pThis = (DERIVED_TYPE*)pCreate->lpCreateParams;
- SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)pThis);
- pThis->m_hwnd = hwnd;
- }
- else
- {
- pThis = (DERIVED_TYPE*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
- }
- if (pThis)
- {
- return pThis->HandleMessage(uMsg, wParam, lParam);
- }
- else
- {
- return DefWindowProc(hwnd, uMsg, wParam, lParam);
- }
- }
- BaseWindow() : m_hwnd(NULL) { }
- BOOL Create(
- PCWSTR lpWindowName,
- DWORD dwStyle,
- DWORD dwExStyle = 0,
- int x = CW_USEDEFAULT,
- int y = CW_USEDEFAULT,
- int nWidth = CW_USEDEFAULT,
- int nHeight = CW_USEDEFAULT,
- HWND hWndParent = 0,
- HMENU hMenu = 0
- )
- {
- WNDCLASS wc = { 0 };
- wc.lpfnWndProc = DERIVED_TYPE::WindowProc;
- wc.hInstance = GetModuleHandle(NULL);
- wc.lpszClassName = ClassName();
- wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
- //wc.lpszMenuName = MAKEINTRESOURCE(IDC_MENUDEMO);
- RegisterClass(&wc);
- m_hwnd = CreateWindowEx(
- dwExStyle, ClassName(), lpWindowName, dwStyle, x, y,
- nWidth, nHeight, hWndParent, hMenu, GetModuleHandle(NULL), this
- );
- return (m_hwnd ? TRUE : FALSE);
- }
- HWND Window() const { return m_hwnd; }
- protected:
- virtual PCWSTR ClassName() const = 0;
- HWND m_hwnd;
- };
- MainWindow.cpp
- #include <Windows.h>
- #include "MainWindow.h"
- #define ID_TIMER 1
- static int idColor[5] = { WHITE_BRUSH, LTGRAY_BRUSH, GRAY_BRUSH,
- DKGRAY_BRUSH, BLACK_BRUSH };
- static int iSelection = ID_BACKGROUND_WHITE;
- LRESULT MainWindow::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- switch (uMsg)
- {
- case WM_CREATE:
- hMenu = LoadMenu(((LPCREATESTRUCT)lParam)->hInstance, MAKEINTRESOURCE(IDC_POPMENU));
- hMenu = GetSubMenu(hMenu, 0);
- break;
- case WM_RBUTTONUP:
- POINT point;
- point.x = LOWORD(lParam);
- point.y = HIWORD(lParam);
- ClientToScreen(m_hwnd, &point);
- TrackPopupMenu(hMenu, TPM_RIGHTBUTTON, point.x, point.y,
- 0, m_hwnd, NULL);
- break;
- case WM_TIMER:
- MessageBeep(0);
- return 0;
- case WM_COMMAND:
- {
- int wmId = LOWORD(wParam);
- switch (wmId)
- {
- case ID_FILE_NEW:
- case ID_FILE_OPEN:
- case ID_FILE_SAVE:
- case ID_FILE_SAVEAS:
- MessageBeep(0);
- break;
- case IDM_EXIT:
- SendMessage(m_hwnd, WM_CLOSE, 0, 0);
- break;
- case ID_EDIT_UNDO:
- case ID_EDIT_CUT:
- case ID_EDIT_COPY:
- case ID_EDIT_PASTE:
- case ID_EDIT_DELETE:
- MessageBeep(0);
- break;
- case ID_BACKGROUND_WHITE: // Note: Logic below
- case ID_BACKGROUND_LIGHTGREY: // assumes that IDM_WHITE
- case ID_BACKGROUND_GRAY: // through IDM_BLACK are
- case ID_BACKGROUND_DARKGRAY: // consecutive numbers in
- case ID_BACKGROUND_BLACK: // the order shown here.
- CheckMenuItem(hMenu, iSelection, MF_UNCHECKED);
- iSelection = wmId;
- CheckMenuItem(hMenu, iSelection, MF_CHECKED);
- SetClassLong(m_hwnd, GCL_HBRBACKGROUND, (LONG)
- GetStockObject
- (idColor[wmId - ID_BACKGROUND_WHITE]));
- InvalidateRect(m_hwnd, NULL, TRUE);
- break;
- case ID_TIMER_START:
- if (SetTimer(m_hwnd, ID_TIMER, 1000, NULL))
- {
- EnableMenuItem(hMenu, ID_TIMER_START, MF_GRAYED);
- EnableMenuItem(hMenu, ID_TIMER_STOP, MF_ENABLED);
- }
- break;
- case ID_TIMER_STOP:
- KillTimer(m_hwnd, ID_TIMER);
- EnableMenuItem(hMenu, ID_TIMER_START, MF_ENABLED);
- EnableMenuItem(hMenu, ID_TIMER_STOP, MF_GRAYED);
- break;
- case ID_HELP_HELP:
- MessageBox(m_hwnd, TEXT("Help not yet implemented!"),
- TEXT("Всплывающее меню"), MB_ICONEXCLAMATION | MB_OK);
- break;
- default: return DefWindowProc(m_hwnd, uMsg, wParam, lParam);
- }
- return 0;
- }
- case WM_DESTROY:
- PostQuitMessage(0);
- return 0;
- }
- return DefWindowProc(m_hwnd, uMsg, wParam, lParam);
- }
- MainWindow.h
- #pragma once
- #include "Resource.h"
- #include "basewin.h"
- class MainWindow : public BaseWindow<MainWindow> {
- HMENU hMenu;
- public:
- PCWSTR ClassName() const { return L"Popmenu Window Class"; }
- LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);
- };
- MenuDemo.rc
- // Microsoft Visual C++ generated resource script.
- //
- #include "resource.h"
- /////////////////////////////////////////////////////////////////////////////
- //
- // Menu
- //
- IDC_POPMENU MENU
- BEGIN
- POPUP "MyMenu"
- BEGIN
- POPUP "&File"
- BEGIN
- MENUITEM "&New", ID_FILE_NEW
- MENUITEM "&Open", ID_FILE_OPEN
- MENUITEM "&Save", ID_FILE_SAVE
- MENUITEM "Save &As...", ID_FILE_SAVEAS
- MENUITEM SEPARATOR
- MENUITEM "E&xit", IDM_EXIT
- END
- POPUP "&Edit"
- BEGIN
- MENUITEM "&Undo", ID_EDIT_UNDO
- MENUITEM SEPARATOR
- MENUITEM "C&ut", ID_EDIT_CUT
- MENUITEM "&Copy", ID_EDIT_COPY
- MENUITEM "&Paste", ID_EDIT_PASTE
- MENUITEM "De&lete", ID_EDIT_DELETE
- END
- POPUP "&Background"
- BEGIN
- MENUITEM "&White", ID_BACKGROUND_WHITE, CHECKED
- MENUITEM "&Light gray", ID_BACKGROUND_LIGHTGREY
- MENUITEM "&Gray", ID_BACKGROUND_GRAY
- MENUITEM "&Dark gray", ID_BACKGROUND_DARKGRAY
- MENUITEM "&Black", ID_BACKGROUND_BLACK
- END
- POPUP "&Timer"
- BEGIN
- MENUITEM "&Start", ID_TIMER_START
- MENUITEM "S&top", ID_TIMER_STOP, INACTIVE
- END
- POPUP "&Help"
- BEGIN
- MENUITEM "&Help...", ID_HELP_HELP
- END
- END
- END
- Resource.h
- //{{NO_DEPENDENCIES}}
- // Microsoft Visual C++ generated include file.
- // Used by PopMenu.rc
- //
- #define IDC_MYICON 2
- #define IDD_POPMENU_DIALOG 102
- #define IDS_APP_TITLE 103
- #define IDD_ABOUTBOX 103
- #define IDM_ABOUT 104
- #define IDM_EXIT 105
- #define IDI_POPMENU 107
- #define IDI_SMALL 108
- #define IDC_POPMENU 109
- #define IDR_MAINFRAME 128
- #define ID_FILE_NEW 32771
- #define ID_FILE_OPEN 32772
- #define ID_FILE_SAVE 32773
- #define ID_FILE_SAVEAS 32774
- #define ID_EDIT_UNDO 32775
- #define ID_EDIT_CUT 32776
- #define ID_EDIT_COPY 32777
- #define ID_EDIT_PASTE 32778
- #define ID_EDIT_DELETE 32779
- #define ID_BACKGROUND_WHITE 32780
- #define ID_BACKGROUND_LIGHTGREY 32781
- #define ID_BACKGROUND_GRAY 32782
- #define ID_BACKGROUND_DARKGRAY 32783
- #define ID_BACKGROUND_BLACK 32784
- #define ID_TIMER_START 32785
- #define ID_TIMER_STOP 32786
- #define ID_HELP_HELP 32787
- #define IDC_STATIC -1
- // Next default values for new objects
- //
- #ifdef APSTUDIO_INVOKED
- #ifndef APSTUDIO_READONLY_SYMBOLS
- #define _APS_NO_MFC 1
- #define _APS_NEXT_RESOURCE_VALUE 129
- #define _APS_NEXT_COMMAND_VALUE 32788
- #define _APS_NEXT_CONTROL_VALUE 1000
- #define _APS_NEXT_SYMED_VALUE 110
- #endif
- #endif
- Source.cpp
- #include <windows.h>
- #include "MainWindow.h"
- int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
- {
- MainWindow win;
- if (!win.Create(L"Всплывающее меню", WS_OVERLAPPEDWINDOW))
- {
- return 0;
- }
- ShowWindow(win.Window(), nCmdShow);
- // Run the message loop.
- MSG msg = {};
- while (GetMessage(&msg, NULL, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return 0;
- }
- Lec6Pg23
- basewin.h
- #pragma once
- template <class DERIVED_TYPE>
- class BaseWindow
- {
- public:
- static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- DERIVED_TYPE *pThis = NULL;
- if (uMsg == WM_NCCREATE)
- {
- CREATESTRUCT* pCreate = (CREATESTRUCT*)lParam;
- pThis = (DERIVED_TYPE*)pCreate->lpCreateParams;
- SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)pThis);
- pThis->m_hwnd = hwnd;
- }
- else
- {
- pThis = (DERIVED_TYPE*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
- }
- if (pThis)
- {
- return pThis->HandleMessage(uMsg, wParam, lParam);
- }
- else
- {
- return DefWindowProc(hwnd, uMsg, wParam, lParam);
- }
- }
- BaseWindow() : m_hwnd(NULL) { }
- BOOL Create(
- PCWSTR lpWindowName,
- DWORD dwStyle,
- DWORD dwExStyle = 0,
- int x = CW_USEDEFAULT,
- int y = CW_USEDEFAULT,
- int nWidth = CW_USEDEFAULT,
- int nHeight = CW_USEDEFAULT,
- HWND hWndParent = 0,
- HMENU hMenu = 0
- )
- {
- WNDCLASS wc = { 0 };
- wc.lpfnWndProc = DERIVED_TYPE::WindowProc;
- wc.hInstance = GetModuleHandle(NULL);
- wc.lpszClassName = ClassName();
- wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
- //wc.lpszMenuName = MAKEINTRESOURCE(IDC_MENUDEMO);
- RegisterClass(&wc);
- m_hwnd = CreateWindowEx(
- dwExStyle, ClassName(), lpWindowName, dwStyle, x, y,
- nWidth, nHeight, hWndParent, hMenu, GetModuleHandle(NULL), this
- );
- return (m_hwnd ? TRUE : FALSE);
- }
- HWND Window() const { return m_hwnd; }
- protected:
- virtual PCWSTR ClassName() const = 0;
- HWND m_hwnd;
- };
- MainWindow.cpp
- #include <Windows.h>
- #include "MainWindow.h"
- #define IDM_SYS_ABOUT 1
- #define IDM_SYS_HELP 2
- #define IDM_SYS_REMOVE 3
- LRESULT MainWindow::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- switch (uMsg)
- {
- case WM_CREATE:
- HMENU hMenu;
- hMenu = GetSystemMenu(m_hwnd, FALSE);
- AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
- AppendMenu(hMenu, MF_STRING, IDM_SYS_ABOUT, TEXT("About..."));
- AppendMenu(hMenu, MF_STRING, IDM_SYS_HELP, TEXT("Help..."));
- AppendMenu(hMenu, MF_STRING, IDM_SYS_REMOVE, TEXT("Remove Additions"));
- return 0;
- case WM_DESTROY:
- PostQuitMessage(0);
- return 0;
- case WM_SYSCOMMAND:
- switch (LOWORD(wParam))
- {
- case IDM_SYS_ABOUT:
- MessageBox(m_hwnd, TEXT("A Poor-Person's Menu Program\n")
- TEXT("(c) TSU, 2019"),
- L"Системное меню", MB_OK | MB_ICONINFORMATION);
- return 0;
- case IDM_SYS_HELP:
- MessageBox(m_hwnd, TEXT("Help not yet implemented!"),
- L"Системное меню", MB_OK | MB_ICONEXCLAMATION);
- return 0;
- case IDM_SYS_REMOVE:
- GetSystemMenu(m_hwnd, TRUE);
- return 0;
- }
- }
- return DefWindowProc(m_hwnd, uMsg, wParam, lParam);
- }
- MainWindow.h
- #pragma once
- #include "basewin.h"
- class MainWindow : public BaseWindow<MainWindow> {
- public:
- PCWSTR ClassName() const { return L"Sysmenu Window Class"; }
- LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);
- };
- Source.cpp
- #include <windows.h>
- #include "MainWindow.h"
- int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
- {
- MainWindow win;
- if (!win.Create(L"Системное меню", WS_OVERLAPPEDWINDOW))
- {
- return 0;
- }
- ShowWindow(win.Window(), nCmdShow);
- // Run the message loop.
- MSG msg = {};
- while (GetMessage(&msg, NULL, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement