惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
P
Proofpoint News Feed
宝玉的分享
宝玉的分享
人人都是产品经理
人人都是产品经理
博客园_首页
爱范儿
爱范儿
博客园 - 叶小钗
aimingoo的专栏
aimingoo的专栏
S
SegmentFault 最新的问题
MyScale Blog
MyScale Blog
阮一峰的网络日志
阮一峰的网络日志
IT之家
IT之家
Microsoft Security Blog
Microsoft Security Blog
Blog — PlanetScale
Blog — PlanetScale
博客园 - 【当耐特】
Y
Y Combinator Blog
量子位
博客园 - 三生石上(FineUI控件)
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
The Blog of Author Tim Ferriss
月光博客
月光博客
有赞技术团队
有赞技术团队
Apple Machine Learning Research
Apple Machine Learning Research
A
About on SuperTechFans

博客园 - calochCN

using webpack5 ubuntu 如何将任何app run为服务 这里想配置一下mysql主从,并归档,以备使用 Using ES6 Module In Browser. reprint, Use of logrotate go copy object deep深拷贝 go get net/http connections count, using middleware 手动数据库分库分片策略 记录一下ubuntu 挂载一下raid1 硬盘的过程 好久不弄css导航条,这里再布一下 sapui5 说说备案问题,说说发现的腾讯云和gitee这些国内服务商,使用过程中发现的一些猫腻 我最近觉得自己的收藏夹很乱,因为没有事情做,就想闲着给自己做个网址导航的软件【一】 学日语看到一个笑话 抽几分钟,写个文档系统的前端页面,先排版一下. 文字竖排,从上到下排列,仿古文的写法 从前做前端的时候, 联通的好多html5活动, 页面布局如何整齐,回顾一个常用的 我依然没有用quest2串流过一次... 这次还是决定好好用hugo写一下模板系统
hotkey resizer, rect win small app using C, tool utils
calochCN · 2026-04-04 · via 博客园 - calochCN

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