Visual C++ beginner

Posted on 1998-06-20
Last Modified: 2006-11-17
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.

Question by:simi
  • 5

Expert Comment

ID: 1166347
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 .

Accepted Solution

WxW earned 50 total points
ID: 1166348
The previous was an answer

Expert Comment

ID: 1166349
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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Author Comment

ID: 1166350
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!

Expert Comment

ID: 1166351
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

Expert Comment

ID: 1166352
Of course , that function uses the Windows API - Similar to the MFC emulation , but not exactly the same .
LVL 22

Expert Comment

ID: 1166353
>>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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question