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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
| #include <Windows.h> #include <commctrl.h> #include <strsafe.h>
HANDLE hOutput; void consoleInit(void) { AllocConsole(); SetConsoleTitle(TEXT("Debug Window")); hOutput = GetStdHandle(STD_OUTPUT_HANDLE); }
HANDLE hThread1; HANDLE hThread2; DWORD CALLBACK ThreadProc1(LPVOID pParam) { char *pszText = (char *)pParam; for (int i = 0; i < 99; i++) { WriteConsole(hOutput, pszText, 9, NULL, NULL); WriteConsole(hOutput, TEXT("\n"), 1, NULL, NULL); Sleep(1000); } return 0; } DWORD CALLBACK ThreadProc2(LPVOID pParam) { char *pszText = (char *)pParam; for (int i = 0; i < 99; i++) { WriteConsole(hOutput, pszText, 9, NULL, NULL); WriteConsole(hOutput, TEXT("\n"), 1, NULL, NULL); Sleep(1000); } return 0; }
#define IDM_OPEN 301 #define IDM_CLOSE 302 #define IDM_EXIT 303 #define IDM_FIND 304 #define IDM_RUN 305 #define IDM_STOP 306 #define IDM_OPTIONS 307 #define IDM_ABOUT 308 class WinMenu { public: WinMenu() {} ~WinMenu() {} inline BOOL createMenu(HWND hWnd) { HMENU hMenu = CreateMenu(); if (!hMenu) return -1; HMENU pop1 = CreatePopupMenu(); HMENU pop2 = CreatePopupMenu(); HMENU pop3 = CreatePopupMenu(); HMENU pop4 = CreatePopupMenu(); HMENU pop5 = CreatePopupMenu(); AppendMenu(hMenu, MF_POPUP, (UINT_PTR)pop1, TEXT("File")); AppendMenu(hMenu, MF_POPUP, (UINT_PTR)pop2, TEXT("Find")); AppendMenu(hMenu, MF_POPUP, (UINT_PTR)pop3, TEXT("Run")); AppendMenu(hMenu, MF_POPUP, (UINT_PTR)pop4, TEXT("Tools")); AppendMenu(hMenu, MF_POPUP, (UINT_PTR)pop5, TEXT("Help"));
AppendMenu(pop1, MF_STRING, IDM_OPEN, TEXT("Open")); AppendMenu(pop1, MF_STRING, IDM_CLOSE, TEXT("Close")); AppendMenu(pop2, MF_STRING, IDM_FIND, TEXT("Find")); AppendMenu(pop3, MF_STRING, IDM_RUN, TEXT("Run")); AppendMenu(pop3, MF_STRING, IDM_STOP, TEXT("Stop")); AppendMenu(pop4, MF_STRING, IDM_OPTIONS, TEXT("Options")); AppendMenu(pop5, MF_STRING, IDM_ABOUT, TEXT("About"));
SetMenu(hWnd, hMenu); return TRUE; } };
#define IDB_BUTTON_START 111 #define IDB_BUTTON_STOP 112 #define IDB_BUTTON_OPEN 113 #define IDB_BUTTON_CLEAN 114 class WinButton { public: WinButton() {} ~WinButton() {} inline BOOL createButton(HWND hWnd, TCHAR title[], INT x = 0, INT y = 0, HMENU IDB_BUTTON = NULL, INT width = 100, INT height = 50) { CreateWindow(WC_BUTTON, title, WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, x, y, width, height, hWnd, IDB_BUTTON, NULL, NULL); return TRUE; } inline BOOL onClick(HWND hWnd, WPARAM wParam, LPARAM lParam) { switch (LOWORD(wParam)) { case IDB_BUTTON_START: { HDC hdc = GetDC(hWnd); HPEN hPen = CreatePen(PS_NULL, 1, RGB(0, 0, 0)); HPEN holdPen = (HPEN)SelectObject(hdc, hPen); HBRUSH hBrush = CreateSolidBrush(RGB(0, 255, 0)); HBRUSH holdBrush = (HBRUSH)SelectObject(hdc, hBrush);
Ellipse(hdc, 10 + 25, 10, 60 + 25, 60);
SelectObject(hdc, holdPen); SelectObject(hdc, holdBrush);
DeleteObject(hPen); DeleteObject(hBrush); ReleaseDC(hWnd, hdc);
consoleInit(); ResumeThread(hThread1); ResumeThread(hThread2); } break; case IDB_BUTTON_STOP: { FreeConsole(); SuspendThread(hThread1); SuspendThread(hThread2);
HDC hdc = GetDC(hWnd); HPEN hPen = CreatePen(PS_NULL, 1, RGB(0, 0, 0)); HPEN holdPen = (HPEN)SelectObject(hdc, hPen); HBRUSH hBrush = CreateSolidBrush(RGB(255, 0, 0)); HBRUSH holdBrush = (HBRUSH)SelectObject(hdc, hBrush);
Ellipse(hdc, 10 + 25, 10, 60 + 25, 60);
SelectObject(hdc, holdPen); SelectObject(hdc, holdBrush);
DeleteObject(hPen); DeleteObject(hBrush); ReleaseDC(hWnd, hdc); } break; case IDB_BUTTON_OPEN: {
MessageBox(hWnd, TEXT("click113"), TEXT("prompt"), MB_OK | MB_ICONINFORMATION); } break; case IDB_BUTTON_CLEAN: {
MessageBox(hWnd, TEXT("click114"), TEXT("prompt"), MB_OK | MB_ICONINFORMATION); } default: { } } return TRUE; } };
class WinEdit { public: WinEdit() {} ~WinEdit() {} inline BOOL createEdit(HWND hWnd, INT x, INT y, INT width = 100, INT height = 50) { m_hEdit = CreateWindow(WC_EDIT, NULL, WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | WS_BORDER | ES_LEFT | ES_MULTILINE | ES_WANTRETURN | ES_NOHIDESEL | ES_AUTOHSCROLL | ES_AUTOVSCROLL, x, y, width, height, hWnd, NULL, NULL, NULL); return TRUE; } inline BOOL appendLogText(HWND hWndLog, LPCTSTR newText) { DWORD left, right; int len = GetWindowTextLength(hWndLog); SendMessage(hWndLog, EM_GETSEL, (WPARAM)&left, (LPARAM)&right); SendMessage(hWndLog, EM_SETSEL, len, len); SendMessage(hWndLog, EM_REPLACESEL, 0, (LPARAM)newText); SendMessage(hWndLog, EM_SETSEL, left, right); return TRUE; } HWND m_hEdit; };
#define IDC_LIST_CLICK 401 class WinListView { public: WinListView() {} ~WinListView() {} inline BOOL createListView(HWND hWnd, INT x, INT y, HMENU IDC_LIST = NULL, INT width = 100, INT height = 50) { m_hListView = CreateWindow(WC_LISTVIEW, TEXT(""), WS_CHILD | WS_VISIBLE | WS_BORDER | LVS_REPORT | LVS_SHOWSELALWAYS, x, y, width, height, hWnd, IDC_LIST, NULL, NULL); ListView_SetExtendedListViewStyle(m_hListView, LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP | LVS_EX_GRIDLINES);
RECT rect; GetClientRect(hWnd, &rect);
colTime.mask |= LVCF_WIDTH | LVCF_TEXT; colTime.cx = (rect.right - rect.left) * 0.15; colTime.pszText = TEXT("time"); ListView_InsertColumn(m_hListView, 0, &colTime);
colContent.mask |= LVCF_WIDTH | LVCF_TEXT; colContent.cx = (rect.right - rect.left) * 0.15; colContent.pszText = TEXT("content"); ListView_InsertColumn(m_hListView, 1, &colContent);
for (int i = 0; i < 10; ++i) { ZeroMemory(&rowInfo, sizeof(rowInfo)); rowInfo.iItem = i; ListView_InsertItem(m_hListView, &rowInfo);
TCHAR szBuffer[128]; StringCchPrintf(szBuffer, 128, TEXT("element %d"), i); ListView_SetItemText(m_hListView, i, 0, szBuffer); ListView_SetItemText(m_hListView, i, 1, szBuffer); }
return TRUE; } inline BOOL onClick(HWND hWnd, WPARAM wParam, LPARAM lParam) { NMHDR *pNmHdr = (NMHDR *)lParam; if (pNmHdr->idFrom == IDC_LIST_CLICK) { if (pNmHdr->code == NM_CLICK) { LPNMITEMACTIVATE lpnmitem = (LPNMITEMACTIVATE)lParam; TCHAR szText[128]; StringCchPrintf(szText, 128, TEXT("click is %d row %d col"), lpnmitem->iItem, lpnmitem->iSubItem); } } return TRUE; } LVCOLUMN colTime = {0}; LVCOLUMN colContent = {0}; LVITEM rowInfo = {0}; HWND m_hListView; };
class WinApp { public: WinApp() {} ~WinApp() {} virtual BOOL init(HINSTANCE hInstance, TCHAR title[]) { WNDCLASS wc; ZeroMemory(&wc, sizeof(wc));
wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hInstance = hInstance; wc.lpfnWndProc = WindowProc; wc.lpszClassName = TEXT("main"); wc.lpszMenuName = NULL; wc.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wc); m_hWnd = CreateWindow(wc.lpszClassName, title, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, wc.hInstance, NULL); CenterWindow(m_hWnd); SetWindowLongPtr(m_hWnd, GWLP_USERDATA, (LONG_PTR)this); RECT rect; GetClientRect(m_hWnd, &rect); this->clientWidth = rect.right - rect.left; this->clientHeight = rect.bottom - rect.top; this->clientSeparator = 10;
ShowWindow(m_hWnd, SW_SHOWNORMAL); UpdateWindow(m_hWnd);
DWORD nID1 = 1; DWORD nID2 = 2; hThread1 = CreateThread(NULL, 0, ThreadProc1, (LPVOID) "********", CREATE_SUSPENDED, &nID1); hThread2 = CreateThread(NULL, 0, ThreadProc2, (LPVOID) "--------", CREATE_SUSPENDED, &nID2);
return TRUE; } virtual BOOL CenterWindow(HWND hWnd) { INT scrWidth, scrHeight; RECT rect; scrWidth = GetSystemMetrics(SM_CXSCREEN); scrHeight = GetSystemMetrics(SM_CYSCREEN); GetWindowRect(hWnd, &rect); long width = rect.right - rect.left; long height = rect.bottom - rect.top; rect.left = (scrWidth - width) / 2; rect.top = (scrHeight - height) / 2;
SetWindowPos(hWnd, HWND_TOP, rect.left, rect.top, width, height, SWP_NOSIZE | SWP_NOZORDER); return TRUE; } static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { WinApp *pWinApp = (WinApp *)GetWindowLongPtr(hWnd, GWLP_USERDATA); if (pWinApp) { return pWinApp->wndProc(uMsg, wParam, lParam); } return DefWindowProc(hWnd, uMsg, wParam, lParam); } virtual LRESULT CALLBACK wndProc(UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_NCCREATE: { } break; case WM_CREATE: { } break; case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(m_hWnd, &ps); HPEN hPen = CreatePen(PS_NULL, 1, RGB(0, 0, 0)); HPEN holdPen = (HPEN)SelectObject(hdc, hPen); HBRUSH hBrush = CreateSolidBrush(RGB(255, 0, 0)); HBRUSH holdBrush = (HBRUSH)SelectObject(hdc, hBrush);
Ellipse(hdc, 10 + 25, 10, 60 + 25, 60);
SelectObject(hdc, holdPen); SelectObject(hdc, holdBrush);
DeleteObject(hPen); DeleteObject(hBrush); EndPaint(m_hWnd, &ps); } break; case WM_SIZE: { MoveWindow(this->m_listView.m_hListView, 10, 70, LOWORD(lParam) * 0.3, HIWORD(lParam) - 80, TRUE);
int x = LOWORD(lParam) * 0.3 + clientSeparator * 2; int cx = LOWORD(lParam) * 0.7 - clientSeparator * 3; MoveWindow(this->m_edit.m_hEdit, x, 70, cx, HIWORD(lParam) - 80, TRUE);
ListView_SetColumnWidth(this->m_listView.m_hListView, 0, LOWORD(lParam) * 0.15); ListView_SetColumnWidth(this->m_listView.m_hListView, 1, LOWORD(lParam) * 0.15); } break; case WM_COMMAND: { this->m_button.onClick(m_hWnd, wParam, lParam); } break; case WM_NOTIFY: { this->m_listView.onClick(m_hWnd, wParam, lParam); } break; case WM_SYSCOMMAND: { if (wParam == SC_CLOSE) { int nRet = MessageBox(m_hWnd, TEXT("Do you want to quit?"), TEXT("prompt"), MB_YESNO | MB_ICONINFORMATION); if (nRet != IDYES) { return 0; } } } break; case WM_CLOSE: { DestroyWindow(m_hWnd); } break; case WM_DESTROY: { PostQuitMessage(0); } break; default: { } } return DefWindowProc(m_hWnd, uMsg, wParam, lParam); } virtual BOOL run() { MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return TRUE; }
HWND m_hWnd; HINSTANCE m_hInstance;
virtual BOOL Menu() { m_menu.createMenu(m_hWnd); return TRUE; } virtual BOOL Button() { m_button.createButton(m_hWnd, TEXT("start"), 110, 10, (HMENU)IDB_BUTTON_START); m_button.createButton(m_hWnd, TEXT("stop"), 220, 10, (HMENU)IDB_BUTTON_STOP); m_button.createButton(m_hWnd, TEXT("open"), 330, 10, (HMENU)IDB_BUTTON_OPEN); m_button.createButton(m_hWnd, TEXT("clean"), 440, 10, (HMENU)IDB_BUTTON_CLEAN); return TRUE; } virtual BOOL Edit() { int x = this->clientWidth * 0.3 + clientSeparator * 2; int y = 70; int cx = this->clientWidth * 0.7 - clientSeparator * 3; int cy = this->clientHeight - 100; m_edit.createEdit(m_hWnd, x, y, cx, cy); return TRUE; } virtual BOOL ListView() { int x = 10; int y = 70; int cx = this->clientWidth * 0.3; int cy = this->clientHeight - 100; m_listView.createListView(m_hWnd, x, y, (HMENU)IDC_LIST_CLICK, cx, cy); return TRUE; } int clientWidth; int clientHeight; int clientSeparator;
WinMenu m_menu; WinButton m_button; WinEdit m_edit; WinListView m_listView; };
|