Question

I need some help here.

Im importing an bitmap onto my Win32 Window. I am building it and after some seconds it's starting to lag a lot. I am not sure why, but I suppose I am not correctly deleting it from memory after using it.

Thank you for Help in advance.

I saw a behavior while I was testing it. If Im not moving the window than it is okey, but after moving it it start to lag and block my IDE. Maybe something with WM_PAINT? Here is my code.

#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;
}
Was it helpful?

Solution

You load the BMP File over and over again in your MainWndProc. You should load it once at Init and use it from there! Have a look at a win32 API tutorial and you will see that MainWndProc is getting called throughout the whole program lifetime. You could load that image in your WM_CREATE state for example.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top