Solved

Visual C++ beginner

Posted on 1998-06-20
7
1,081 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now