今天说个,把编程作为一种兴趣,以后没事儿就不写代码这个事儿,修正ai代码添加功能,rectwin
走过漫长数载岁月,人生风景迭代更换,在如今大消条的时间,其实只是偶尔试一下写代码弄个有趣的东西,别的真的没什么意思了,如果还能找来活就去干吧,别的时候还是不弄这些了。不过看起来还是蛮好玩的。
上次写那个程序,还是有些不如意,就是win下设置窗口的,结果ai现在不准确了,要自己有点功底才能修改,所以就阅读代码,然后修正。
看起来windows下用c,主要在windows.h, shellapi.h, 记得编译要是 gcc -o r.exe main.c -mwindows, 后面选项不能少
这里首先要明确,因为我要按键组合,在c语言中,就先查到各个不同键的键值,会对应内存地址:
这个值在winuser.h里面都会查到
 
0x1 鼠标左键 0x2 鼠标右键 0x3 CANCEL 键 0x4 鼠标中键 0x8 BACKSPACE 键(退格键) 0x9 TAB 键 0xC CLEAR 键 0xD ENTER 键 0x10 SHIFT 键 0x11 CTRL 键 0x12 MENU 键 0x13 PAUSE 键 0x14 CAPS LOCK 键 0x1B ESC 键 0x20 SPACEBAR 键 0x21 PAGE UP 键 0x22 PAGE DOWN 键 0x23 END 键 0x24 HOME 键 0x25 LEFT ARROW 键 0x26 UP ARROW 键 0x27 RIGHT ARROW 键 0x28 DOWN ARROW 键 0x29 SELECT 键 0x2A PRINT SCREEN 键 0x2B EXECUTE 键 0x2C SNAPSHOT 键 0x2D INSERT 键 0x2E DELETE 键 0x2F HELP 键 0x90 NUM LOCK 键 A 至 Z 键与 A – Z 字母的 ASCII 码相同: 65 A 键 66 B 键 67 C 键 68 D 键 69 E 键 70 F 键 71 G 键 72 H 键 73 I 键 74 J 键 75 K 键 76 L 键 77 M 键 78 N 键 79 O 键 80 P 键 81 Q 键 82 R 键 83 S 键 84 T 键 85 U 键 86 V 键 87 W 键 88 X 键 89 Y 键 90 Z 键 0 至 9 键与数字 0 – 9 的 ASCII 码相同: 48 0 键 49 1 键 50 2 键 51 3 键 52 4 键 53 5 键 54 6 键 55 7 键 56 8 键 57 9 键 下列常数代表数字键盘上的键: 0x60 0 键 0x61 1 键 0x62 2 键 0x63 3 键 0x64 4 键 0x65 5 键 0x66 6 键 0x67 7 键 0x68 8 键 0x69 9 键 0x6A MULTIPLICATION SIGN (*) 键 0x6B PLUS SIGN (+) 键 0x6C ENTER 键 0x6D MINUS SIGN (–) 键 0x6E DECIMAL POINT (.) 键 0x6F DIVISION SIGN (/) 键 下列常数代表功能键: 0x70 F1 键 0x71 F2 键 0x72 F3 键 0x73 F4 键 0x74 F5 键 0x75 F6 键 0x76 F7 键 0x77 F8 键 0x78 F9 键 0x79 F10 键 0x7A F11 键 0x7B F12 键 0x7C F13 键 0x7D F14 键 0x7E F15 键 0x7F F16 键
目前的代码要修正的是taskbar占用的空间,还有就是不用最大化,因为上次发现最大化后再按其他组合不会恢复大小。
最大化是一种窗口的placement状态,最大化后就不能使用定义的其他快捷键了,必须先恢复窗口到正常状态才能使用,所以更改了逻辑,依然在ctrl alt enter里面使用设置窗口大小,不再有还有的逻辑,最终效果如下,还是可以使用的
taskbar在完全在上部的情况已经可用, 哈哈:)就先这样吧
 
#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() { HWND taskbarHandle = FindWindow("Shell_TrayWnd", NULL); RECT taskbarRect; int taskbarWidth = taskbarRect.right - taskbarRect.left; int taskbarHeight = taskbarRect.bottom - taskbarRect.top; printf("Taskbar Location: (%d, %d)\n", taskbarRect.left, taskbarRect.top); printf("Taskbar Size: %d x %d\n", taskbarWidth, taskbarHeight); if (taskbarHandle && GetWindowRect(taskbarHandle, &taskbarRect)) { if (GetAsyncKeyState(VK_CONTROL) & 0x8000) { if (GetAsyncKeyState(VK_LEFT) & 0x8000) { HWND hwnd = GetForegroundWindow(); HMONITOR hMonitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST); MONITORINFO monitorInfo; monitorInfo.cbSize = sizeof(MONITORINFO); GetMonitorInfo(hMonitor, &monitorInfo); int screenWidth = monitorInfo.rcMonitor.right - monitorInfo.rcMonitor.left; int screenHeight = monitorInfo.rcMonitor.bottom - monitorInfo.rcMonitor.top; resizeAndMoveWindow(hwnd, monitorInfo.rcMonitor.left, monitorInfo.rcMonitor.top + taskbarHeight, screenWidth / 2+13, screenHeight - taskbarHeight); } else if (GetAsyncKeyState(VK_RIGHT) & 0x8000) { HWND hwnd = GetForegroundWindow(); HMONITOR hMonitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST); MONITORINFO monitorInfo; monitorInfo.cbSize = sizeof(MONITORINFO); GetMonitorInfo(hMonitor, &monitorInfo); int screenWidth = monitorInfo.rcMonitor.right - monitorInfo.rcMonitor.left; int screenHeight = monitorInfo.rcMonitor.bottom - monitorInfo.rcMonitor.top; resizeAndMoveWindow(hwnd, monitorInfo.rcMonitor.left + screenWidth / 2, monitorInfo.rcMonitor.top + taskbarHeight, screenWidth / 2, screenHeight - taskbarHeight); } else if (GetAsyncKeyState(VK_UP) & 0x8000) { HWND hwnd = GetForegroundWindow(); HMONITOR hMonitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST); MONITORINFO monitorInfo; monitorInfo.cbSize = sizeof(MONITORINFO); GetMonitorInfo(hMonitor, &monitorInfo); int screenWidth = monitorInfo.rcMonitor.right - monitorInfo.rcMonitor.left; int screenHeight = monitorInfo.rcMonitor.bottom - monitorInfo.rcMonitor.top; resizeAndMoveWindow(hwnd, monitorInfo.rcMonitor.left, monitorInfo.rcMonitor.top + taskbarHeight, screenWidth, screenHeight / 2 - taskbarHeight+13); } else if (GetAsyncKeyState(VK_DOWN) & 0x8000) { HWND hwnd = GetForegroundWindow(); HMONITOR hMonitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST); MONITORINFO monitorInfo; monitorInfo.cbSize = sizeof(MONITORINFO); GetMonitorInfo(hMonitor, &monitorInfo); int screenWidth = monitorInfo.rcMonitor.right - monitorInfo.rcMonitor.left; int screenHeight = monitorInfo.rcMonitor.bottom - monitorInfo.rcMonitor.top; resizeAndMoveWindow(hwnd, monitorInfo.rcMonitor.left, monitorInfo.rcMonitor.top + screenHeight / 2, screenWidth, screenHeight / 2); } else if (GetAsyncKeyState(VK_MENU) & 0x8000 && GetAsyncKeyState(VK_RETURN) & 0x8000) { HWND hwnd = GetForegroundWindow(); HMONITOR hMonitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST); MONITORINFO monitorInfo; monitorInfo.cbSize = sizeof(MONITORINFO); GetMonitorInfo(hMonitor, &monitorInfo); int screenWidth = monitorInfo.rcMonitor.right - monitorInfo.rcMonitor.left; int screenHeight = monitorInfo.rcMonitor.bottom - monitorInfo.rcMonitor.top; resizeAndMoveWindow(hwnd, monitorInfo.rcMonitor.left, monitorInfo.rcMonitor.top + taskbarHeight, screenWidth, screenHeight - taskbarHeight); } } } else { printf("Taskbar not found.\n"); } } 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; }
很奇怪的, 加了13px的留白,也不知道数值,先这样让它看起来好看点
然后说下扩展吧,
taskbar有四个位置,我揣测,起始点 (0,0)上时, 窗口起始点为, 0,taskbar_height, 大小为screen_width, screen_height-taskbar_height, 此时判定为 taskbar: width为screen_width, height 为自身height
其他点依次类推即可。。。
 
                    
                 
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号