|
| 1 | +// MyApplication.cpp : Defines the entry point for the application. |
| 2 | +// |
| 3 | + |
| 4 | +#include "framework.h" |
| 5 | +#include "MyApplication.h" |
| 6 | + |
| 7 | +// Opt-in to version 6.0 of the Common Controls library for modern dialog style |
| 8 | +#pragma comment(linker,"\"/manifestdependency:type='win32' \ |
| 9 | +name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \ |
| 10 | +processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") |
| 11 | + |
| 12 | +#define MAX_LOADSTRING 100 |
| 13 | +#define ID_LISTBOX 101 |
| 14 | + |
| 15 | +#define WINDOW_WIDTH 720 |
| 16 | +#define WINDOW_HEIGHT 480 |
| 17 | + |
| 18 | +// Global Variables: |
| 19 | +HINSTANCE hInst; // current instance |
| 20 | +WCHAR szTitle[MAX_LOADSTRING]; // The title bar text |
| 21 | +WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name |
| 22 | + |
| 23 | +// Forward declarations of functions included in this code module: |
| 24 | +ATOM MyRegisterClass(HINSTANCE hInstance); |
| 25 | +BOOL InitInstance(HINSTANCE, int); |
| 26 | +LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); |
| 27 | +INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); |
| 28 | + |
| 29 | +int APIENTRY wWinMain(_In_ HINSTANCE hInstance, |
| 30 | + _In_opt_ HINSTANCE hPrevInstance, |
| 31 | + _In_ LPWSTR lpCmdLine, |
| 32 | + _In_ int nCmdShow) |
| 33 | +{ |
| 34 | + UNREFERENCED_PARAMETER(hPrevInstance); |
| 35 | + UNREFERENCED_PARAMETER(lpCmdLine); |
| 36 | + |
| 37 | + // TODO: Place code here. |
| 38 | + |
| 39 | + // Initialize global strings |
| 40 | + LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); |
| 41 | + LoadStringW(hInstance, IDC_MYAPPLICATION, szWindowClass, MAX_LOADSTRING); |
| 42 | + MyRegisterClass(hInstance); |
| 43 | + |
| 44 | + // Perform application initialization: |
| 45 | + if (!InitInstance (hInstance, nCmdShow)) |
| 46 | + { |
| 47 | + return FALSE; |
| 48 | + } |
| 49 | + |
| 50 | + HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MYAPPLICATION)); |
| 51 | + |
| 52 | + MSG msg; |
| 53 | + |
| 54 | + // Main message loop: |
| 55 | + while (GetMessage(&msg, nullptr, 0, 0)) |
| 56 | + { |
| 57 | + if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) |
| 58 | + { |
| 59 | + TranslateMessage(&msg); |
| 60 | + DispatchMessage(&msg); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return (int) msg.wParam; |
| 65 | +} |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | +// |
| 70 | +// FUNCTION: MyRegisterClass() |
| 71 | +// |
| 72 | +// PURPOSE: Registers the window class. |
| 73 | +// |
| 74 | +ATOM MyRegisterClass(HINSTANCE hInstance) |
| 75 | +{ |
| 76 | + WNDCLASSEXW wcex; |
| 77 | + |
| 78 | + wcex.cbSize = sizeof(WNDCLASSEX); |
| 79 | + |
| 80 | + wcex.style = CS_HREDRAW | CS_VREDRAW; |
| 81 | + wcex.lpfnWndProc = WndProc; |
| 82 | + wcex.cbClsExtra = 0; |
| 83 | + wcex.cbWndExtra = 0; |
| 84 | + wcex.hInstance = hInstance; |
| 85 | + wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MYAPPLICATION)); |
| 86 | + wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); |
| 87 | + wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); |
| 88 | + wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_MYAPPLICATION); |
| 89 | + wcex.lpszClassName = szWindowClass; |
| 90 | + wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); |
| 91 | + |
| 92 | + return RegisterClassExW(&wcex); |
| 93 | +} |
| 94 | + |
| 95 | +// |
| 96 | +// FUNCTION: InitInstance(HINSTANCE, int) |
| 97 | +// |
| 98 | +// PURPOSE: Saves instance handle and creates main window |
| 99 | +// |
| 100 | +// COMMENTS: |
| 101 | +// |
| 102 | +// In this function, we save the instance handle in a global variable and |
| 103 | +// create and display the main program window. |
| 104 | +// |
| 105 | +BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) |
| 106 | +{ |
| 107 | + hInst = hInstance; // Store instance handle in our global variable |
| 108 | + |
| 109 | + HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, |
| 110 | + CW_USEDEFAULT, 0, WINDOW_WIDTH, WINDOW_HEIGHT, nullptr, nullptr, hInstance, nullptr); |
| 111 | + |
| 112 | + if (!hWnd) |
| 113 | + { |
| 114 | + return FALSE; |
| 115 | + } |
| 116 | + |
| 117 | + ShowWindow(hWnd, nCmdShow); |
| 118 | + UpdateWindow(hWnd); |
| 119 | + |
| 120 | + return TRUE; |
| 121 | +} |
| 122 | + |
| 123 | +// |
| 124 | +// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM) |
| 125 | +// |
| 126 | +// PURPOSE: Processes messages for the main window. |
| 127 | +// |
| 128 | +// WM_COMMAND - process the application menu |
| 129 | +// WM_PAINT - Paint the main window |
| 130 | +// WM_DESTROY - post a quit message and return |
| 131 | +// |
| 132 | +// |
| 133 | +LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) |
| 134 | +{ |
| 135 | + static HWND hListBox; |
| 136 | + static HFONT hFont; |
| 137 | + constexpr const auto LISTBOX_MARGIN = 10; |
| 138 | + constexpr const auto LISTBOX_WIDTH_OFFSET = 2 * LISTBOX_MARGIN; |
| 139 | + constexpr const auto LISTBOX_HEIGHT_OFFSET = 2 * LISTBOX_MARGIN + 16; |
| 140 | + |
| 141 | + switch (message) |
| 142 | + { |
| 143 | + case WM_CREATE: |
| 144 | + { |
| 145 | + // Set font |
| 146 | + hFont = CreateFont( |
| 147 | + 18, 0, 0, 0, FW_NORMAL, false, false, false, |
| 148 | + DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, |
| 149 | + L"Segoe UI" |
| 150 | + ); |
| 151 | + |
| 152 | + // Create ListBox |
| 153 | + hListBox = CreateWindowExW( |
| 154 | + WS_EX_CLIENTEDGE, |
| 155 | + L"LISTBOX", |
| 156 | + nullptr, |
| 157 | + WS_CHILD | WS_VISIBLE | WS_VSCROLL | LBS_NOTIFY, |
| 158 | + LISTBOX_MARGIN, LISTBOX_MARGIN + 20, |
| 159 | + WINDOW_WIDTH - LISTBOX_WIDTH_OFFSET, WINDOW_HEIGHT - LISTBOX_HEIGHT_OFFSET, |
| 160 | + hWnd, |
| 161 | + (HMENU)ID_LISTBOX, |
| 162 | + (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), |
| 163 | + nullptr |
| 164 | + ); |
| 165 | + SendMessage(hListBox, WM_SETFONT, (WPARAM)hFont, true); |
| 166 | + |
| 167 | + // Fill ListBox with command line parameters |
| 168 | + for (int i = 0; i < __argc; ++i) { |
| 169 | + SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)__wargv[i]); |
| 170 | + } |
| 171 | + } |
| 172 | + break; |
| 173 | + case WM_COMMAND: |
| 174 | + { |
| 175 | + int wmId = LOWORD(wParam); |
| 176 | + // Parse the menu selections: |
| 177 | + switch (wmId) |
| 178 | + { |
| 179 | + case IDM_ABOUT: |
| 180 | + DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); |
| 181 | + break; |
| 182 | + case IDM_EXIT: |
| 183 | + DestroyWindow(hWnd); |
| 184 | + break; |
| 185 | + default: |
| 186 | + return DefWindowProc(hWnd, message, wParam, lParam); |
| 187 | + } |
| 188 | + } |
| 189 | + break; |
| 190 | + case WM_PAINT: |
| 191 | + { |
| 192 | + PAINTSTRUCT ps; |
| 193 | + HDC hdc = BeginPaint(hWnd, &ps); |
| 194 | + // TODO: Add any drawing code that uses hdc here... |
| 195 | + |
| 196 | + // Write text |
| 197 | + HFONT hOldFont = (HFONT)SelectObject(hdc, hFont); |
| 198 | + TextOut(hdc, 10, 8, L"Command line arguments:", 23); |
| 199 | + SelectObject(hdc, hOldFont); |
| 200 | + |
| 201 | + EndPaint(hWnd, &ps); |
| 202 | + } |
| 203 | + break; |
| 204 | + case WM_SIZE: |
| 205 | + { |
| 206 | + // Resize ListBox when window resized |
| 207 | + MoveWindow( |
| 208 | + hListBox, |
| 209 | + LISTBOX_MARGIN, LISTBOX_MARGIN + 20, |
| 210 | + LOWORD(lParam) - LISTBOX_WIDTH_OFFSET, HIWORD(lParam) - LISTBOX_HEIGHT_OFFSET, |
| 211 | + true |
| 212 | + ); |
| 213 | + } |
| 214 | + break; |
| 215 | + case WM_DESTROY: |
| 216 | + DeleteObject(hFont); |
| 217 | + PostQuitMessage(0); |
| 218 | + break; |
| 219 | + default: |
| 220 | + return DefWindowProc(hWnd, message, wParam, lParam); |
| 221 | + } |
| 222 | + return 0; |
| 223 | +} |
| 224 | + |
| 225 | +// Message handler for about box. |
| 226 | +INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) |
| 227 | +{ |
| 228 | + UNREFERENCED_PARAMETER(lParam); |
| 229 | + switch (message) |
| 230 | + { |
| 231 | + case WM_INITDIALOG: |
| 232 | + return (INT_PTR)TRUE; |
| 233 | + |
| 234 | + case WM_COMMAND: |
| 235 | + if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) |
| 236 | + { |
| 237 | + EndDialog(hDlg, LOWORD(wParam)); |
| 238 | + return (INT_PTR)TRUE; |
| 239 | + } |
| 240 | + break; |
| 241 | + } |
| 242 | + return (INT_PTR)FALSE; |
| 243 | +} |
0 commit comments