Bitmaps

This is a discussion on Bitmaps within the Windows Programming forums, part of the Platform Specific Boards category; Hi again. I've been goin' through the forgers tutorial over the past couple of days, and I have a question ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    183

    Bitmaps

    Hi again.

    I've been goin' through the forgers tutorial over the past couple of days, and I have a question about the bitmap section.

    Bitmap section of the Forgers tutorial.

    I pretty much copied and pasted that into Dev-C++, but I got two errors I really don't understand.

    Code:
    Line : g_hbmBall = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BALL));
    
    Error for that line : IDB_BALL undeclared. (First use this function.)
    I understand that I must declare stuff before using them, but what is the IDB part about ? Here's error two :

    Code:
    Line : HBITMAP hbmOld = SelectObject(hdcMem, g_hbmBall);
    
    Error for that line : Invalid conversion from `void*' to `HBITMAP__*'
    Again, I understand what the error is saying, but haven't got a clue as to how to fix it. Is this code correct ?

    Thanks for any future replies.

  2. #2
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    You should download the source code that theForger provides. IDB_BALL is a #define in a resource header. It's also probably included in a resource script. You'll need both to compile your application.

    The invalid cast you're getting is a casting error because you're compiling poorly written C code with C++. Just add a (HBITMAP) cast before SelectObject and that error should go away.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    Just downloaded. That's amazingly complicated, but the errors went away.
    Cheers Dante, you rule !

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    It's not complicated at all. Forget all the other defines, they are crap. Just remind the #define IDB_BALL 101 part.

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,855
    >>Forget all the other defines, they are crap. Just remind the #define IDB_BALL 101 part.

    The IDB_BALL will be an image. Will need the resource script .rc file (which contains the actual image, bmp) or the code will still do nothing because it will fail to load the image.


    >>The invalid cast you're getting is a casting error because you're compiling poorly written C code with C++. Just add a (HBITMAP) cast before SelectObject and that error should go away.

    Or if using C (not C++) use .c file not .cpp files. Then you will not need the cast.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    How can a .rc file contain an image ? The image destination or what ?

  7. #7
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Quote Originally Posted by Necrofear
    How can a .rc file contain an image ? The image destination or what ?
    The BITMAP resource-definition statement has the following format

    Code:
    nameID BITMAP filename
    Examples on that page

    Code:
    disk1   BITMAP disk.bmp
    12      BITMAP diskette.bmp

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    Ah. So is that all I need in the resource, or do I need all the other junk as well ?
    Cheers.
    Last edited by Necrofear; 06-13-2006 at 10:49 AM.

  9. #9
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Quote Originally Posted by Necrofear
    do I need all the other junk as well ?
    Depends. Sometimes the compiler you're using needs them to identify stuff.
    Try removing them and see if it compiles.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    It is compiling, but it isn't loading the bmp. I think I did something wrong.

    It comes up with an error message saying could not load Ball. ( The error message is coded in the program. It is not a message from the system. ) Anyway, when I click OK, it creates a blank window.

    I'm lost. If anyone knows what's goin on, ( despite lack of code / description ) please help.

  11. #11
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Make sure you added the resource script to your project.

    If all fails, zip up all your files, upload them, and hopefully someone here can take a look.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    Won't it be easier to post it ? Or is there some exellence about zip files that I'm missing ?

    P.S. Sorry, that wasn't meant to sound sarcastic.

  13. #13
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    It could be a problem with your project file, your resource file, your .cpp file...etc

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    183

    Code

    I'll post them all then :

    Test.cpp :
    Code:
    #include <windows.h>
    #include "resource.h"
    
    int mb_result = 0;                                          // For quit message.
    const char g_szClassName[] = "WindowClass";
    BOOL RemoveMenu(HMENU hMenu, UINT uPosition, UINT uFlags);  // For system menu editing.
    
    HBITMAP g_hbmBall = NULL;
    
    // Step 4: the Window Procedure
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        
        switch(msg)
        
        {
            HMENU hSysMenu;    // System menu handle.
            
            case WM_CREATE:
            
            {
                ////////////////////////////////////////////////////////////////////
                //////////////////////////// BMP Stuff /////////////////////////////
                ////////////////////////////////////////////////////////////////////
                
                g_hbmBall = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BALL));
                
                if( g_hbmBall == NULL )
                {
                    
                    MessageBox(hwnd, "Could not load bitmap!", "Error", MB_OK | MB_ICONEXCLAMATION);
                    
                }    
                
                ////////////////////////////////////////////////////////////////////
                
                hSysMenu = GetSystemMenu(hwnd, FALSE);
    		    RemoveMenu(hSysMenu, 1, MF_BYPOSITION);
    		    
    		    hSysMenu = GetSystemMenu(hwnd, FALSE);
    		    RemoveMenu(hSysMenu, 2, MF_BYPOSITION);
    		    
    		    hSysMenu = GetSystemMenu(hwnd, FALSE);
    		    RemoveMenu(hSysMenu, 3, MF_BYPOSITION);    // Get rid of system menu items.
    		    
    		    hSysMenu = GetSystemMenu(hwnd, FALSE);
    		    RemoveMenu(hSysMenu, 4, MF_BYPOSITION);
    		    
    		    hSysMenu = GetSystemMenu(hwnd, FALSE);
    		    RemoveMenu(hSysMenu, 5, MF_BYPOSITION);
    		    
    		    hSysMenu = GetSystemMenu(hwnd, FALSE);
    		    RemoveMenu(hSysMenu, 6, MF_BYPOSITION);
    		
            }
    		
    	break;
    		
    		
    		
    	case WM_PAINT:
            
            {
                BITMAP bm;
                PAINTSTRUCT ps;
    
                HDC hdc = BeginPaint(hwnd, &ps);
    
                HDC hdcMem = CreateCompatibleDC(hdc);
                HBITMAP hbmOld = ( HBITMAP ) SelectObject(hdcMem, g_hbmBall);
    
                GetObject(g_hbmBall, sizeof(bm), &bm);
    
                BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
    
                SelectObject(hdcMem, hbmOld);
                DeleteDC(hdcMem);
    
                EndPaint(hwnd, &ps);
            
            }
            
            break;
    
    		
    	////////////////////////////// Menu Actions ////////////////////////////
    		
    	case WM_COMMAND:    // Menu was pressed:
    		    
                switch(LOWORD(wParam))    // 
                
                {
                    
                    case ID_FILE_EXIT:    // ID_FILE_EXIT is pressed:
                    
                    {
                        
                        PostMessage(hwnd, WM_CLOSE, 0, 0);    // Exit.
                        
                    }
                    
                    break;
                    
                    
                    
                    case ID_EXECUTE_RUN:
                    
                    {
                        
                        MessageBox(NULL, "This is where you'd put your 'Run' code.", "Test", MB_OK);
                        
                    }
                    
                    break;
                    
                }
            
            break;    
    		
            ////////////////////////////////////////////////////////////////////////
    		
            case WM_CLOSE:
            
            mb_result = MessageBox ( NULL, "        Do you wish to exit the program ?                    ", "   Exit", MB_YESNO | MB_ICONEXCLAMATION );
            
            if ( mb_result == IDYES )
            
            {
            
                DestroyWindow(hwnd);
                
            }    
            
            break;
            
            
            
            case WM_DESTROY:
                
            DeleteObject(g_hbmBall);
    
            PostQuitMessage(0);
            
            break;
            
            
            
            default:
            
            return DefWindowProc(hwnd, msg, wParam, lParam);
        
        }
        
        return 0;
    
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
        LPSTR lpCmdLine, int nCmdShow)
    {
        HMENU GetSystemMenu(HWND hWnd, BOOL bRevert);   // Handle for system menu menu.
        WNDCLASSEX wc;
        HWND hwnd;
        MSG Msg;
    
        //Step 1: Registering the Window Class
        
        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 + 2 );
        wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MENU);
        wc.lpszClassName = g_szClassName;
        wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
        if(!RegisterClassEx(&wc))
        {
            MessageBox(NULL, "Window Registration Failed!", "   Error!", MB_ICONERROR | MB_OK);
            return 0;
        }
    
        // Step 2: Creating the Window
        
        hwnd = CreateWindowEx(
            0,
            g_szClassName,
            "Win32",
            WS_BORDER | WS_CAPTION | WS_SYSMENU,
            0, 0, 250, 250,
            NULL, NULL, hInstance, NULL);
    
        if(hwnd == NULL)
        {
            MessageBox(NULL, "Window Creation Failed!", "   Error!",
            MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        ShowWindow(hwnd, SW_MAXIMIZE);
        UpdateWindow(hwnd);
    
        // Step 3: The Message Loop
        
        while(GetMessage(&Msg, NULL, 0, 0) > 0)
        {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
        
        return Msg.wParam;
    }
    Here's resource.h :

    Code:
    #define IDR_MENU    0x12
    
    #define ID_FILE_EXIT	13
    
    #define ID_EXECUTE_RUN  14
    
    #define ID_EXECUTE_DEFAULT  15
    
    /////////////// Bitmap . ///////////////
    
    #define IDB_BALL 0x012
    Finally Menu.rc :

    Code:
    #include "resource.h" 
    
    IDR_MENU MENU
    
    BEGIN
    
        POPUP "&File"
    
        BEGIN 
    
            MENUITEM "Exit", 13
    
        END
        
        POPUP "&Execute"
        
        BEGIN
        
            MENUITEM "Run", 14
            MENUITEM "&Default", 15, GRAYED
            
        END
    END
    
    /////////////// Bitmap. ///////////////
    
    0x6263 BITMAP "C:/Dev-Cpp/Programs/Test/Ball.bmp"
    That's it. It's probably just some stupid mistake I've made. (I hope not, that would be so embarrassing.) However, if anyone notices anythin' wrong with the code, please point it out to me. Thanks to anyone who can help.

    Cheers

    P.S. All resources and headers have been added to the project.
    Last edited by Necrofear; 06-13-2006 at 12:42 PM.

  15. #15
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Your values for your ball resource are different in the header and resource script.

    Change this line resource script

    Code:
    0x6263 BITMAP "C:/Dev-Cpp/Programs/Test/Ball.bmp"
    to

    Code:
    IDB_BALL BITMAP "C:/Dev-Cpp/Programs/Test/Ball.bmp"

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing bitmaps efficiently
    By scwizzo in forum Windows Programming
    Replies: 28
    Last Post: 06-30-2009, 08:25 PM
  2. DX9 Not Displaying Bitmaps
    By Sentral in forum Game Programming
    Replies: 9
    Last Post: 01-31-2006, 04:35 AM
  3. MFC: Multiple clicks shows multiple bitmaps - how?
    By BrianK in forum Windows Programming
    Replies: 0
    Last Post: 06-20-2004, 07:25 PM
  4. Loading bitmaps with createfile() +CreateDIBSection
    By Stevo in forum Windows Programming
    Replies: 7
    Last Post: 05-13-2004, 09:22 PM
  5. using standard bitmaps in the toolbar editor
    By Kibble in forum Windows Programming
    Replies: 0
    Last Post: 12-23-2002, 07:43 PM

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