Win32 Programming (Drawing a Bitmap)

I want to draw a Bitmap on Screan not using MFC
(I use visual C++ 5.0 )

So hire is my problem: I want to load a BMP and show it on my screan, hire is my code:

void DrawBitmap (HDC hdc, HBITMAP hBitmap, int xStart, int yStart)
  BITMAP bm ;
  HDC hdcMem ;
  DWORD dwSize ;
  POINT ptSize, ptOrg ;

  hdcMem = CreateCompatibleDC (hdc) ;
  SelectObject (hdcMem, hBitmap) ;
  SetMapMode (hdcMem, GetMapMode(hdc)) ;

  GetObject (hBitmap, sizeof (BITMAP), (LPVOID) & bm) ;

  ptSize.x = bm.bmWidth ;
  ptSize.y = bm.bmHeight ;
  DPtoLP (hdc, &ptSize, 1) ;

  ptOrg.x = 0 ;
  ptOrg.y = 0 ;
  DPtoLP (hdcMem, &ptOrg, 1) ;

  BitBlt (hdc, 1, 1, ptSize.x, ptSize.y, hdcMem, ptOrg.x, ptOrg.y,
  DeleteDC (hdcMem) ;


I then try to Draw it in WinProc:
      case WM_CREATE :                        
                        /*hwndEdit = CreateWindow ("edit", NULL, WS_CHILD | WS_VISIBLE | WS_OVERLAPPEDWINDOW  |
                                                                  WS_HSCROLL | WS_VSCROLL | ES_LEFT | ES_MULTILINE |
                                                                  ES_AUTOHSCROLL | ES_AUTOVSCROLL, 10, 10, 15, 15, hwnd, (HMENU) EDITID,
                                                                  ((LPCREATESTRUCT) lParam) -> hInstance, NULL) ;-*/
                        if(hBitmap = CreateBitmap (200,200,1,1,BMPpoint)) ;
                         else MessageBox (hwnd, "NO BMP","NONE", MB_ICONEXCLAMATION | MB_OK) ;    
                        //SetWindowText (hwndEdit, "Text Document") ;            
                        hdc2     = CreateDC ("DISPLAY", NULL, NULL, NULL) ;
                        DrawBitmap (hdc2, hBitmap, 10, 10) ;      
                        DeleteDC(hdc2) ;
                        //BitBlt (hdc, 0, 0, 100, 100, hdc, 10, 20, BLACKNESS ) ;
                        DrawBitmap (hdc2, hBitmap, 10, 10) ;      
Anyhow it dos't work !!

What im I doing wrong, ?? My hBitmap dos't work welll, and I can't see why !!
Who is Participating?

Instead of using the WM_CREATE to draw the bitmap, use the WM_PAINT, where all the window painting must be done, and don't create the DC that way, use the given DC to do it, also you have to delete the bitmap object, like this:

static HBITMAP hBitmap;

  hBitmap = CreateBitmap(...);

case WM_PAINT:
  BeginPaint(hWindow, &ps);
  DrawBitmap(ps.hdc, hBitmap, 0, 0);
  EndPaint(hWindow, &ps);


Best Regards,

Wenderson Teixeira
Your DrawBitmap() function als has some serious memory leaks.  When you select an object into a DC, the previous one is selected out.  When you are "given" a DC it has a buncyh of default objects (pens, brushes, birtmaps), you must make sure that when you are donw with the DC that these original objets are ale selected back.  They must be selected back before your destroy the DC.  ALso when you create an object, like a pen or bitmap, you must delete it.  Again, you must make sure that object is selected out of the DC before the object is deleted.
win32Author Commented:
Thats a start Yes! But the real problem is that I can't get the CreateBitmap(..) function to work right !. When running the program one pixel is displayed !!??
>>>Hire is the Code<<<<
tatic HBITMAP                   hBitmap;      //Handel to Draw static int                  BMP[200][200];
static char *BMPpoint=BMP;

for(int i=0;i<200;i++)      
 for(int ii=0;ii<200;ii++)      
hBitmap = CreateBitmap (200,200,16,8,&BMPpoint) ;
// I want 8bit per pixel ...

I think that i might not use the CreateBitmap function right !

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

One problem is that you are setting the color planes to 16 (3rd parameter).  Set it to 1.  I don't think any device supports 16 color planes.  In fact, I don't think any used today support more than 1.  
win32Author Commented:
1) Your DrawBitmap() function als....(from nietod)
    Yes I can see that's a problem! How do I solve problem ?,   what api's would I use ?

2) Ok I got my program to work win 32bit,16 colors, using WORD and DWORD to make the bitmapdata !. But what will I do if I want to use 24 bit display ??. A simple datatype like WORD24 dos not exist !. How do I solve that problem ??

3)Is it possible to make modifikations to the Palette like this:
SetPalRGB(int R, int G, int B, PalNr); in a 16,24,and 32 bit display enviroment ?

Thans for all the helpe that I have recived ! :-)
1) FYI I have no idea how that "als" got in there.

Take a look at SelectObject().  It returns an object of the type selected in.  If you select in one brush, it returns the brush that was previously in use.  Before you destroy your brush, you must get it out of the DC.  How are you going to get it out?  By selecting back in the brush that was  originaly there.  That also solves the other problem, the DC must have its original objects, before it is destroyed, so this returns its originial object, the brush, to it.  You must do this any time you use SelectObject(), even with bitmaps.  

2) work with arrays of bytes, like char.  You can figure out how many character to allocate based on the dimensions of the bitmap and the number of bits per pixel.  Like

new char[Width*Height*BitsPerPixel/8];

(Actually, the calculation is probably a bit more complex than that, because I beleive there may be restrictions on the row length, like each row of pixels must be even that sort of thing.)

3) Look at SetPaletteEntries().
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.