2014-04-28 21 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