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
| #include<Windows.h> #include<iostream> #include<CommCtrl.h> #include"resource.h" LRESULT CALLBACK WindowProc( _In_ HWND hwnd, _In_ UINT uMsg, _In_ WPARAM wParam, _In_ LPARAM lParam ) { static HINSTANCE hInstance = GetModuleHandleW(NULL); switch (uMsg) { case WM_CREATE: CreateWindowW(WC_BUTTON, L"移动窗口", WS_CHILD | WS_VISIBLE, 10, 10, 120, 40, hwnd, (HMENU)0x100, hInstance, 0); CreateWindowW(WC_BUTTON, L"获取文本框内容", WS_CHILD | WS_VISIBLE, 10, 60, 120, 40, hwnd, (HMENU)0x101, hInstance, 0); CreateWindowW(WC_BUTTON, L"设置文本框内容", WS_CHILD | WS_VISIBLE, 10, 110, 120, 40, hwnd, (HMENU)0x102, hInstance, 0); CreateWindowW(WC_BUTTON, L"设置父窗口", WS_CHILD | WS_VISIBLE, 10, 160, 120, 40, hwnd, (HMENU)0x103, hInstance, 0); CreateWindowW(WC_EDIT, L"文本框内容", WS_CHILD | WS_BORDER | WS_VISIBLE, 10, 320, 320, 80, hwnd, (HMENU)0x104, hInstance, 0); CreateWindowW(WC_BUTTON, L"设置ICON", WS_CHILD | WS_VISIBLE, 10, 220, 120, 40, hwnd, (HMENU)0x105, hInstance, 0); CreateWindowW(WC_BUTTON, L"设置菜单", WS_CHILD | WS_VISIBLE, 10, 420, 120, 40, hwnd, (HMENU)0x106, hInstance, 0); break; case WM_CLOSE: MessageBoxW(hwnd, L"窗口关闭了", L"提示", MB_OK); DestroyWindow(hwnd); PostQuitMessage(0); break; case WM_RBUTTONDOWN: {
POINT point{ 0 };
GetCursorPos(&point); HMENU hmenu = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDR_MENU1)); HMENU hSubMenu = GetSubMenu(hmenu, 0); TrackPopupMenu(hSubMenu,TPM_LEFTALIGN, point.x, point.y,0,hwnd,NULL); break; } case WM_COMMAND: { WORD ControlId = LOWORD(wParam); switch (ControlId) { case 0x100: { RECT rect{ 0 }; GetClientRect(hwnd, &rect); int x = rand() % (rect.right - 120); int y = rand() % (rect.bottom - 40); MoveWindow((HWND)lParam, x, y, 120, 40, TRUE); break; } case 0x101: { WCHAR buff[100]{ 0 }; HWND hedit = GetDlgItem(hwnd, 0x104); GetWindowTextW(hedit, buff, 100); MessageBoxW(hwnd, buff, L"提示", MB_OK); break; } case 0x102: {
SetDlgItemTextW(hwnd, 0x104, L"SetDlgItemTextW"); break; } case 0x103: { HWND hnote = FindWindowW(NULL, L"无标题 - 记事本"); SetParent((HWND)lParam, hnote); break; } case 0x106: { HMENU hmenu = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDR_MENU1)); SetMenu(hwnd, hmenu); break; } case ID_Menu: { MessageBoxW(hwnd, L"子菜单1", L"子菜单1被点击了", MB_OK); break; } } break;
} default: break; } return DefWindowProcW(hwnd, uMsg, wParam, lParam); } int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPreInstance, LPSTR lpCmdeLine, int nCmdShow ) { WNDCLASSW myClass = { 0 }; myClass.lpszClassName = L"51hook"; myClass.lpfnWndProc = WindowProc; myClass.hbrBackground = CreateSolidBrush(RGB(255, 255, 255)); RegisterClassW(&myClass);
HWND hwindow = CreateWindowW( myClass.lpszClassName, L"51hook", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, 0 ); ShowWindow(hwindow, SW_SHOWNORMAL);
MSG msg = { 0 }; while (GetMessageW(&msg, 0, 0, 0)) { DispatchMessageW(&msg); }
return 0; }
|