Solved

Visual C++ beginner

Posted on 1998-06-20
7
1,080 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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. …
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 how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

706 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

15 Experts available now in Live!

Get 1:1 Help Now