<-

hotkey resizer, rect win small app using C, tool utils

my rectwin app enhanced:

 

#include <windows.h>
#include <shellapi.h> // For Shell_NotifyIcon functions
#include <stdio.h>

// building the app:
// gcc h1.c -o h1.exe -luser32 -lcomctl32 -mwindows

#define ID_TRAY_APP_ICON 1001
#define WM_TRAYICON (WM_USER + 1)

// Hotkey IDs
#define HOTKEY_QUARTER_U 1
#define HOTKEY_QUARTER_I 2
#define HOTKEY_QUARTER_J 3
#define HOTKEY_K 4
#define HOTKEY_HALF_LEFT 5
#define HOTKEY_HALF_RIGHT 6
#define HOTKEY_HALF_UP 7
#define HOTKEY_HALF_DOWN 8
#define HOTKEY_FULL_SCREEN 9 // <-- New hotkey for full screen

HICON hIcon;
NOTIFYICONDATA nid;

// Function prototypes
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
void RegisterHotkeys(HWND hwnd);
void UnregisterHotkeys(HWND hwnd);
void ShowContextMenu(HWND hwnd, POINT pt);
void SetupTrayIcon(HWND hwnd);
void RemoveTrayIcon(HWND hwnd);
void ResizeActiveWindow(double rel_x, double rel_y, double rel_w, double rel_h);
void InitTrayIcon(HWND hwnd);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR, int nCmdShow)
{
    // Register window class
    WNDCLASS wc = {0};
    wc.lpfnWndProc = WndProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = "HotkeyResizerTray";

    if (!RegisterClass(&wc))
    {
        MessageBox(NULL, "Failed to register window class", "Error", MB_OK);
        return 1;
    }

    // Create invisible window
    HWND hwnd = CreateWindow(wc.lpszClassName, "Hotkey Resizer Tray", 0,
                             CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
                             NULL, NULL, hInstance, NULL);
    if (!hwnd)
    {
        MessageBox(NULL, "Failed to create window", "Error", MB_OK);
        return 1;
    }

    // Load an icon for the tray
    hIcon = LoadIcon(NULL, IDI_APPLICATION);

    // Setup system tray icon
    SetupTrayIcon(hwnd);

    // Register hotkeys
    RegisterHotkeys(hwnd);

    // Message loop
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    // Cleanup
    UnregisterHotkeys(hwnd);
    RemoveTrayIcon(hwnd);
    DestroyIcon(hIcon);
    return 0;
}

// Register hotkeys
void RegisterHotkeys(HWND hwnd)
{
    RegisterHotKey(hwnd, HOTKEY_QUARTER_U, MOD_CONTROL, 0x55); // Ctrl+U
    RegisterHotKey(hwnd, HOTKEY_QUARTER_I, MOD_CONTROL, 0x49); // Ctrl+I
    RegisterHotKey(hwnd, HOTKEY_QUARTER_J, MOD_CONTROL, 0x4A); // Ctrl+J
    RegisterHotKey(hwnd, HOTKEY_K, MOD_CONTROL, 0x4B);         // Ctrl+K

    RegisterHotKey(hwnd, HOTKEY_HALF_LEFT, MOD_CONTROL, VK_LEFT);
    RegisterHotKey(hwnd, HOTKEY_HALF_RIGHT, MOD_CONTROL, VK_RIGHT);
    RegisterHotKey(hwnd, HOTKEY_HALF_UP, MOD_CONTROL, VK_UP);
    RegisterHotKey(hwnd, HOTKEY_HALF_DOWN, MOD_CONTROL, VK_DOWN);

    // Add Ctrl+Enter for full screen resize
    RegisterHotKey(hwnd, HOTKEY_FULL_SCREEN, MOD_CONTROL, VK_RETURN);
}

// Unregister hotkeys
void UnregisterHotkeys(HWND hwnd)
{
    for (int id = HOTKEY_QUARTER_U; id <= HOTKEY_FULL_SCREEN; id++)
    {
        UnregisterHotKey(hwnd, id);
    }
}

// Setup system tray icon
void SetupTrayIcon(HWND hwnd)
{
    memset(&nid, 0, sizeof(nid));
    nid.cbSize = sizeof(nid);
    nid.hWnd = hwnd;
    nid.uID = ID_TRAY_APP_ICON;
    nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
    nid.uCallbackMessage = WM_TRAYICON;
    nid.hIcon = hIcon;
    strcpy(nid.szTip, "Hotkey Resizer (Right-click for options)");
    Shell_NotifyIcon(NIM_ADD, &nid);
}

// Remove tray icon
void RemoveTrayIcon(HWND hwnd)
{
    Shell_NotifyIcon(NIM_DELETE, &nid);
}

// Show context menu
void ShowContextMenu(HWND hwnd, POINT pt)
{
    HMENU hMenu = CreatePopupMenu();
    if (hMenu)
    {
        InsertMenu(hMenu, -1, MF_BYPOSITION, 1, "Exit");
        SetForegroundWindow(hwnd);
        TrackPopupMenu(hMenu, TPM_BOTTOMALIGN | TPM_LEFTALIGN, pt.x, pt.y, 0, hwnd, NULL);
        DestroyMenu(hMenu);
    }
}

// Resize window based on relative position and size
void ResizeActiveWindow(double rel_x, double rel_y, double rel_w, double rel_h)
{
    HWND hwndActive = GetForegroundWindow();
    if (!hwndActive)
        return;

    RECT workArea;
    SystemParametersInfo(SPI_GETWORKAREA, 0, &workArea, 0);
    int screenWidth = workArea.right - workArea.left;
    int screenHeight = workArea.bottom - workArea.top;

    int newX = workArea.left + (int)(rel_x * screenWidth);
    int newY = workArea.top + (int)(rel_y * screenHeight);
    int newW = (int)(rel_w * screenWidth);
    int newH = (int)(rel_h * screenHeight);

    SetWindowPos(hwndActive, HWND_TOP, newX, newY, newW, newH, SWP_NOZORDER);
}

// Resize to full screen
void ResizeToFullScreen()
{
    HWND hwndActive = GetForegroundWindow();
    if (!hwndActive)
        return;

    RECT workArea;
    SystemParametersInfo(SPI_GETWORKAREA, 0, &workArea, 0);

    int width = workArea.right - workArea.left;
    int height = workArea.bottom - workArea.top;

    SetWindowPos(hwndActive, HWND_TOP, workArea.left, workArea.top, width, height, SWP_NOZORDER);
}

// Main window procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
    case WM_TRAYICON:
        if (lParam == WM_RBUTTONDOWN)
        {
            POINT pt;
            GetCursorPos(&pt);
            ShowContextMenu(hwnd, pt);
        }
        break;
    case WM_COMMAND:
        if (LOWORD(wParam) == 1)
        { // Exit menu item
            PostQuitMessage(0);
        }
        break;
    case WM_HOTKEY:
        switch (wParam)
        {
        case HOTKEY_QUARTER_U:
            ResizeActiveWindow(0, 0, 0.5, 0.5);
            break;
        case HOTKEY_QUARTER_I:
            ResizeActiveWindow(0.5, 0, 0.5, 0.5);
            break;
        case HOTKEY_QUARTER_J:
            ResizeActiveWindow(0, 0.5, 0.5, 0.5);
            break;
        case HOTKEY_K:
            ResizeActiveWindow(0.5, 0.5, 0.5, 0.5);
            break;
        case HOTKEY_HALF_LEFT:
            ResizeActiveWindow(0, 0, 0.5, 1);
            break;
        case HOTKEY_HALF_RIGHT:
            ResizeActiveWindow(0.5, 0, 0.5, 1);
            break;
        case HOTKEY_HALF_UP:
            ResizeActiveWindow(0, 0, 1, 0.5);
            break;
        case HOTKEY_HALF_DOWN:
            ResizeActiveWindow(0, 0.5, 1, 0.5);
            break;
        case HOTKEY_FULL_SCREEN:
            ResizeToFullScreen();
            break;
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}
View Code

 

revision monitor detection:

 

#include <windows.h>
#include <shellapi.h> // For Shell_NotifyIcon functions
#include <stdio.h>

// building the app:
// gcc h1.c -o h1.exe -luser32 -lcomctl32 -mwindows

#define ID_TRAY_APP_ICON 1001
#define WM_TRAYICON (WM_USER + 1)

// Hotkey IDs
#define HOTKEY_QUARTER_U 1
#define HOTKEY_QUARTER_I 2
#define HOTKEY_QUARTER_J 3
#define HOTKEY_K 4
#define HOTKEY_HALF_LEFT 5
#define HOTKEY_HALF_RIGHT 6
#define HOTKEY_HALF_UP 7
#define HOTKEY_HALF_DOWN 8
#define HOTKEY_FULL_SCREEN 9 // <-- New hotkey for full screen

HICON hIcon;
NOTIFYICONDATA nid;

// Function prototypes
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
void RegisterHotkeys(HWND hwnd);
void UnregisterHotkeys(HWND hwnd);
void ShowContextMenu(HWND hwnd, POINT pt);
void SetupTrayIcon(HWND hwnd);
void RemoveTrayIcon(HWND hwnd);
void ResizeActiveWindow(double rel_x, double rel_y, double rel_w, double rel_h);
void InitTrayIcon(HWND hwnd);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR, int nCmdShow)
{
    // Register window class
    WNDCLASS wc = {0};
    wc.lpfnWndProc = WndProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = "HotkeyResizerTray";

    if (!RegisterClass(&wc))
    {
        MessageBox(NULL, "Failed to register window class", "Error", MB_OK);
        return 1;
    }

    // Create invisible window
    HWND hwnd = CreateWindow(wc.lpszClassName, "Hotkey Resizer Tray", 0,
                             CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
                             NULL, NULL, hInstance, NULL);
    if (!hwnd)
    {
        MessageBox(NULL, "Failed to create window", "Error", MB_OK);
        return 1;
    }

    // Load an icon for the tray
    hIcon = LoadIcon(NULL, IDI_APPLICATION);

    // Setup system tray icon
    SetupTrayIcon(hwnd);

    // Register hotkeys
    RegisterHotkeys(hwnd);

    // Message loop
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    // Cleanup
    UnregisterHotkeys(hwnd);
    RemoveTrayIcon(hwnd);
    DestroyIcon(hIcon);
    return 0;
}

// Register hotkeys
void RegisterHotkeys(HWND hwnd)
{
    RegisterHotKey(hwnd, HOTKEY_QUARTER_U, MOD_CONTROL, 0x55); // Ctrl+U
    RegisterHotKey(hwnd, HOTKEY_QUARTER_I, MOD_CONTROL, 0x49); // Ctrl+I
    RegisterHotKey(hwnd, HOTKEY_QUARTER_J, MOD_CONTROL, 0x4A); // Ctrl+J
    RegisterHotKey(hwnd, HOTKEY_K, MOD_CONTROL, 0x4B);         // Ctrl+K

    RegisterHotKey(hwnd, HOTKEY_HALF_LEFT, MOD_CONTROL, VK_LEFT);
    RegisterHotKey(hwnd, HOTKEY_HALF_RIGHT, MOD_CONTROL, VK_RIGHT);
    RegisterHotKey(hwnd, HOTKEY_HALF_UP, MOD_CONTROL, VK_UP);
    RegisterHotKey(hwnd, HOTKEY_HALF_DOWN, MOD_CONTROL, VK_DOWN);

    // Add Ctrl+Enter for full screen resize
    RegisterHotKey(hwnd, HOTKEY_FULL_SCREEN, MOD_CONTROL, VK_RETURN);
}

// Unregister hotkeys
void UnregisterHotkeys(HWND hwnd)
{
    for (int id = HOTKEY_QUARTER_U; id <= HOTKEY_FULL_SCREEN; id++)
    {
        UnregisterHotKey(hwnd, id);
    }
}

// Setup system tray icon
void SetupTrayIcon(HWND hwnd)
{
    memset(&nid, 0, sizeof(nid));
    nid.cbSize = sizeof(nid);
    nid.hWnd = hwnd;
    nid.uID = ID_TRAY_APP_ICON;
    nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
    nid.uCallbackMessage = WM_TRAYICON;
    nid.hIcon = hIcon;
    strcpy(nid.szTip, "Hotkey Resizer (Right-click for options)");
    Shell_NotifyIcon(NIM_ADD, &nid);
}

// Remove tray icon
void RemoveTrayIcon(HWND hwnd)
{
    Shell_NotifyIcon(NIM_DELETE, &nid);
}

// Show context menu
void ShowContextMenu(HWND hwnd, POINT pt)
{
    HMENU hMenu = CreatePopupMenu();
    if (hMenu)
    {
        InsertMenu(hMenu, -1, MF_BYPOSITION, 1, "Exit");
        SetForegroundWindow(hwnd);
        TrackPopupMenu(hMenu, TPM_BOTTOMALIGN | TPM_LEFTALIGN, pt.x, pt.y, 0, hwnd, NULL);
        DestroyMenu(hMenu);
    }
}

// Function to get the monitor info for the active window
RECT GetActiveWindowMonitorWorkArea()
{
    HWND hwnd = GetForegroundWindow();
    if (!hwnd)
    {
        // Fallback to primary monitor if no window
        RECT r;
        SystemParametersInfo(SPI_GETWORKAREA, 0, &r, 0);
        return r;
    }
    HMONITOR hMonitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONULL);
    if (!hMonitor)
    {
        // No monitor found, fallback
        RECT r;
        SystemParametersInfo(SPI_GETWORKAREA, 0, &r, 0);
        return r;
    }

    MONITORINFO mi;
    mi.cbSize = sizeof(mi);
    if (GetMonitorInfo(hMonitor, &mi))
    {
        return mi.rcWork;
    }
    else
    {
        RECT r;
        SystemParametersInfo(SPI_GETWORKAREA, 0, &r, 0);
        return r;
    }
}

// Resize window based on relative position and size
void ResizeActiveWindow(double rel_x, double rel_y, double rel_w, double rel_h)
{
    RECT workArea = GetActiveWindowMonitorWorkArea();
    int width = workArea.right - workArea.left;
    int height = workArea.bottom - workArea.top;

    int newX = workArea.left + (int)(rel_x * width);
    int newY = workArea.top + (int)(rel_y * height);
    int newW = (int)(rel_w * width);
    int newH = (int)(rel_h * height);
    HWND hwndActive = GetForegroundWindow();
    if (!hwndActive)
        return;

    // RECT workArea;
    // SystemParametersInfo(SPI_GETWORKAREA, 0, &workArea, 0);
    // int screenWidth = workArea.right - workArea.left;
    // int screenHeight = workArea.bottom - workArea.top;

    // int newX = workArea.left + (int)(rel_x * screenWidth);
    // int newY = workArea.top + (int)(rel_y * screenHeight);
    // int newW = (int)(rel_w * screenWidth);
    // int newH = (int)(rel_h * screenHeight);

    SetWindowPos(hwndActive, HWND_TOP, newX, newY, newW, newH, SWP_NOZORDER);
}

// Resize to full screen
void ResizeToFullScreen()
{

    RECT workArea = GetActiveWindowMonitorWorkArea();
    int width = workArea.right - workArea.left;
    int height = workArea.bottom - workArea.top;

    HWND hwndActive = GetForegroundWindow();
    if (!hwndActive)
        return;

    // RECT workArea;
    // SystemParametersInfo(SPI_GETWORKAREA, 0, &workArea, 0);

    // int width = workArea.right - workArea.left;
    // int height = workArea.bottom - workArea.top;

    SetWindowPos(hwndActive, HWND_TOP, workArea.left, workArea.top, width, height, SWP_NOZORDER);
}

// Main window procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
    case WM_TRAYICON:
        if (lParam == WM_RBUTTONDOWN)
        {
            POINT pt;
            GetCursorPos(&pt);
            ShowContextMenu(hwnd, pt);
        }
        break;
    case WM_COMMAND:
        if (LOWORD(wParam) == 1)
        { // Exit menu item
            PostQuitMessage(0);
        }
        break;
    case WM_HOTKEY:
        switch (wParam)
        {
        case HOTKEY_QUARTER_U:
            ResizeActiveWindow(0, 0, 0.5, 0.5);
            break;
        case HOTKEY_QUARTER_I:
            ResizeActiveWindow(0.5, 0, 0.5, 0.5);
            break;
        case HOTKEY_QUARTER_J:
            ResizeActiveWindow(0, 0.5, 0.5, 0.5);
            break;
        case HOTKEY_K:
            ResizeActiveWindow(0.5, 0.5, 0.5, 0.5);
            break;
        case HOTKEY_HALF_LEFT:
            ResizeActiveWindow(0, 0, 0.5, 1);
            break;
        case HOTKEY_HALF_RIGHT:
            ResizeActiveWindow(0.5, 0, 0.5, 1);
            break;
        case HOTKEY_HALF_UP:
            ResizeActiveWindow(0, 0, 1, 0.5);
            break;
        case HOTKEY_HALF_DOWN:
            ResizeActiveWindow(0, 0.5, 1, 0.5);
            break;
        case HOTKEY_FULL_SCREEN:
            ResizeToFullScreen();
            break;
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}
View Code

 

posted @ 2026-04-04 12:39  calochCN  阅读(14)  评论(0)    收藏  举报