不断改进的提示语:
write a program in c and compile by gcc, create a system tray icon, when the icon is right clicked, pop up an exit menu to exit the program, listen in backgroup, when key combination Ctrl+Left Arrow is pressed, get current active window in windows, then set the window size to half the screen resolution and locate to left half screen, when Ctrl+Right Arrow is pressed, get the current active window in windows and fix into the right half of the screen, when ctrl+up arrow is pressed, fix the current active window to upper half of the screen, when ctrl+down arrow is pressed, fix the current active window into the bottom half of the screen, when ctrl+alt+enter is pressed, if the current active window is in max size, restore its size and location to normal, or else, set it to max size if its size and location is normal
结果:
#include <stdio.h> #include <windows.h> #include <shellapi.h> #define KEY_CTRL_LEFT_ARROW 0x25 #define KEY_CTRL_RIGHT_ARROW 0x27 #define KEY_CTRL_UP_ARROW 0x26 #define KEY_CTRL_DOWN_ARROW 0x28 #define KEY_CTRL_ALT_ENTER 0x0D #define TRAY_ICON_ID 1001 #define WM_TRAY_ICON (WM_USER + 1) HWND hwnd; HMENU hMenu; void resizeAndMoveWindow(HWND hwnd, int x, int y, int width, int height) { SetWindowPos(hwnd, NULL, x, y, width, height, SWP_NOZORDER); } void handleKeyPress() { if (GetAsyncKeyState(VK_CONTROL) & 0x8000) { if (GetAsyncKeyState(VK_LEFT) & 0x8000) { HWND hwnd = GetForegroundWindow(); int screenWidth = GetSystemMetrics(SM_CXSCREEN); int screenHeight = GetSystemMetrics(SM_CYSCREEN); resizeAndMoveWindow(hwnd, 0, 0, screenWidth / 2, screenHeight); } else if (GetAsyncKeyState(VK_RIGHT) & 0x8000) { HWND hwnd = GetForegroundWindow(); int screenWidth = GetSystemMetrics(SM_CXSCREEN); int screenHeight = GetSystemMetrics(SM_CYSCREEN); resizeAndMoveWindow(hwnd, screenWidth / 2, 0, screenWidth / 2, screenHeight); } else if (GetAsyncKeyState(VK_UP) & 0x8000) { HWND hwnd = GetForegroundWindow(); int screenWidth = GetSystemMetrics(SM_CXSCREEN); int screenHeight = GetSystemMetrics(SM_CYSCREEN); resizeAndMoveWindow(hwnd, 0, 0, screenWidth, screenHeight / 2); } else if (GetAsyncKeyState(VK_DOWN) & 0x8000) { HWND hwnd = GetForegroundWindow(); int screenWidth = GetSystemMetrics(SM_CXSCREEN); int screenHeight = GetSystemMetrics(SM_CYSCREEN); resizeAndMoveWindow(hwnd, 0, screenHeight / 2, screenWidth, screenHeight / 2); } else if (GetAsyncKeyState(VK_MENU) & 0x8000 && GetAsyncKeyState(VK_RETURN) & 0x8000) { HWND hwnd = GetForegroundWindow(); DWORD style = GetWindowLong(hwnd, GWL_STYLE); WINDOWPLACEMENT placement; placement.length = sizeof(WINDOWPLACEMENT); GetWindowPlacement(hwnd, &placement); if (style & WS_MAXIMIZE || placement.showCmd == SW_MAXIMIZE) { ShowWindow(hwnd, SW_RESTORE); } else { ShowWindow(hwnd, SW_MAXIMIZE); } } } } LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { POINT cursorPos; switch (uMsg) { case WM_CREATE: hMenu = CreatePopupMenu(); AppendMenu(hMenu, MF_STRING, 1, "Exit"); break; case WM_TRAY_ICON: switch (lParam) { case WM_RBUTTONDOWN: GetCursorPos(&cursorPos); SetForegroundWindow(hwnd); TrackPopupMenu(hMenu, TPM_RIGHTBUTTON, cursorPos.x, cursorPos.y, 0, hwnd, NULL); break; } break; case WM_COMMAND: switch (LOWORD(wParam)) { case 1: DestroyWindow(hwnd); break; } break; case WM_CLOSE: DestroyWindow(hwnd); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, uMsg, wParam, lParam); } return 0; } LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) { if (nCode == HC_ACTION && (lParam & 0x80000000) == 0) { KBDLLHOOKSTRUCT *kbStruct = (KBDLLHOOKSTRUCT *)lParam; if (kbStruct->vkCode == KEY_CTRL_LEFT_ARROW || kbStruct->vkCode == KEY_CTRL_RIGHT_ARROW || kbStruct->vkCode == KEY_CTRL_UP_ARROW || kbStruct->vkCode == KEY_CTRL_DOWN_ARROW || kbStruct->vkCode == KEY_CTRL_ALT_ENTER) { handleKeyPress(); } } return CallNextHookEx(NULL, nCode, wParam, lParam); } int main() { HINSTANCE hInstance = GetModuleHandle(NULL); // Create the window class WNDCLASS wc = {0}; wc.lpfnWndProc = WindowProc; wc.hInstance = hInstance; wc.lpszClassName = "MyWindowClass"; RegisterClass(&wc); // Create the window hwnd = CreateWindowEx(0, "MyWindowClass", "Window Resizer", 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, hInstance, NULL); // Create the system tray icon NOTIFYICONDATA nid = {0}; nid.cbSize = sizeof(NOTIFYICONDATA); nid.hWnd = hwnd; nid.uID = TRAY_ICON_ID; nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; nid.uCallbackMessage = WM_TRAY_ICON; nid.hIcon = LoadIcon(NULL, IDI_APPLICATION); strcpy(nid.szTip, "Window Resizer"); Shell_NotifyIcon(NIM_ADD, &nid); // Set the keyboard hook HHOOK hook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, hInstance, 0); if (hook == NULL) { printf("Failed to set keyboard hook.\n"); return 1; } // Message loop MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } // Clean up UnhookWindowsHookEx(hook); Shell_NotifyIcon(NIM_DELETE, &nid); return 0; }
程序已经可以正常运行,
注意, 编译的时候要加上-mwindows这个选项,不然会多出来一个黑空控制台窗口: gcc window_resizer.c -o window_resizer -mwindows,
然后 直接执行程序就可以看到托盘图标可以退出,正常使用了, 太开心了。
标签:编码,CTRL,ai,hwnd,差不多,nid,int,window,KEY From: https://www.cnblogs.com/hualiu0/p/17750494.html