导入BitMap使我的窗口滞后

时间:2014-04-28 12:07:57

标签: 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;
}

1 个答案:

答案 0 :(得分:5)

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