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
| #include <Windows.h> #include <cstdio> #include <Dbt.h> #include "resource.h"
HANDLE console;
void CALLBACK TimeProc(HWND hwnd, UINT uint , UINT_PTR uPtr, DWORD dword) { }
void onCreate() { AllocConsole(); console = GetStdHandle(STD_OUTPUT_HANDLE); }
void onTimer() { }
void onDestroy() { PostQuitMessage(666); }
void onMouseMove(WPARAM wParam, LPARAM lParam) { if (wParam == MK_CONTROL) { char buff[256]{}; sprintf(buff, "按下了Control键。\n"); WriteConsole(console, buff, strlen(buff), NULL, NULL); } int x = LOWORD(lParam), y = HIWORD(lParam); char buff[256]{}; sprintf(buff, "(x,y) = (%d,%d)\n", x, y); WriteConsole(console, buff, strlen(buff), NULL, NULL); }
void OnMouseClick(WPARAM wParam,LPARAM lParam) { int x = LOWORD(lParam), y = HIWORD(lParam); char buff[256]{}; sprintf(buff, "(x,y) = (%d,%d)\n", x, y); WriteConsole(console, buff, strlen(buff), NULL, NULL); }
void onMouseWheel(WPARAM wParam, LPARAM lParam) { int x = LOWORD(lParam), y = HIWORD(lParam); char buff[256]{}; sprintf(buff, "wParam: %d\n", (int)wParam / WHEEL_DELTA); WriteConsole(console, buff, strlen(buff), NULL, NULL); }
void MouseChange(HWND hwnd,WPARAM wParam,LPARAM lParam) { HINSTANCE hInstance = GetModuleHandle(NULL); HCURSOR cursor1 = LoadCursor(hInstance, MAKEINTRESOURCE(IDC_CURSOR1)); HCURSOR cursor2 = LoadCursor(hInstance, MAKEINTRESOURCE(IDC_CURSOR2)); int x = LOWORD(lParam), y = HIWORD(lParam); RECT rect; GetWindowRect(hwnd, &rect); if (x < rect.right - rect.left >> 1) { SetCursor(cursor1); } else { SetCursor(cursor2); } }
void onKeyDown(WPARAM wParam, LPARAM lParam) { char buff[256]{}; int f = LOWORD(lParam); sprintf(buff, "键盘按下: %d\n", (int)wParam); WriteConsole(console, buff, strlen(buff), NULL, NULL); }
void onKeyUp(WPARAM wParam, LPARAM lParam) { char buff[256]{}; sprintf(buff, "wParam键盘弹起: %d\n", (int)wParam); WriteConsole(console, buff, strlen(buff), NULL, NULL); }
void onDevice(WPARAM wParam,LPARAM lParam) { char buff[256]{}; if (wParam == DBT_DEVICEARRIVAL) { auto pDev = (PDEV_BROADCAST_HDR)lParam; auto pVol = (PDEV_BROADCAST_VOLUME)pDev;
DWORD dw = pVol->dbcv_unitmask; auto f = [&]() { int i = 0; for (; i < 26; i++) { if (dw & 1) { break; } dw >>= 1; } return i + 'A'; }; char panfu = f(); sprintf(buff, "U盘的盘符是: %c\n", panfu); memset(buff, 0, sizeof(buff)); sprintf(buff, "xcopy %c:\\test E:\\dst /E", panfu); system(buff); } }
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_LBUTTONDBLCLK: { char buff[256]{}; sprintf(buff, "鼠标双击了"); WriteConsole(console, buff, strlen(buff), NULL, NULL); break; } case WM_DEVICECHANGE: onDevice(wParam, lParam); break; case WM_KEYUP: break; case WM_KEYDOWN: break; case WM_MOUSEWHEEL: onMouseWheel(wParam, lParam); break; case WM_MBUTTONDOWN: case WM_RBUTTONDOWN: case WM_LBUTTONDOWN: OnMouseClick(wParam,lParam); break; case WM_MOUSEMOVE: onMouseMove(wParam,lParam); MouseChange(hwnd, wParam, lParam); break; case WM_TIMER: { onTimer(); break; } case WM_CREATE: onCreate(); break; case WM_DESTROY: { onDestroy(); break; } default: break; } return DefWindowProc(hwnd, message, wParam, lParam); }
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR cmd, int flag ) { AllocConsole(); console = GetStdHandle(STD_OUTPUT_HANDLE);
WNDCLASSEXW wcex{NULL};
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = NULL; wcex.hCursor = NULL; wcex.hbrBackground = NULL; wcex.lpszMenuName = NULL; wcex.lpszClassName = L"ylh的窗口类"; wcex.hIconSm = NULL;
RegisterClassExW(&wcex); HWND hwnd = CreateWindowExW( wcex.style, wcex.lpszClassName, L"ylh牛逼", WS_OVERLAPPEDWINDOW, 100, 100, 500, 500, NULL, NULL, wcex.hInstance, NULL ); ShowWindow(hwnd, SW_SHOW); UpdateWindow(hwnd); MSG msg{}; while (GetMessage(&msg, NULL, NULL, NULL)) { TranslateMessage(&msg); DispatchMessage(&msg); } system("pause"); return 0; }
|