Why isn't my CPen working right?

This code draws a black rectangle with 1px lines on my DC, it should be drawing a 3px red rectangle. what's wrong?

      COLORREF penColor = RGB(255,0,0);
      CPen penRed;
      penRed.CreatePen(PS_SOLID, 3, penColor );
      CPen* pOldPen = (CPen*)SelectObject(hDC, &penRed);

      // create a null brush for un-filled rectange.
       CBrush pNullBrush;
       pNullBrush.CreateStockObject(NULL_BRUSH);

      CBrush * pOldBrush = (CBrush *)SelectObject(hDC, pNullBrush);

      RECT* rpBoxRect = new RECT;      // rectangle
      rpBoxRect->left = iLeft;      // temp hard-coded.. derive from INI map_size and window size values.
      rpBoxRect->top = iTop;      
      rpBoxRect->right = iRight;
      rpBoxRect->bottom = iBottom;

      // draw rectangle overlay.
      pDC->Rectangle(rpBoxRect);


???

thanks!
-Paul
PMH4514Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
Try these lines instead

CPen* pOldPen = (CPen*)pDC->SelectObject(&penRed);
CBrush * pOldBrush = (CBrush *)pDC->SelectObject(pNullBrush);


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RuskialtCommented:
The brush is used for "filling" the rectangle, while the pen is used for the frame.

To draw a red rectangle simply call

pDC->FillSolidRect( nLeft , nTop , nWidth , nHeight , RGB(255,0,0) );

You don't need to create no null brush, it's ready for you already in the dc

pDC->SelectStockObject( NULL_BRUSH );

Are you in MM_TEXT mode, use pDC->SetMapMode() to try it out. Are you sure you have got the right hDC, the device has two! Try GetSafeHdc() on the device context, or use the dc pointer instead like both Andy and I already did.

This worked for me:

CPen pp;
pp.CreatePen(PS_SOLID,3,RGB(255,0,0));
pDC->SelectObject(&pp);
pDC->SelectStockObject(NULL_BRUSH);
pDC->Rectangle( 10,10,40,40 );

0
PMH4514Author Commented:
I think maybe you misunderstood. I want to draw an unfilled rectangle (ie. the NULL_BRUSH) - drawn out of red lines. that is, an empty red box.

 I'll try your code chunk when I get to the office. thanks!

-paul
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

AndyAinscowFreelance programmer / ConsultantCommented:
My comment should have identified and solved your problem.
0
PMH4514Author Commented:
AndyAinscow - yup, I actually just noticed your comment.. When I responded above, I had only seen the one from Ruskialt, I must have scrolled past it..

trying it right now...

beautiful.. it worked!

Could you kindly explain why mine didn't work?

Thanks!
-Paul
0
AndyAinscowFreelance programmer / ConsultantCommented:
I suspect your hDC you used initially was not the hDC of the pDC.  Does that make sense?
0
RuskialtCommented:
The DC has two:

m_hDC
m_hAttribDC

But afaik you are safe using pDC-> instead. The dc will lead your command to the right handle..
0
PMH4514Author Commented:
Gotcha.  Thanks!

-Paul
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.