Thread: CreateCompatibleBitmap & CreateCompatibleDC

  1. #1
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718

    CreateCompatibleBitmap & CreateCompatibleDC

    Would this:
    Code:
    HDC hDC = GetDC( NULL );
    HDC hDC2 = CreateCompatibleDC( hDC );
    HBITMAP hBM = CreateCompatibleBitmap( hDC, 400, 400 );
    SelectObject( hDC2, hBM );
    have the same effect as this:
    Code:
    HDC hDC = GetDC( NULL );
    HDC hDC2 = CreateCompatibleDC( hDC );
    HBITMAP hBM = CreateCompatibleBitmap( hDC2, 400, 400 );
    SelectObject( hDC2, hBM );
    Last edited by XSquared; 08-20-2003 at 07:37 PM.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    No they are not the same. Your second method will produce a black and white image.

    Edit: Shouldn't you be passing a width and height to your CreateCompatibleBitmap call?
    Last edited by MrWizard; 08-20-2003 at 07:23 PM.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Good point. That's not copied from my code. Heh.

    >>No they are not the same. Your second method will produce a black and white image.
    What about this:
    Code:
    // g_hWnd being the handle to my app's window
    HDC hDC = GetDC( g_hWnd );
    HDC hDC2 = CreateCompatibleDC( hDC );
    HBITMAP hBM = CreateCompatibleBitmap( hDC2, 400, 400 );
    SelectObject( hDC2, hBM );
    Also, if I was to use the HBITMAP as a global variable, would it be OK to keep a global HDC that has the HBITMAP selected open from the very start of the program to the end of it, or would it be better to only get an HDC whenever I plan on doing operations on the bitmap?
    Last edited by XSquared; 08-20-2003 at 07:40 PM.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by XSquared
    Good point. That's not copied from my code. Heh.

    >>No they are not the same. Your second method will produce a black and white image.
    What about this:
    Code:
    // g_hWnd being the handle to my app's window
    HDC hDC = GetDC( g_hWnd );
    HDC hDC2 = CreateCompatibleDC( hDC );
    HBITMAP hBM = CreateCompatibleBitmap( hDC2, 400, 400 );
    SelectObject( hDC2, hBM );
    Also, if I was to use the HBITMAP as a global variable, would it be OK to keep a global HDC that has the HBITMAP selected open from the very start of the program to the end of it, or would it be better to only get an HDC whenever I plan on doing operations on the bitmap?
    I didn't see that you were using GetDC( 0 ) in your first post. That would retrieve the DC for the desktop which is probably not what you intended. So the only difference with your new code is that you use your windows hwnd. That's what I thought you did when I responded first.

    The global variables aren't that big of deal. Are you going to be using it a lot? If so just keep it global.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    So if using GetDC on my window's handle and then creating a compatible bitmap makes one in black and white, how would I go about creating a colour one?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by XSquared
    So if using GetDC on my window's handle and then creating a compatible bitmap makes one in black and white, how would I go about creating a colour one?
    Sorry maybe I was a little vague. You use GetDC with your window's handle and create a compatible bitmap with your window's dc NOT the compatible DC. That's the only thing you need to do.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Cool. Works fine now. But why would it create a black and white bitmap when I use the compatible DC?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >> But why would it create a black and white bitmap when I use the compatible DC?

    Because the compatible DC is created with a 1 x 1 black and white bmp in it.

    the other is a copy of the one used by that HWND.

    don't forget to ReleaseDC() the one you GetDC() 'ed.

    don't forget to return the DC's back to the original condition (by SelectObject() all original GDI objects, pens, bmps ect) before you try to DeleteDC() it.

    don't forget to DeleteObject() any GDI objects you create ie pens, brushes and bmp's. Most GDI objects will not delete if currently selected into a DC.

    As to globals I usually have two global DC's, the original bmp's and their created bmp's. I keep these in a class or structure. Have an array, one element for each window or control, so you can write generic functions passing the correct element of the array to init and destroy these items.
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CreateCompatibleDC() vs RestoreDC
    By csonx_p in forum Windows Programming
    Replies: 3
    Last Post: 04-14-2008, 01:52 AM
  2. MFC, CreateCompatibleDC question.
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 05-13-2003, 12:29 AM