Thread: Super-Newbie. Need to see how to use LoadBitmap() correctly?

  1. #1
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    Unhappy Super-Newbie. Need to see how to use LoadBitmap() correctly?

    Hi, I am trying to load a bitmap as the background of my first windows app. So I tried using LoadBitmap() but I am using it wrong. I set the first parameter to "hwnd", and the second I set to the name of a pointer that contains the text: "C://a_bitmap.bmp". But this is not the type of pointer it wants. But the documentation specifically states:
    _________________________________________


    HBITMAP LoadBitmap(

    HINSTANCE hInstance, // handle of application instance
    LPCTSTR lpBitmapName // address of bitmap resource name
    );
    Parameters

    hInstance

    Identifies the instance of the module whose executable file contains the bitmap to be loaded.

    lpBitmapName

    Points to a null-terminated string that contains the name of the bitmap resource to be loaded.

    ________________________________________

    Can someone help?
    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;
    }

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Well for a start hInstance is a peramiter passed to WinMain. Dont use the handle to a window (hwnd).

    Second, if your using VC++, load the bitmap as a resource. Go to its properties and put inverted commas before and after its name (IE "MyBitmap" not MyBitmap)

    Then do

    char BM1[] = "MyBitmap";

    LoadBitmap(hInstance,BM1);

    I had loads of problems once with this function. This is the method I use and it should work.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Thanks, I will first change the "hwnd" to "hinstance" and see if it works. I don't have the Visual C++ compiler, I am using Bloodsheds Dev C++ compiler. Can't I keep the second parameter as a filepath(well a pointer to the text "C://myBitmap.bmp" ? If not then what?
    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;
    }

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    I always use rc files.

    Try creating a file with

    Code:
    MyBitmap BITMAP  "C:\MyBitmap.bmp"
    and save it as yourproject.rc

    now add that to the project when you compile (I have never used Bloodshed so I dont know how this is exactly done on that compiler).

    You should then be able to use

    Code:
    char BM1[] = "MyBitmap"; 
    
    LoadBitmap(hInstance,BM1);
    Anyway - give it a go see if it works

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Thank you. But shouldn't it be:

    BITMAP MyBitmap "C:\MyBitmap.bmp"

    ...instead of:

    MyBitmap BITMAP "C:\MyBitmap.bmp"
    ??



    Either way, I will try it!! Thanks!!
    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;
    }

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    Aaaaaaaarg!

    Ok, here's what I did:

    The program already generated a .rc file for the icon, I think, and so I just added that snippet to it, saved it, changed the "hwnd" to "hThisInstance"(that's my hinstance) in my program, and set the second parameter to "myBitmap". But not only does the compiler say that "myBitmap" is undeclared, but also "hThisInstance"!! I rechecked my code, and it should recognize "hThisInstance" at least. Now here's what the resource file looks like:
    ____________________________________
    500 ICON MOVEABLE PURE LOADONCALL DISCARDABLE "C:/DEV-C++/Icon/MAINICON.ICO"

    myBitmap BITMAP "C//:nuntitled.bmp"

    _____________________________________

    I also tried it the other way around, that is:

    BITMAP myBitmap "C//:nuntitled.bmp"

    ...didn't work either.

    Now, could it be that I should give it more "commands" as the icon also defined there was (the: ...MOVEABLE PURE LOADONCALL DISCARDABLE ...part?)

    I just don't know.

    As of now, all my app consists of is the window, it's title, a gray background, an ellipse, and text over that ellipse! At this rate, I should have a nice application written in Win95 by the release of Windows 2020!
    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;
    }

  7. #7
    Registered User Chemanuel's Avatar
    Join Date
    Aug 2001
    Posts
    13

    Re: Aaaaaaaarg!

    Originally posted by Sebastiani
    Ok, here's what I did:

    The program already generated a .rc file for the icon, I think, and so I just added that snippet to it, saved it, changed the "hwnd" to "hThisInstance"(that's my hinstance) in my program, and set the second parameter to "myBitmap". But not only does the compiler say that "myBitmap" is undeclared, but also "hThisInstance"!! I rechecked my code, and it should recognize "hThisInstance" at least. Now here's what the resource file looks like:
    ____________________________________
    500 ICON MOVEABLE PURE LOADONCALL DISCARDABLE "C:/DEV-C++/Icon/MAINICON.ICO"

    myBitmap BITMAP "C//:nuntitled.bmp"

    _____________________________________

    I also tried it the other way around, that is:

    BITMAP myBitmap "C//:nuntitled.bmp"

    ...didn't work either.

    I also use Dev-C++ and this is the correct way to do it.

    In the resource file include the bitmap like this

    PHP Code:
    myBitmap BITMAP "C:/nuntitled.bmp" 
    In the source code declare a general variable for the bitmap.

    PHP Code:
    HBITMAP hBmp
    You can do it also for HINSTANCE but I prefer to get it from hWnd.

    In the windows procedure include the following

    PHP Code:
    ....
    case 
    WM_CREATE:
            {
             
    HINSTANCE hInst GetWindowLong(hWndGWL_HINSTANCE);
             
    hBmp LoadBitmap(hInst"myBitmap");
            }
    break;

    ....

    case 
    WM_PAINT:
            
    /* Include here the functions to paint 
                the bitmap in the client area */
    break;

    .....

    case 
    WM_DESTROY:
            
    DeleteObject(hBmp);
    break;

    .... 
    hth
    Chemanuel

    Lo bueno si breve dos veces bueno (Baltasar Gracián 1601-1658)

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Thank you, sir. I will try it right away!
    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;
    }

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Focusing on this bit of code:

    ////////////////////////////////////////////////////////

    case WM_CREATE:
    {
    HINSTANCE hInst = GetWindowLong(hWnd, GWL_HINSTANCE);
    hBmp = LoadBitmap(hInst, "myBitmap");
    }
    break;

    //////////////////////////////////////////////////////////////

    ...there was an error. So I separated each sub-statement like so:

    GetWindowLong(hWnd,

    GWL_HINSTANCE); //line 105

    ...and there was definately an error at 105, though I looked it up and it looks correct.

    I even tried other GWL_'s and still the error.

    In every case, the compilers complaint was the same:

    "attempt to make pointer from integer without a cast..."

    Any ideas?

    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;
    }

  10. #10
    Unregistered
    Guest

    This might help...

    Try creating a global variable to hold your app's hinstance.

    eg.

    ---CODE---

    #include <windows.h>

    HINSTANCE hMainInst;

    int WINAPI WinMain (HINSTANCE hinstance, ...)
    {
    hMainInst = hinstance;
    // main code here
    }

    ---CODE ENDS---

    then remove the GetWindowLong function, and replace hInst with hMainInst.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. super newbie
    By kamikadam in forum C++ Programming
    Replies: 9
    Last Post: 08-05-2004, 01:18 AM
  2. With super powers would you:
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 09-17-2003, 11:27 PM
  3. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM
  4. Problems with LoadBitmap()
    By minesweeper in forum Windows Programming
    Replies: 4
    Last Post: 04-12-2002, 05:44 PM