c++ - 导入位图使我的窗口滞后

标签 c++ winapi bitmap win32gui

我需要一些帮助。

我正在将位图导入我的 Win32 窗口。我正在构建它,几秒钟后它开始滞后很多。我不确定为什么,但我想我在使用后没有正确地从内存中删除它。

提前感谢您的帮助。

我在测试时看到了一个行为。如果我不移动窗口就没关系,但在移动它之后它开始滞后并阻塞我的 IDE。也许是 WM_PAINT 的东西? 这是我的代码。

#include <windows.h>

//For more makros
#include <windowsx.h>
#include "Simulatron.h"




HINSTANCE hProgramInstance;
Simulatron Exo;

char Simulatron::m_szClassName[] = "Simulatron";



Simulatron::Simulatron(HINSTANCE hInstance)
{
    m_hInstance = hInstance; // Save Instance handle
    m_wndClass.cbSize = sizeof(WNDCLASSEX); // Must always be sizeof(WNDCLASSEX)
    m_wndClass.style = CS_DBLCLKS; // Class styles  
    m_wndClass.lpfnWndProc = MainWndProc; // Pointer to callback procedure
    m_wndClass.cbClsExtra = 0; // Extra bytes to allocate following the wndclassex structure
    m_wndClass.cbWndExtra = 0; // Extra bytes to allocate following an instance of the structure
    m_wndClass.hInstance = hInstance; // Instance of the application
    m_wndClass.hIcon = NULL;//LoadIcon(hInstance, MAKEINTRESOURCE(IDC_MAINCURSOR)); // Class Icon
    m_wndClass.hCursor = LoadCursor(NULL, IDC_ARROW); // Class cursor
    m_wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); // Background brush
    m_wndClass.lpszMenuName = NULL; // Menu Resource
    m_wndClass.lpszClassName = (LPCWSTR)m_szClassName; // Name of this class
    m_wndClass.hIconSm = NULL;//LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APP_ICON)); // Small icon for this class
}

Simulatron::~Simulatron()
{
}

Simulatron::Simulatron()
{
    // If we declare a window class with a default constructor,
    // we need to reset the window to a nothing
}
LRESULT CALLBACK Simulatron::MainWndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    static HDC hdc;
    static PAINTSTRUCT ps;
    static HDC hdc_mem;
    static HBRUSH newbrush;

    //Child Window Handles
    Simulatron create;
    RECT rect;

    hProgramInstance = (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE);

    static HBITMAP logo = NULL;
    static BITMAP  bitmap;
    logo = (HBITMAP)LoadImage(hProgramInstance, L"Space.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    GetObject(logo, sizeof(bitmap), &bitmap);

    switch (msg)
    {
    case WM_CREATE:
        {
            create.Create(hProgramInstance,hwnd,lParam,logo);

        }
        break;

    case WM_GETMINMAXINFO:
        {
            LPMINMAXINFO pInfo = (LPMINMAXINFO) lParam;
            //pInfo -> ptMaxTrackSize.x = 450;
            //pInfo -> ptMaxTrackSize.y = 650;  
        }
        break;

    case WM_SIZE:

        break;

    case WM_CTLCOLORSTATIC:

        SetTextColor((HDC)wParam, RGB(150, 100, 255));
        SetBkMode((HDC)wParam, TRANSPARENT);
        newbrush = (HBRUSH)GetStockObject(NULL_BRUSH);
        DeleteObject(newbrush);
        return (LRESULT)newbrush;
        break;

    case WM_COMMAND:

    break;

    case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);
        GetClientRect(hwnd , &rect);
        hdc_mem = CreateCompatibleDC(hdc);

        SelectObject(hdc_mem, logo);
        BitBlt(hdc, 0, 0, bitmap.bmWidth, bitmap.bmHeight, hdc_mem, 0, 0, SRCCOPY);

        DeleteObject(hdc_mem);
        EndPaint(hwnd, &ps);
        break;

        //Handle the combinations from the keyboard input

    case WM_DESTROY:
        PostQuitMessage (0);
        DeleteObject(logo);
        DeleteBitmap(logo);
        break;

    default:
        return DefWindowProc (hwnd, msg, wParam, lParam);
    }

    return 0;
}



//Create function of all Childs
void Simulatron::Create(HINSTANCE Hinst, HWND hWindow, LPARAM lParam, HBITMAP logo)
{

    Hinst = ((LPCREATESTRUCT) lParam) -> hInstance;                          // handle to instance for custom cursor
    logo = (HBITMAP)LoadImage(Hinst, L"Space.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
}

bool Simulatron::Run(int nCmdShow)
{
    if(!RegisterClassEx(&m_wndClass))
        return false;
    m_hwnd = CreateWindowEx(0,(LPCWSTR)m_szClassName,
        L"Simulatron",
        //WS_OVERLAPPEDWINDOW,
        WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX, // Dissable Resizing and Maximizing
        0, 0, 1280, 1000,
        NULL, NULL,
        m_hInstance,
        NULL);

    if(!m_hwnd)
        return false;

    ShowWindow(m_hwnd, nCmdShow);

    return true;
}

Simulatron::operator HWND()
{
    // This overloaded operator allows us to use HWND anyway we want
    return m_hwnd;
}

最佳答案

您在 MainWndProc 中一遍又一遍地加载 BMP 文件。您应该在 Init 加载一次并从那里使用它!查看 win32 API 教程,您会发现 MainWndProc 在整个程序生命周期中都被调用。例如,您可以在 WM_CREATE 状态下加载该图像。

关于c++ - 导入位图使我的窗口滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23340737/

相关文章:

Android - 如何加载大图像(将其转换为较小的尺寸)并将其作为 Base64 字节数组发送(避免 OutOfMemory 错误)

android - 使用网络服务和 picasso 加载图像,如何添加采样大小?

c++ - 在 C++ 中重载一元运算符有困难吗?

performance - 如何使用 Delphi 创建 Windows 性能计数器?

c++ - 在通过 ReadFile() 函数读取文件期间检查文件结尾

winapi - AdjustWindowRectEx() 和 GetWindowRect() 使用 WS_OVERLAPPED 给出错误的大小

c++ - 获取在可变参数列表中传递的参数类型

c++ - 为什么 cout 立即输出?

c++ - 什么时候使用虚拟析构函数?

android - 在 Bitmap 上创建 EditText - Android 编程