Bitmap-Ressource wird erst nach resize vom Fenster angezeigt [gelöst]



  • Ich habe eine Bitmap als Hintergundbild in mein Programm geladen,
    welches allerdings erst erscheint, wenn man die Größe des Fensters ändert.
    Habe mit UpdateWindow() , InvalidateRect() und Konsorten versucht,
    eine WM_PAINT Nachricht zu schicken, hat aber auch nichts gebracht. Was mache ich falsch?

    #include "resource.h"
    #include <windows.h>
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
    
    HWND hwnd;
    HINSTANCE hIn;
    
    int WINAPI WinMain (HINSTANCE hI, HINSTANCE hPrI, PSTR szCmdLine, int iCmdShow)
    {
     char szName[] = "Fensterklasse";
     WNDCLASS wc;
    
     wc.style         = CS_HREDRAW | CS_VREDRAW;
     wc.lpfnWndProc   = WndProc;
     wc.cbClsExtra    = 0;
     wc.cbWndExtra    = 0;
     wc.hInstance     = hI;
     wc.hIcon         = LoadIcon (NULL, IDI_WINLOGO);
     wc.hCursor       = 0;
     wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
     wc.lpszMenuName  = NULL;
     wc.lpszClassName = szName;
    
     RegisterClass (&wc);
    
     hwnd = CreateWindow (szName, "MUSTER", WS_OVERLAPPEDWINDOW,
                                0, 0, 420, 350, NULL, NULL, hI, NULL);
     ShowWindow   (hwnd, iCmdShow);
     UpdateWindow (hwnd);
    
    //-----------------------------------------------------------------------------------
      MSG msg;
      hIn = hI;
      while (GetMessage (&msg, NULL, 0, 0))
      {
        TranslateMessage (&msg);
        DispatchMessage  (&msg);
      }
      return msg.wParam;
    }
    
    //-----------------------------------------------------------------------------------
    
    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
     HDC hdc, MemDCExercising;
     PAINTSTRUCT ps;
     HBITMAP bmpExercising;
    
     switch (message)
     {
      case WM_PAINT:
    		hdc = BeginPaint (hwnd, &ps);
    			// Load the bitmap from the resource
    		bmpExercising = LoadBitmap(hIn, MAKEINTRESOURCE(IDB_BITMAP1));
    
    		// Create a memory device compatible with the above DC variable
    		MemDCExercising = CreateCompatibleDC(hdc);
    		// Select the new bitmap
    		SelectObject(MemDCExercising, bmpExercising);
    
    		// Copy the bits from the memory DC into the current dc
    		BitBlt(hdc, 0, 0, 450, 400, MemDCExercising, 0, 0, SRCCOPY);
    		// Restore the old bitmap
    		DeleteDC(MemDCExercising);
    		DeleteObject(bmpExercising);
    		EndPaint (hwnd, &ps);
      return 0;
      case WM_DESTROY:
          PostQuitMessage (0);
      return 0;
     }
    
     return DefWindowProc (hwnd, message, wParam, lParam);
    }
    

    Dazugehörige Headerdatei:

    #ifndef IDC_STATIC
    #define IDC_STATIC (-1)
    #endif
    
    #define IDB_BITMAP1                             100
    

    Hier nochmal die Resource:

    // Generated by ResEdit 1.5.2
    // Copyright (C) 2006-2010
    // http://www.resedit.net
    
    #include <windows.h>
    #include <commctrl.h>
    #include <richedit.h>
    #include "resource.h"
    
    //
    // Bitmap resources
    //
    LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
    IDB_BITMAP1        BITMAP         "C:\\Users\\MUSTER\\Desktop\\bitmap_0110.bmp"
    


  • Beim ersten WM_PAINT ist hIn in der Funktion LoadBitmap noch NULL.

    Eigentlich reicht es, wenn du einfügst

    int WINAPI WinMain (HINSTANCE hI, HINSTANCE hPrI, PSTR szCmdLine, int iCmdShow)
    {
       hIn = hI; // <---
       char szName[] = "Fensterklasse"; 
       .
       .
       .
    

    edit:
    wenn du nur diese eine Grafik lädst, würde sich das in WM_CREATE anbieten...



  • Ich habs!

    InvalidateRect(hwnd, 0, 1); NACH UpdateWindow() einfügen!

    Endlich.

    @kpeter:

    Danke für den Tipp.


Anmelden zum Antworten