Thread: bitmap not going onto screen

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    157

    bitmap not going onto screen

    i just did up a bitmap (.bmp) in mspaint so i can display on my main window when the program starts up. i first put the bitmap as a resource and then loaded it. in the code i load the bitmap and work with a device context and a memdc so i can display the bitmap on the client area. but when i run the program, the bitmap does not show up on the window like i thought i coded it to.

    here's my resource file with the bitmap in it:
    Code:
    #include <windows.h>
    #include "resources.h"
    
    500 ICON MOVEABLE PURE LOADONCALL DISCARDABLE "C:/Dev-C++/Icon/MAINICON.ICO"
    
    MainBMP BITMAP "C:/Progs/WinAPI/WinQuiz/WinQuiz.bmp"
    
    OptDlg DIALOG 5, 5, 150, 100
    STYLE DS_MODALFRAME | WS_VISIBLE | WS_SYSMENU | WS_MINIMIZEBOX | WS_POPUP | WS_CAPTION | DS_MODALFRAME
    CAPTION "Quizer Options/Settings"
    {
     DEFPUSHBUTTON "Continue...", IDD_OKAY, 32, 50, 30, 14, WS_CHILD | WS_VISIBLE | WS_TABSTOP
     PUSHBUTTON "Exit", IDB_EXIT, 32, 50, 30, 14, WS_CHILD | WS_VISIBLE | WS_TABSTOP
     EDITTEXT IDE_EFILE, 5, 5, 25, 10, ES_LEFT | ES_AUTOHSCROLL | WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP
     GROUPBOX "File Chooser", IDF_FFRAME, 1, 1, 40, 20
     PUSHBUTTON "Browse...", IDB_BROWSE, 1, 1, 40, 20, WS_CHILD | WS_VISIBLE | WS_TABSTOP
     CTEXT "Recent Files:", IDS_RECLAB, 1, 1, 40, 20, WS_CHILD | WS_VISIBLE
     LISTBOX IDL_RECENT, 1, 1, 40, 20, WS_CHILD | LBS_NOTIFY | WS_VISIBLE | WS_BORDER | WS_VSCROLL |WS_TABSTOP
     PUSHBUTTON "Add/Remove", IDB_ADDREM, 1, 1, 40, 20, WS_CHILD | WS_VISIBLE | WS_TABSTOP
    }
    and here's the snippet of code that i use to load and display the bitmap:
    Code:
    /* get a handle to the bitmap */
                hMainBMP = LoadBitmap(hInst, "MainBMP");
    
                if(hMainBMP == NULL)
                    MessageBox(NULL, TEXT("Couldn't display bitmap!"), TEXT("Error!"), 0);
    
                /* display the bitmap on the window */
                hDC = GetDC(hwnd);
                memDC = CreateCompatibleDC(hDC);
                SelectObject(memDC, hMainBMP);
                BitBlt(hDC, 0, 0, 363, 222, memDC, 0, 0, SRCCOPY);
                ReleaseDC(hwnd, hDC);
                DeleteDC(memDC);
    what could i possibly be doing wrong so that the bitmap is not being displayed on the window like i'm expecting it to?

    thanks!

  2. #2
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798
    I had this problem once. Try making the handle to the bitmap a static variable.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    20
    Instead of this......

    Code:
    /* get a handle to the bitmap */
                hMainBMP = LoadBitmap(hInst, "MainBMP");
    
                if(hMainBMP == NULL)
                    MessageBox(NULL, TEXT("Couldn't display bitmap!"), TEXT("Error!"), 0);
    
                /* display the bitmap on the window */
                hDC = GetDC(hwnd);
                memDC = CreateCompatibleDC(hDC);
                SelectObject(memDC, hMainBMP);
                BitBlt(hDC, 0, 0, 363, 222, memDC, 0, 0, SRCCOPY);
                ReleaseDC(hwnd, hDC);
                DeleteDC(memDC);
    Try this.....
    Code:
    /* get a handle to the bitmap */
                hMainBMP = LoadBitmap(hInst, "MainBMP");
                HBITMAP OldBitmap;
    
                if(hMainBMP == NULL)
                    MessageBox(NULL, "Couldn't display bitmap!", "Error!"), MB_OK | MB_ICONERROR);
    
                /* display the bitmap on the window */
                hDC = GetDC(hwnd);
                memDC = CreateCompatibleDC(hDC);
                // Draws Bitmap
                OldBitmap= (HBITMAP)SelectObject(memDC, hMainBMP);
                BitBlt(hDC, 0, 0, 363, 222, memDC, 0, 0, SRCCOPY);
                SelectObject(memDC, OldBitmap);
    
                ReleaseDC(hwnd, hDC);
                DeleteDC(memDC);

  4. #4
    Registered User SAMSAM's Avatar
    Join Date
    Nov 2001
    Posts
    218
    What about your hInstance?is it global ? and how are you retracting it?;

    if not global.before loadbitmap do this;
    hInstance = (LPCREATESTRUCT) lParam)->hInstance)))

    and as minesweeper said, make your handle static.



    cheers!

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    157
    i have fixed the problem but i dont know why i have fixed it and the reasons behind what my other problem was. now the bitmap is correctly place on the screen where i want it. what i had before was i was putting the bitmap on the window in the wm_create message, so i decided that i should probably put it when the window receives the wm_paint message. so i did that and it all worked out. here's the code snipped:
    Code:
                /* get a handle to the bitmap */
                hMainBMP = LoadBitmap(hInst, TEXT("MainBMP"));
    
                if(hMainBMP == NULL)
                    MessageBox(NULL, TEXT("Couldn't display bitmap!"), TEXT("Error!"), 0);
    
                /* display the bitmap on the window */
                return 0;
    
            case WM_PAINT:
                hDC = BeginPaint(hwnd, &ps);
    
                memDC = CreateCompatibleDC(hDC);
                SelectObject(memDC, hMainBMP);
    
                BitBlt(hDC, 13, 8, 363, 222, memDC, 0, 0, SRCCOPY);
    
                DeleteDC(memDC);
                EndPaint(hwnd, &ps);
                return 0;
    now, i have no idea why the code didnt work while it was in wm_create. maybe because the window had not had its initial drawing and i was trying to put an image on something that didnt exist? but that cant be, because the window, even if not on the screen, should still have a device context, which i was writing to.

    any idea why that didnt work then but does work now? thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Feedback: Functional Specification Wording
    By Ragsdale85 in forum C++ Programming
    Replies: 0
    Last Post: 01-18-2006, 04:56 PM
  2. char copy
    By variable in forum C Programming
    Replies: 8
    Last Post: 02-06-2005, 10:18 PM
  3. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 02:41 AM
  4. OpenGL -- Bitmaps
    By HQSneaker in forum Game Programming
    Replies: 14
    Last Post: 09-06-2004, 04:04 PM
  5. Simple Ddraw question. Display bitmap on a screen.
    By tegwin in forum Game Programming
    Replies: 0
    Last Post: 05-22-2004, 05:50 PM