Solved

Visual C++ beginner

Posted on 1998-06-20
7
1,085 Views
Last Modified: 2006-11-17
Hi
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;
   bmpHello.loadBitmap(IDB_HELLO);

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

   CDC dcMem;
   dcMem.CreateCompatibleDC(pDC);

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

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
dcMem.SelectObject(pbmpOld);
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.
Thanks.

   
0
Comment
Question by:simi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
7 Comments
 
LVL 6

Expert Comment

by:WxW
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 .
0
 
LVL 6

Accepted Solution

by:
WxW earned 50 total points
ID: 1166348
The previous was an answer
0
 
LVL 6

Expert Comment

by:WxW
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:simi
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
dcMem.SelectObject(pbmpOld);
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!
Thanks
0
 
LVL 6

Expert Comment

by:WxW
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;

      SelectObject(TheDC,hBitmap);

      hMemDC = CreateCompatibleDC(TheDC);
      if (hMemDC == NULL)
            {
            DeleteObject(hBitmap);
            return 2;
            }

      SelectObject(hMemDC,hBitmap);
      BitBlt(TheDC,x,y,w,h,hMemDC,0,0,SRCCOPY);

      DeleteObject(hBitmap);
      DeleteDC(hMemDC);
      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
0
 
LVL 6

Expert Comment

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

Expert Comment

by:nietod
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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Building cUrl in Windows v7.43.0 4 39
Interoperability issue between VC++ (ocx) & VB.NET applications 7 111
FMX enumerated colours 2 133
boost::uuid crashes 17 38
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

756 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