如何调整窗口边框大小

时间:2020-09-28 04:24:49

标签: c++ winapi

我有一个窗口,我希望它的行为类似于切换按钮。单击后将添加4px边框,单击后将使边框消失。我想出了如何使用BS_PUSHLIKEButton_SetCheck()使窗口像切换按钮一样工作,但似乎无法弄清楚如何调整此窗口的边框大小。

感谢所有花时间帮助的人

1 个答案:

答案 0 :(得分:2)

也许您可以使用MoveWindow来调整窗口的大小,然后像这样自己绘制边框,

enter image description here

首先绘制一个无边界窗口:

HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      100, 100, 800, 600, nullptr, nullptr, hInstance, nullptr);
LONG lStyle = GetWindowLong(hWnd, GWL_STYLE);
lStyle &= ~(WS_CAPTION | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SYSMENU);
SetWindowLong(hWnd, GWL_STYLE, lStyle);

然后处理WM_LBUTTONDOWN消息中的窗口边框:

int num = 0;
case WM_LBUTTONDOWN:
    {     
        RECT rcWind;
        HDC dc = GetDC(hWnd);
        GetWindowRect(hWnd, &rcWind);     
        if (num >= 0)
        {        
            num--;
            RECT rcClient;
            MoveWindow(hWnd, rcWind.left - 4, rcWind.top - 4, 8 + rcWind.right - rcWind.left, 8 + rcWind.bottom - rcWind.top, TRUE);
            GetClientRect(hWnd, &rcClient);
            HPEN hPen = CreatePen(PS_SOLID, 4, RGB(255, 128, 1));
            HGDIOBJ hOldPen = SelectObject(dc, hPen);
            Rectangle(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
            DeleteObject(hPen);
        }   
        else if (num < 0)
        {
            MoveWindow(hWnd, rcWind.left + 4, rcWind.top + 4, rcWind.right - rcWind.left - 8, rcWind.bottom - rcWind.top - 8, TRUE);
            num++;
        }
    }
    break;