【问题标题】:Resizing window with Win32 API使用 Win32 API 调整窗口大小
【发布时间】:2016-11-04 00:06:54
【问题描述】:

我正在使用 Win32 API 编写一个程序,只是为了掌握它并遇到了这个烦人的问题。在调整窗口大小一段时间后最小化并再次打开它后它是可见的但不可点击,点击它只会激活它下方窗口上的内容。唯一的解决方案是退出程序。

起初我以为是因为我定制了调整窗口大小的功能(我使用自定义 gui 并且不想要默认的 windows sizebox 边框),但后来我重新激活了默认的 sizebox,问题仍然存在。很难说,但在调整大小大致相同的时间/刻度后,问题似乎一直出现。

我之前遇到过类似的问题,窗口会最小化,然后再次打开时完全变白,由于某种原因,这是由调整大小函数中的 for 循环引起的。

我已在代码中排除了尽可能多的潜在问题,但仍未找到解决方案。以下是处理窗口绘制方式的所有代码所在的源文件(使用默认大小框)。

感谢我能得到的任何帮助。

附言。对于任何糟糕的语言(非母语人士)、错误使用的术语或错误的语法(第一次冒险进入 API),我深表歉意。

//WinMain.cpp

#include <Windows.h>

#include "Utility.h"
#include "Mouse.h"
#include "AppInfo.h"
#include "Buttons.h"

//Function prototypes
ATOM MainRegister();
bool MainInit(HWND &hWnd, int nCmdShow);
void MatchRectToWnd(RECT &rect);

//Variables define in AppInfo.h
HRGN rgnMain, rgnCaptionbar;
bool _APPRUNNING = true;
const char _APPTITLE[] = "Dark";

//Variables
bool WIREFRAME = false;

//Pointers to buttons (singelton design)
btnCloseClass * btnCloseClass::s_Instance = 0;
btnMaximizeClass * btnMaximizeClass::s_Instance = 0;
btnMinimizeClass * btnMinimizeClass::s_Instance = 0;

LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
    case WM_LBUTTONDBLCLK: //Left mouse button double-clicked
        Mouse.CheckDblClick(hWnd, wParam);
        Mouse.m_Pressed = false;
    break;
    case WM_LBUTTONDOWN: //Left mouse button clicked
        //Update the mouse position variables
        GetCursorPos(&Mouse.prevPt);
        GetCursorPos(&Mouse.m_LastClick);

        Mouse.m_Pressed = true; 

        Mouse.CheckClickDown(hWnd);
    break;
    case WM_LBUTTONUP: //Left mouse button released
    {
        GetCursorPos(&Mouse.prevPt);
        Mouse.CheckClickUp(hWnd);
        Mouse.m_Pressed = false;
    }
    break;
    case WM_SIZE: //Check if the window has been resized
    {
        //Update the buttons
        btnClose->Update(hWnd);
        btnMaximize->Update(hWnd);
        btnMinimize->Update(hWnd);

        //Update the regions
        RECT rect; GetWindowRect(hWnd, &rect);
        rgnMain = CreateRectRgn(0, 0, rect.right - rect.left, rect.bottom- rect.top);
        rgnCaptionbar = CreateRectRgn(0, 0, rect.right - rect.left, CAPTIONBAR_HEIGHT);

    }
    break;
    case WM_PAINT: //Draw the window
    {
        HDC hdc;
        PAINTSTRUCT ps;
        HBRUSH hBrush;

        hdc = BeginPaint(hWnd, &ps);

        //Color the mainregion
        hBrush = CreateSolidBrush(COLOR_MAIN);
        FillRgn(hdc, rgnMain, hBrush);

        //Color the captionbarregion
        hBrush = CreateSolidBrush(COLOR_CAPTIONBAR);
        FillRgn(hdc, rgnCaptionbar, hBrush);

        //Color the button backgrounds
        hBrush = CreateSolidBrush(COLOR_BUTTON_BACKGROUND);
        FillRgn(hdc, btnClose->GetRegion(), hBrush);
        FillRgn(hdc, btnMinimize->GetRegion(), hBrush);
        FillRgn(hdc, btnMaximize->GetRegion(), hBrush);

        //Color the button icons
        hBrush = CreateSolidBrush(COLOR_BUTTON_ICON);
        FillRgn(hdc, btnClose->GetIcon(), hBrush);
        FillRgn(hdc, btnMinimize->GetIcon(), hBrush);
        FillRgn(hdc, btnMaximize->GetIcon(), hBrush);

        //Paint the wireframe
        if (WIREFRAME)
        {
            hBrush = CreateSolidBrush(COLOR_WIREFRAME);
            FrameRgn(hdc, rgnMain, hBrush, 1, 1);
            FrameRgn(hdc, rgnCaptionbar, hBrush, 1, 1);
            FrameRgn(hdc, btnClose->GetRegion(), hBrush, 1, 1);
            FrameRgn(hdc, btnMaximize->GetRegion(), hBrush, 1, 1);
            FrameRgn(hdc, btnMinimize->GetRegion(), hBrush, 1, 1);
            FrameRgn(hdc, btnClose->GetIcon(), hBrush, 1, 1);
            FrameRgn(hdc, btnMaximize->GetIcon(), hBrush, 1, 1);
            FrameRgn(hdc, btnMinimize->GetIcon(), hBrush, 1, 1);
        }

        //Free up memomry
        DeleteObject(hBrush);
        EndPaint(hWnd, &ps);
    }
    break;
    case WM_KEYDOWN:
    {
        switch (wParam)
        {
        case VK_TAB: //If TAB is pressed
        {
            if (WIREFRAME) //Activate the wireframe
                WIREFRAME = false;
            else
                WIREFRAME = true;
            InvalidateRgn(hWnd, NULL, true);
        }
            break;
        case VK_ESCAPE: //If the ESCAPE is pressed
            PostMessage(hWnd, WM_DESTROY, 0, 0);
            break;
        }
    }
    break;
    case WM_DESTROY: //Free up memory and exit the program
        _APPRUNNING = false;
        DeleteObject(rgnMain);
        DeleteObject(rgnCaptionbar);
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, msg, wParam, lParam);
        break;
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    HWND hWnd;
    MSG msg;
    _APPRUNNING = true;

    //Register the main window
    if (!MainRegister())
    {
        MessageBox(hWnd, "Error registering main window!", "Error", MB_ICONERROR);
        return false;
    }

    //Initialize the main window
    MainInit(hWnd, nCmdShow);

    //App-loop
    while (_APPRUNNING)
    {
        if ((GetKeyState(VK_LBUTTON) & 0x80) == 0) //Make sure the mouse's status gets updated
        {
            Mouse.m_Pressed = false;
        }
        Mouse.Update(hWnd);

        //Message-loop
        if (PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE) > 0)
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    return msg.wParam;
}

bool MainInit(HWND &hWnd, int nCmdShow)
{
    //Create the window
    hWnd = CreateWindowEx(
        WS_EX_LAYERED,
        _APPTITLE,
        _APPTITLE,
        WS_OVERLAPPEDWINDOW,
        0, 0, //Starting positons (x,y)
        START_WIDTH, START_HEIGHT, //Width and height
        NULL, //Parent-handle
        NULL, //Menu-handle
        GetModuleHandle(NULL),
        NULL);

    //Make sure the window was created properly
    if (hWnd == NULL)
    {
        MessageBox(hWnd, "Error initializing main window!", "Error!", MB_ICONERROR);
        return false;
    }

    SetLayeredWindowAttributes(hWnd, NULL, NULL, NULL);

    //Get and set the window's style
    DWORD wndStyle = GetWindowLong(hWnd, GWL_STYLE);
    wndStyle &= ~(WS_CAPTION);
    SetWindowLong(hWnd, GWL_STYLE, wndStyle);

    //Create regions
    rgnMain = CreateRectRgn(0, 0, START_WIDTH, START_HEIGHT);
    rgnCaptionbar = CreateRectRgn(0, 0, START_WIDTH, CAPTIONBAR_HEIGHT);

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    return true;
}

ATOM MainRegister()
{
    //Create the window's classEx (extended class)
    WNDCLASSEX wc;
    wc.cbClsExtra = 0;
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.cbWndExtra = 0;
    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hIcon = NULL;
    wc.hIconSm = NULL;
    wc.hInstance = GetModuleHandle(NULL);
    wc.lpfnWndProc = MainWndProc;
    wc.lpszClassName = _APPTITLE;
    wc.lpszMenuName = NULL;
    wc.style = CS_DBLCLKS;

    //Register the classEx
    return RegisterClassEx(&wc);
}

【问题讨论】:

  • 您的 WM_PAINT 处理程序会泄露资源,就像没人管事一样。这可能就是你的问题所在。 GDI 对象的限制非常高,但是如果重绘时像筛子一样漏水,并且强制重绘很多次,你最终会碰到它……你为什么不让操作系统按照操作系统的设计去做呢?让它绘制窗口标题和框架,处理鼠标事件和调整大小等。
  • 此外,如果您想要一个没有边框的可调整大小的窗口,请处理 WM_NCHITTEST 消息并返回适当的 HT* 值。然后让操作系统自行处理调整窗口大小。如果您想帮助/控制/修改它的操作方式,请处理 WM_WINDOWPOSCHANGING 消息。
  • WM_SIZE 处理程序也会泄漏区域。
  • @CodyGray 我在查看内存泄漏的位置时遇到了一些麻烦(每次都重复使用相同的句柄),但感谢您提及它。我不让它绘制窗口标题和框架的原因是因为我正在使用这些区域来制作我提到的 gui,而不是标准的窗口外观。看来我还有一些阅读要做。
  • 主要泄漏是当你调用hBrush = CreateSolidBrush(COLOR_MAIN); 然后hBrush = CreateSolidBrush(COLOR_CAPTIONBAR); 没有DeleteObject 之间。您在每个绘制周期创建 4-5 个画笔,但只删除 1 个。在 WM_SIZE 中,您不会在分配新区域之前删除区域。

标签: c++ winapi


【解决方案1】:

正如@CodyGray 所建议的,这里是工作代码以及处理释放区域内存的类和函数,现在也画笔了。

编辑:该问题是由创建而不是删除画笔 (HBRUSH) 时 WM_PAINT 中的内存泄漏引起的。

//WinMain.cpp

#include <Windows.h>

#include "Utility.h"
#include "Mouse.h"
#include "AppInfo.h"
#include "Buttons.h"

//Function prototypes
ATOM MainRegister();
bool MainInit(HWND &hWnd, int nCmdShow);
void MatchRectToWnd(RECT &rect);

//Variables define in AppInfo.h
HRGN rgnMain, rgnCaptionbar;
bool _APPRUNNING = true;
const char _APPTITLE[] = "Dark";

//Variables
bool WIREFRAME = false;

//Pointers to buttons (singelton design)
btnCloseClass * btnCloseClass::s_Instance = 0;
btnMaximizeClass * btnMaximizeClass::s_Instance = 0;
btnMinimizeClass * btnMinimizeClass::s_Instance = 0;

LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
    case WM_LBUTTONDBLCLK: //Left mouse button double-clicked
        Mouse.CheckDblClick(hWnd, wParam);
        Mouse.m_Pressed = false;
    break;
    case WM_LBUTTONDOWN: //Left mouse button clicked
        //Update the mouse position variables
        GetCursorPos(&Mouse.prevPt);
        GetCursorPos(&Mouse.m_LastClick);

        Mouse.m_Pressed = true; 

        Mouse.CheckClickDown(hWnd);
    break;
    case WM_LBUTTONUP: //Left mouse button released
    {
        GetCursorPos(&Mouse.prevPt);
        Mouse.CheckClickUp(hWnd);
        Mouse.m_Pressed = false;
    }
    break;
    case WM_SIZE: //Check if the window has been resized
    {
        DeleteObject(rgnMain);
        DeleteObject(rgnCaptionbar);

        //Update the buttons
        btnClose->Update(hWnd);
        btnMaximize->Update(hWnd);
        btnMinimize->Update(hWnd);

        //Update the regions
        RECT rect; GetWindowRect(hWnd, &rect);
        rgnMain = CreateRectRgn(0, 0, rect.right - rect.left, rect.bottom- rect.top);
        rgnCaptionbar = CreateRectRgn(0, 0, rect.right - rect.left, CAPTIONBAR_HEIGHT);
    }
    break;
    case WM_PAINT: //Draw the window
    {

        HDC hdc;
        PAINTSTRUCT ps;
        HBRUSH hBrush;

        hdc = BeginPaint(hWnd, &ps);

        //Color the mainregion
        hBrush = CreateSolidBrush(COLOR_MAIN);
        FillRgn(hdc, rgnMain, hBrush);
        DeleteObject(hBrush);

        //Color the captionbarregion
        hBrush = CreateSolidBrush(COLOR_CAPTIONBAR);
        FillRgn(hdc, rgnCaptionbar, hBrush);
        DeleteObject(hBrush);

        //Color the button backgrounds
        FillRgn(hdc, btnClose->GetRegion(), btnClose->GetBrush());
        FillRgn(hdc, btnMinimize->GetRegion(), btnMinimize->GetBrush());
        FillRgn(hdc, btnMaximize->GetRegion(), btnMaximize->GetBrush());

        //Color the button icons
        FillRgn(hdc, btnClose->GetIcon(), btnClose->GetBrushIcon());
        FillRgn(hdc, btnMinimize->GetIcon(), btnMinimize->GetBrushIcon());
        FillRgn(hdc, btnMaximize->GetIcon(), btnMaximize->GetBrushIcon());

        //Paint the wireframe
        if (WIREFRAME)
        {
            hBrush = CreateSolidBrush(COLOR_WIREFRAME);
            FrameRgn(hdc, rgnMain, hBrush, 1, 1);
            FrameRgn(hdc, rgnCaptionbar, hBrush, 1, 1);
            FrameRgn(hdc, btnClose->GetRegion(), hBrush, 1, 1);
            FrameRgn(hdc, btnMaximize->GetRegion(), hBrush, 1, 1);
            FrameRgn(hdc, btnMinimize->GetRegion(), hBrush, 1, 1);
            FrameRgn(hdc, btnClose->GetIcon(), hBrush, 1, 1);
            FrameRgn(hdc, btnMaximize->GetIcon(), hBrush, 1, 1);
            FrameRgn(hdc, btnMinimize->GetIcon(), hBrush, 1, 1);
            DeleteObject(hBrush);
        }

        //Free up memomry
        DeleteObject(hBrush);
        EndPaint(hWnd, &ps);
    }
    break;
    case WM_KEYDOWN:
    {
        switch (wParam)
        {
        case VK_TAB: //If TAB is pressed
        {
            if (WIREFRAME) //Activate the wireframe
                WIREFRAME = false;
            else
                WIREFRAME = true;
            InvalidateRgn(hWnd, NULL, true);
        }
        break;
        case VK_ESCAPE: //If the ESCAPE is pressed
            PostMessage(hWnd, WM_DESTROY, 0, 0);
        break;
        }
    }
    break;
    case WM_DESTROY: //Free up memory and exit the program
        _APPRUNNING = false;
        DeleteObject(rgnMain);
        DeleteObject(rgnCaptionbar);
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, msg, wParam, lParam);
        break;
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    HWND hWnd;
    MSG msg;
    _APPRUNNING = true;

    //Register the main window
    if (!MainRegister())
    {
        MessageBox(hWnd, "Error registering main window!", "Error", MB_ICONERROR);
        return false;
    }

    //Initialize the main window
    MainInit(hWnd, nCmdShow);

    //App-loop
    while (_APPRUNNING)
    {
        if ((GetKeyState(VK_LBUTTON) & 0x80) == 0) //Make sure the mouse's status gets updated
        {
            Mouse.m_Pressed = false;
        }
        Mouse.Update(hWnd);

        //Message-loop
        if (PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE) > 0)
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    return msg.wParam;
}

bool MainInit(HWND &hWnd, int nCmdShow)
{
    //Create the window
    hWnd = CreateWindowEx(
        WS_EX_LAYERED,
        _APPTITLE,
        _APPTITLE,
        WS_OVERLAPPEDWINDOW,
        0, 0, //Starting positons (x,y)
        START_WIDTH, START_HEIGHT, //Width and height
        NULL, //Parent-handle
        NULL, //Menu-handle
        GetModuleHandle(NULL),
        NULL);

    //Make sure the window was created properly
    if (hWnd == NULL)
    {
        MessageBox(hWnd, "Error initializing main window!", "Error!", MB_ICONERROR);
        return false;
    }

    SetLayeredWindowAttributes(hWnd, NULL, NULL, NULL);

    //Get and set the window's style
    DWORD wndStyle = GetWindowLong(hWnd, GWL_STYLE);
    wndStyle &= ~(WS_CAPTION | WS_SIZEBOX);
    SetWindowLong(hWnd, GWL_STYLE, wndStyle);

    //Create regions
    rgnMain = CreateRectRgn(0, 0, START_WIDTH, START_HEIGHT);
    rgnCaptionbar = CreateRectRgn(0, 0, START_WIDTH, CAPTIONBAR_HEIGHT);

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    return true;
}

ATOM MainRegister()
{
    //Create the window's classEx (extended class)
    WNDCLASSEX wc;
    wc.cbClsExtra = 0;
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.cbWndExtra = 0;
    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hIcon = NULL;
    wc.hIconSm = NULL;
    wc.hInstance = GetModuleHandle(NULL);
    wc.lpfnWndProc = MainWndProc;
    wc.lpszClassName = _APPTITLE;
    wc.lpszMenuName = NULL;
    wc.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;

    //Register the classEx
    return RegisterClassEx(&wc);
}

//Button.h
//Header file for the class responsible for handling the memory of the
//regions and brushes
class Button
{
protected:
    //Protected default constructor
    Button();

    //Protected default destructor
    ~Button();

    //Protected variables
    HRGN m_Rgn;
    HRGN m_Icon;
    HBRUSH m_hBrush;
    HBRUSH m_hBrush_Icon;
    bool m_Hovered;
public:
    //Public functions
    virtual void CreateIcon() = 0;
    virtual void Update(HWND hWnd) = 0;
    bool Clicked(POINT pt);
    bool CheckIfHovered(HWND hWnd);
    HRGN GetRegion() { return m_Rgn; }
    HRGN GetIcon() { return m_Icon; }
    HBRUSH GetBrush() { return m_hBrush; }
    HBRUSH GetBrushIcon() { return m_hBrush_Icon; }
};

//Button.cpp
//Default constructor
Button::Button()
{
    m_hBrush = CreateSolidBrush(COLOR_BUTTON_BACKGROUND);
    m_hBrush_Icon = CreateSolidBrush(COLOR_BUTTON_ICON);
    m_Hovered = false;
}

//Default destructor
Button::~Button()
{
    //Free up memory
    DeleteObject(m_Rgn);
    DeleteObject(m_Icon);
    DeleteObject(m_hBrush);
    DeleteObject(m_hBrush_Icon);
}

再次感谢@CodyGray @theB 和@AdrianMcCarthy!

【讨论】:

    猜你喜欢
    • 2011-03-17
    • 1970-01-01
    • 2011-07-09
    • 2021-09-20
    • 2023-03-09
    • 1970-01-01
    • 2016-04-07
    • 1970-01-01
    • 2022-06-10
    相关资源
    最近更新 更多