2014-04-28 22 views
1

我需要一些帮助。导入BitMap使我的窗口滞后

Im将位图导入我的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; 
} 

回答

5

您在MainWndProc中反复加载BMP文件。您应该在Init上加载一次,然后使用它!看看一个win32 API教程,你会发现MainWndProc在整个程序生命周期中都被调用。例如,您可以在WM_CREATE状态下加载该图像。

+0

是不是wm_create也一次又一次地被调用? – Nicholas

+0

@nico每个窗口最多发送一次WM_CREATE。 – IInspectable

+1

Yay谢谢你,我重构了我的代码,制作了一个资源头文件,从WM_CREATE获得了我的位图,现在它正在运行状态! @Inspectable谢谢,现在很清楚。 – Nicholas