每次调整窗口大小时内存都会增加

编程入门 行业动态 更新时间:2024-10-28 18:32:54
本文介绍了每次调整窗口大小时内存都会增加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我创建了一个简单的 Win32 应用程序并尝试用颜色填充客户区.当包含Clear RenderTarget"这一行时,我看到每次调整窗口大小时内存都会增加几 KB.

I have created a simple Win32 Application and try to fill the Client Area with a Color. When the line "Clear RenderTarget" is included I see that the memory increases a few KB every time I resize the window.

我的 WindowProc:

My WindowProc:

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
        case WM_SIZE:
        {
            if (pRenderTarget != NULL)
            {
                RECT rc;
                GetClientRect(globalWindowHandle, &rc);

                D2D1_SIZE_U size = D2D1::SizeU(rc.right, rc.bottom);

                pRenderTarget->Resize(size);

                InvalidateRect(globalWindowHandle, NULL, FALSE);
            }
            return  0;
        }
        break;
        case WM_CREATE:
        {
            HRESULT dx = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &pFactory);

            if (FAILED(dx))
            {
                MessageBox(globalWindowHandle, "Error creating D2D1Factory", "Error", MB_ICONERROR);
                return -1;
            }

            return 0;
        }
        break;
        case WM_KEYDOWN:
        {
            int ret = HandleKeyboardInput(uMsg, wParam, lParam);

            if (ret == 0)
            {
                return 0;
            }
        }
        break;
        case WM_PAINT:
        {
            HRESULT hr = CreateGraphicsResources();

            if (SUCCEEDED(hr))
            {

                PAINTSTRUCT ps;
                HDC hdc = BeginPaint(hwnd, &ps);

                pRenderTarget->BeginDraw();

                // Clear RenderTarget
                pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::SkyBlue));

                hr = pRenderTarget->EndDraw();

                if (FAILED(hr) || hr == D2DERR_RECREATE_TARGET)
                {
                    pRenderTarget->Release();
                    pSolidBrush->Release();
                    pRenderTarget = NULL;
                    pSolidBrush = NULL;
                }

                EndPaint(hwnd, &ps);

                return 0;
            }

        }
        break;
        case WM_CLOSE:
        {
            int box = MessageBox(hwnd, "Would you like to close the editor ?", "Question", MB_OKCANCEL);
            if (box == IDOK)
            {
                DestroyWindow(hwnd);
            }
            return 0;
        }
        break;
        case WM_DESTROY:
        {
            PostQuitMessage(0);
            return 0;
        }
        break;
        default:
        {
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
        }
        break;
    }

    return 0;
}

创建图形资源

HRESULT CreateGraphicsResources()
{
    HRESULT hr = S_OK;

    if (pRenderTarget == NULL)
    {
        RECT rc;
        GetClientRect(globalWindowHandle, &rc);

        D2D1_SIZE_U size = D2D1::SizeU(rc.right, rc.bottom);

        hr = pFactory->CreateHwndRenderTarget(
            D2D1::RenderTargetProperties(),
            D2D1::HwndRenderTargetProperties(globalWindowHandle, size),
            &pRenderTarget);

        if (SUCCEEDED(hr))
        {
            const D2D1_COLOR_F color = D2D1::ColorF(1.0f, 1.0f, 1.0f, 0);
            hr = pRenderTarget->CreateSolidColorBrush(color, &pSolidBrush);

            if (SUCCEEDED(hr))
            {

            }
        }


    }
    return hr;
}

全局变量:

BOOL ctrlPressed = FALSE;
HWND globalWindowHandle;
ID2D1Factory *pFactory;
ID2D1SolidColorBrush *pSolidBrush;
ID2D1HwndRenderTarget *pRenderTarget;

我是否错过了一些释放内存的东西,或者可能是什么原因?如果我调整窗口大小,例如5 秒内存从 4KB 增加到 22KB.

Do I miss something to free up memory or what could be the reason? If I resize the window e.g. 5 sec the memory goes from 4KB up to 22KB.

我的操作系统是 Windows 10 x64

My OS is Windows 10 x64

推荐答案

问题似乎已解决.实际上内存并没有增加太多,所以似乎操作系统分配了其他内存.

The issue seems to be solved. In fact the memory did not go up much more so it seems the OS assigns the other memory.

这篇关于每次调整窗口大小时内存都会增加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-25 23:58:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1125555.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:都会   内存   窗口   小时

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!