导入位图会使我的窗口滞后

Importing a BitMap makes my Window Lagg

本文关键字:窗口 滞后 我的 位图 导入      更新时间:2023-10-16

我需要一些帮助。

我正在将位图导入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状态下加载该图像。