2014-02-10 114 views
1

讓我說,我是很新的MFC應用程序開始,所以請多多包涵......如何在一個窗口中改變圖像MFC應用程序

我想創建一個窗口的圖像(讓我們說綠燈)。必要時,我希望應用程序更改圖像(黃色或紅色)。我有代碼,將創建窗口並顯示圖像(綠燈),但它不會更新圖像(當i == 15或I == 50)

#include "resource.h" 

using namespace std; 

const char g_szClassName[] = "myWindowClass"; 

HBITMAP Light = NULL; 

// Step 4: the Window Procedure 
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) 
{ 
    switch (msg) 
    { 
    case WM_PAINT: 
     { 
      BITMAP bm; 
      PAINTSTRUCT ps; 

      HDC hdc = BeginPaint(hwnd, &ps); 

      HDC hdcMem = CreateCompatibleDC(hdc); 
      HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, Light); 

      GetObject(Light, sizeof(bm), &bm); 

      BitBlt(hdc, 20, 180, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY); 

      SelectObject(hdcMem, hbmOld); 
      DeleteDC(hdcMem); 

      EndPaint(hwnd, &ps); 
     } 
    case WM_CREATE: 
     { 
      Light = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP1)); 
     } 
     break; 
    case WM_COMMAND: 
     switch (LOWORD(wParam)) 
     { 

     } 
     break; 
    case WM_CLOSE: 
     DestroyWindow(hwnd); 
     break; 
    case WM_DESTROY: 
    { 
     DeleteObject(Light); 
     PostQuitMessage(0); 
    } 
     break; 
    default: 
     return DefWindowProc(hwnd, msg, wParam, lParam); 
    } 
    return 0; 
} 

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
    LPSTR lpCmdLine, int nCmdShow) 
{ 
    WNDCLASSEX wc; 
    HWND hwnd; 
    MSG Msg; 
    int eventCode; 

    int i = 0; 

    wc.cbSize = sizeof(WNDCLASSEX); 
    wc.style = 0; 
    wc.lpfnWndProc = WndProc; 
    wc.cbClsExtra = 0; 
    wc.cbWndExtra = 0; 
    wc.hInstance = hInstance; 
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); 
    wc.hCursor = LoadCursor(NULL, IDC_ARROW); 
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); 
    wc.lpszMenuName = NULL; 
    wc.lpszClassName = (LPCSTR)g_szClassName; 
    wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); 

    if (!RegisterClassEx(&wc)) 
    { 
     MessageBox(NULL, (LPCSTR)"Window Registration Failed!", (LPCSTR)"Error!", 
      MB_ICONEXCLAMATION | MB_OK); 
     return 0; 
    } 

    // Step 2: Creating the Window 
    hwnd = CreateWindowEx(
     WS_EX_CLIENTEDGE, 
     (LPCSTR)g_szClassName, 
     (LPCSTR)"Test Picture", 
     WS_OVERLAPPEDWINDOW, 
     CW_USEDEFAULT, CW_USEDEFAULT, 420, 280, 
     NULL, NULL, hInstance, NULL); 

    if (hwnd == NULL) 
    { 
     MessageBox(NULL, (LPCSTR)"Window Creation Failed!", (LPCSTR)"Error!", 
      MB_ICONEXCLAMATION | MB_OK); 
     return 0; 
    } 

    ShowWindow(hwnd, nCmdShow); 
    UpdateWindow(hwnd); 

    // Step 3: The Message Loop 
    while (GetMessage(&Msg, NULL, 0, 0) > 0) 
    { 
     TranslateMessage(&Msg); 
     DispatchMessage(&Msg); 

     if (i == 15) 
     { 
      Light = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP3)); 
      UpdateWindow(hwnd); 
     } 
     else if (i == 50) 
     { 
      Light = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP2)); 
      UpdateWindow(hwnd); 
     } 
     i++; 
    } 

    return Msg.wParam; 
} 

我認爲我可以做

Light = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP3)); 
UpdateWindow(hwnd); 

更新窗口畫面,但是我錯了:(,有什麼能我用它來改變形象?

+0

您是否設置了斷點並遍歷代碼?你是否檢查API調用的返回碼? – rrirower

+0

我在這裏沒有看到任何MFC,只是Windows API的東西。 MFC *可能會使這更容易(CTimer等)。 – crashmstr

回答

2

需要調用InvalidateRect引起重繪。UpdateWindow不會做任何事情如果窗口的無效區域是空的。

相關問題