Why isn't my CPen working right?

Posted on 2004-11-15
Last Modified: 2013-11-20
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;

      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.


Question by:PMH4514
    LVL 43

    Accepted Solution

    Try these lines instead

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

    LVL 2

    Expert Comment

    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;
    pDC->Rectangle( 10,10,40,40 );


    Author Comment

    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!

    LVL 43

    Expert Comment

    My comment should have identified and solved your problem.

    Author Comment

    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?

    LVL 43

    Expert Comment

    I suspect your hDC you used initially was not the hDC of the pDC.  Does that make sense?
    LVL 2

    Expert Comment

    The DC has two:


    But afaik you are safe using pDC-> instead. The dc will lead your command to the right handle..

    Author Comment

    Gotcha.  Thanks!


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
    Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    761 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

    8 Experts available now in Live!

    Get 1:1 Help Now