Thread: DialogBox and BITMAPS

  1. #1
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92

    Question DialogBox and BITMAPS

    Hi

    Im trying to load a BMP onto a DialogBox, but it never displays...

    heres my code:

    In my resource.rc file i have this:

    Code:
    IDB_WALLS BITMAP "wall.bmp"
    
    IDD_TESTBMP DIALOGEX 225, 200, 486, 313
    STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
    CAPTION "Cell:"
    FONT 8, "MS Sans Serif"
    BEGIN
        DEFPUSHBUTTON   "OK",IDOK,429,7,50,14
        CONTROL         "",IDB_WALLS,"Static",SS_BITMAP,0,0,305,278
    END
    In my Resource.h file i have the following:

    Code:
    #define IDB_WALLS 501
    Code:
    case WM_INITDIALOG:
            {
               hDC = BeginPaint(hWnd, &Ps);
    
               walls = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_WALLS));
               if(walls == NULL)
                    MessageBox(hwnd, "Could not load Bitmap!", "Error", MB_OK | MB_ICONEXCLAMATION);
               break;
               
               MemDC = CreateCompatibleDC(hDC);
               SelectObject(MemDC, walls);
               //Works?
               BitBlt(hDC, 0, 0, 305, 278, MemDC, 0, 0, SRCCOPY);
               DeleteDC(MemDC);
    	       DeleteObject(walls);
    
               return TRUE;
            }
    Now the dimensions of the BMP are 305x278, and im unsure the BitBlt() function call has the correct parameters? Im sure its something like im drawing on the wrong portion of the DialogBox? Ive also checked the file is in the correct directory also, and im using DevC++ / WinXP

    Any help appreciated greatly !
    Last edited by ventolin; 07-26-2004 at 11:07 AM.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Firstly, do your drawing in your WM_PAINT handler and not WM_INITDIALOG as you seem to be doing. Secondly, don't delete a dc that hasn't been restored to its original state. Rewriting what you have to reflect these points:
    Code:
    case WM_INITDIALOG:
      {
      walls = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_WALLS));
      if(walls == NULL)
        MessageBox(hwnd, "Could not load Bitmap!", "Error", MB_OK | MB_ICONEXCLAMATION);
      return TRUE;
      }
    case WM_PAINT:
      {
      HBITMAP     hOrig;
      HDC         MemDC;
      PAINTSTRUCT ps;
    
      BeginPaint(hwnd,&ps);
        MemDC = CreateCompatibleDC(hDC);
        hOrig=(HBITMAP)SelectObject(MemDC,walls);
        BitBlt(ps.hdc, 0, 0, 305, 278, MemDC, 0, 0, SRCCOPY);
      EndPaint(hwnd,&ps);
      /*restore and clean up gdi resources*/
      SelectObject(MemDC,hOrig);
      DeleteDC(MemDC);
      return 0;
      }
    case WM_DESTROY:
      DeleteObject(walls); 
      return 0;
    }
    If you're unsure about parameters for api functions then check them up on msdn.

    edit: corrected BitBlt first parameter.
    Last edited by Ken Fitlike; 07-27-2004 at 01:51 PM.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92
    Tried that, it still doesnt load

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Sorry, I didn't use the return value from BeginPaint as you had in your original example as the first parameter to BitBlt; it's fixed now (the hdc parameter of the PAINTSTRUCT is the same as the dc handle returned by BeginPaint).

    If that still fails to work then check the return value from LoadBitmap and ensure it's non-NULL. If it is NULL then use GetLastError to try and get some sort of idea of why it's failing.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed