Go Premium for a chance to win a PS4. Enter to Win

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1091
  • Last Modified:

Visual C++ beginner

I am learning Ms Visual C++
I have the following problem

Theo following example is regarding loading and displaying a bitmap.

void CBitmapView::OnDraw(CDC* pDC)
   CBitmap bmpHello;

   BITMAP bm;
   bmpHello.GetObject(sizeof(BITMAP), &bm);

   CDC dcMem;

  CBitmap* pbmpOld = dcMem.SelectObject(&bmpHello);
  pDC->BitBlt(10,10,bm.bmWidth,bm.bmHeight,&dcMem,               0,0,SRCCOPY)

I think I understand most of it.
Still I do not get exactly the use of SelectObject function.
I read about it in the MFC, and it says that the "
CPen* SelectObject( CPen* pPen );

SelectObject function returns a pointer to the object being replaced....For example, if pObject of the general version of SelectObject points to a CPen object, the function replaces the current pen with the pen specified by pObject.

I am confused.
What is the current pen ?(or bitmap). Is there any special meaning for such a "current pen", or current bitmap ?

In my example.
I understand that the first time, I declare a pointer to a CBitmap class, this pointer is pbmpOld.
By assigning to pbmpOld the value returned by the dcMem.SelectOjbect(&bmpHello) function, I make the pbmOlod pointer to point to the bmpHello object wich is an object of  CBitmap class.
after that, I use the BitBlt function to copy the content of the pbmpOld pointed object(or I could say the content of the bmpHello object), from the DC to the deviced specified by the pDC pointer.
Untill here I did not make any use of the pbmpOld.
Here I use
I do not understand the meaning of using this function.
The explination in the book is, " Reselect the original bitmap into the memory DC.".

Hope someone can help.

  • 5
1 Solution
SelectObject() enables you to choose between multiple brushes / pens etc

For example , assume you want to draw two rectangles to a DC you have , with a different brush color ( the brush is used to fill the rectangle ) .

If you had not SelectObject , you would be limited to select only one brush , draw the rectangle , then release and re-get the DC to select the other brush to draw the other rectangle . All handlers to objects like pens , brushes , fonts and others , can be used to multiple DC's , not just one . Thats why you need SelectObject() : To specify for which DC you need the brush/pen etc .
The previous was an answer
The "Current" pen/brush/font , etc , is the one you have selected with SelectObject AND it will be used for drawing , until you release the DC , discard the object with DeleteObject , or select another object using SelectObject() again
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

simiAuthor Commented:
I understand that I select the bmpHello object and I make the pbmpOld pointer point to it ,with
CBitmap* pbmpOld = dcmem.SelectObject(&bmpHello);
I do not get what I do with
The explination in the book is that I "Reselect the original bitmap into the memory  DC", but if the first time I had selected the only CBitmap object that I had declared, more preciselly bmpHello, what am I selecting the second time ?
I select the adress of the pbmpOld pointer that points to the same object isn't it ?
This is where I get confused.
Can you please clarify!
BitBlt copies the bitmap from a MEMORY DC to a HDC ( to screen for example ) . Thats why the code first selects the bitmap to the memory DC and then copies it to screen with BitBlt . I suspect that calling BitBlt destroys the original MEMORY DC copy of the bitmap , thats why the code re-selects it . I think that if you do not need to do anyother with that bitmap , except displaying it , you do not need to re-select it to the Memory DC

a MEMORY DC is a handle to a DC in memory , which you "virtually" draw the images , and then copy them to the original DC using a bitmap function like BitBlt . When I want to display a bitmap , I use the following code :

int DrawBitmap(HDC TheDC,HINSTANCE TheInstance,LPCTSTR TheBitmap,int x = 0,int y = 0,int w = 200,int h = 200)
      HBITMAP hBitmap;
      HDC        hMemDC;

      hBitmap = LoadBitmap(TheInstance,TheBitmap);
      if (hBitmap == NULL)
            return 1;


      hMemDC = CreateCompatibleDC(TheDC);
      if (hMemDC == NULL)
            return 2;


      return 0;

This takes : The DC to draw the bitmap , the HINSTANCE of the application and the resource string for the bitmap to load it ,and the bitmap X-Y-W-H
Of course , that function uses the Windows API - Similar to the MFC emulation , but not exactly the same .
>>The explination in the book is that I "Reselect the original bitmap into the memory  DC", but if the first time I had selected the only CBitmap object that I had declared, more preciselly bmpHello, what am I selecting the second time ?

Whenever you select in an item, SellectObject returns a pointer to the previously used item of the same type.  Thus when you select in a brush, it returns a handle to the brush it had been using.  When you select in a pen, it returns a handle to a pen it had been using.  Same with bitnmaps etc.

There are two important rules however.  1) You may not delete an object that is still selected into a DC.  2) You must delete all the objects you create.  

That means if you create a brush and select it into a DC, you will need to delete the brush as when you are done with it.  but it is still selected into a DC so you can't delete it.  To get it out of the DC, you must select back the brush that was originally in the DC.  Then you can safely delete the brush.

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now