CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2007
    Posts
    8

    Bitmap not displaying

    For the last day or so I've been struggling to display a bitmap on a Window that I have created using the WINAPI C++. I started off using Loadbitmap(), but that was giving me problems in that the bitmap would never load because I had issues with resource handling (Dev C++). Now I'm using Loadimage() and I'm able to load the bitmap but it is not displayed in the window!? Here is the code I am using:
    Code:
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        HDC hDC, MemDCExercising;
        PAINTSTRUCT Ps;
        HBITMAP bmpExercising;
        BITMAP bm;
       
         switch (message)                  /* handle the messages */
        {
            case WM_CREATE:
            bmpExercising = (HBITMAP) LoadImage( hInst, "C:\\test.bmp", IMAGE_BITMAP ,0 ,0 ,LR_LOADFROMFILE);
    	    
    	    if(bmpExercising == NULL)
    				MessageBox(hwnd, "Could not load Exercise!", "Error", MB_OK | MB_ICONEXCLAMATION);
            break;
            
            case WM_DESTROY:
                PostQuitMessage (0);       
                break;
                
            case WM_PAINT:
    	        hDC = BeginPaint(hwnd, &Ps);
    	    
    	   
    	    MemDCExercising = CreateCompatibleDC(hDC);
                SelectObject(MemDCExercising, bmpExercising);
                GetObject(bmpExercising, sizeof(bm), &bm);
    	    BitBlt(hDC, 10, 10,bm.bmWidth, bm.bmHeight, MemDCExercising, 0, 0, SRCCOPY);
    
    	    
    	    DeleteDC(MemDCExercising);
    	    DeleteObject(bmpExercising);
    	    EndPaint(hwnd, &Ps);
    	    break;
            default:                      
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
    Most of the code in WM_PAINT was centred around me using Loadbitmap() before I gave up and I figured it could still be used for Loadimage(). Maybe this is causing the problem?

    In the long run I am hoping to change the picture displayed in the Window using data I receive from a winsock server as an argument as to which picture is shown. Can anyone see any potential difficulties with this? Thanks a lot.

  2. #2
    Join Date
    Sep 2004
    Location
    Italy
    Posts
    389

    Re: Bitmap not displaying

    WindowProcedure is called MANY times, and each time the bmpExercising variable is created and not initialized. So what actually happen is that you load the image with LoadImage, but then the handle is lost, and SelectObject select a (hopefully) NULL bitmap.

    Code:
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
       HDC hDC, MemDCExercising;
       PAINTSTRUCT Ps;
       static HBITMAP bmpExercising;

  3. #3
    Join Date
    Mar 2007
    Posts
    8

    Re: Bitmap not displaying

    Thanks for the reply, my understanding from what you said is that I need to declare bmpExercising from outside of the procedure?

  4. #4
    Join Date
    Mar 2007
    Posts
    8

    Re: Bitmap not displaying

    Great! Thanks, it worked after all of that.

    In the long run I'm hoping to change the picture displayed in the Window using data I receive from a winsock server as an argument as to which picture is shown.
    Would this be possible?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured