Why won't my bitmap load

This is a discussion on Why won't my bitmap load within the Windows Programming forums, part of the Platform Specific Boards category; Heres all the code i have relating to my bitmap that won't load, anyone see whats wrong with it, i'm ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    81

    Why won't my bitmap load

    Heres all the code i have relating to my bitmap that won't load, anyone see whats wrong with it, i'm using Borland C++ 5.0


    //Test.RC
    TEST BITMAP DISCARDABLE "test.bmp"

    //In CPP file.

    static HBITMAP hBitmap;
    static int cxClient, cyClient, cxSource, cySource;
    BITMAP bitmap;
    HDC hdc,hdcMem;
    HINSTANCE hInstance;
    int x,y;
    PAINTSTRUCT ps;

    case WM_CREATE:
    {
    hBitmap = LoadBitmap(hInstance,"TEST");
    GetObject(hBitmap,sizeof(BITMAP), &bitmap);
    cxSource = bitmap.bmWidth;
    cySource = bitmap.bmHeight;
    return(0);
    }

    case WM_SIZE:
    {
    cxClient = LOWORD(lParam);
    cyClient = HIWORD(lParam);
    return(0);
    }
    case WM_PAINT:
    {
    hdc = BeginPaint(hwnd,&ps);
    hdcMem = CreateCompatibleDC(hdc);
    SelectObject(hdcMem,hBitmap);
    for(y=0;y<cyClient;y+=cySource)
    for(x=0;x<cxClient;x+=cxSource)
    {
    BitBlt(hdc,x,y,cxSource,cySource,hdcMem,0,0,SRCCOP Y);
    }
    DeleteDC(hdcMem);
    EndPaint(hwnd,&ps);
    return(0);
    } break;
    case WM_DESTROY:
    {
    DeleteObject(hBitmap);
    PostQuitMessage(0);
    return(0);
    } break;

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    317
    Did you make hInstance global? If not you need to retrieve it in your WinProc. Use:
    Code:
    case WM_CREATE:
                       hInstance=((LPCREATESTRUCT)lParam)->hInstance;
                       //rest of code

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    81
    Thanks, that works, never could get that to compile before dunno why it works now

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    317
    simple, you obtain the instance of you program in your WinMain function, however this is a local variable and goes out of scope when you leave that function. When you tried to load your bitmap, as a resource of your program with LoadBitmap(hInstance, "TEST"), you are telling the OS to look for a bitmap id of TEST in hInstance(your program). However since hInstance was never defined the OS did not know where to look, or actually it looked for it in whatever memory the unitialized handle(hInstance) held.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Do yourself a favor. Take this function and place it in your include files. Call it once in WinMain and forget about it. After being initialized, call this function without parameters whenever you need a handle to the current instance!
    I got so tired of passing this useless variable around and I haven't missed it since!

    ie:



    int WINAPI WinMain(HINSTANCE hThisInstance, //...)
    {
    Instance( hThisInstance ); //...initialize once...

    //...stuff
    }



    HBITMAP GetBitmap(char *name)
    {
    return LoadBitmap( Instance(), name ); //...get this instance...
    }




    Code:
    
    HINSTANCE Instance( HINSTANCE hThisInstance = NULL )
    {
     static HINSTANCE globalInstance;
    
     if(hThisInstance != NULL) globalInstance = hThisInstance;
    
     return globalInstance;
    }
    
    
    
    //...or try the AI version:
    
    
    HINSTANCE Instance( HINSTANCE hThisInstance = NULL )
    {
     static bool forgot = true;
    
     static HINSTANCE globalInstance;
    
     if(hThisInstance != NULL) 
      { 
        globalInstance = hThisInstance;
        
        forgot = false;
       }
    
     if(forgot) MessageBox(NULL, "You Forgot To Call Me In WinMain ;)", "Hey, Mr. Programmer:", 0);
    
     return globalInstance;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loading a bitmap (Without using glaux)
    By Shamino in forum Game Programming
    Replies: 7
    Last Post: 03-16-2006, 09:43 AM
  2. Load Bitmap as Window Background
    By Epo in forum Windows Programming
    Replies: 6
    Last Post: 07-19-2005, 10:37 AM
  3. OpenGL -- Bitmaps
    By HQSneaker in forum Game Programming
    Replies: 14
    Last Post: 09-06-2004, 05:04 PM
  4. 256-Color Bitmap in MFC Resource :: Doesn't Load Right
    By SyntaxBubble in forum Windows Programming
    Replies: 2
    Last Post: 09-16-2003, 04:55 PM
  5. bitmap not going onto screen
    By stallion in forum Windows Programming
    Replies: 4
    Last Post: 02-22-2003, 10:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21