• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1113
  • Last Modified:

How to get a transparent rectangle in a view

Hiya

I have an MFC SDI application. In the View I want to draw a rectangle with a black border. However, I want the inside of the rectangle to be empty i.e. transparent. That is, I want to be able to put the rectangle over other shapes in the View and they will appear through the rectangle.

I have tried it so far using

pDC->SetBkMode(TRANSPARENT);
pDC->Rectangle(100,-100,400,-400);

and nothing covered in the rectangle comes through.

John
0
IssacJones
Asked:
IssacJones
  • 2
1 Solution
 
ZoppoCommented:
Hi IssacJones,

'Rectangle' uses the current selected brush to fill the rectangle. To draw a transparent one do it like this:

> CBrush* pOldBrush = (CBrush*)pDC->SelectStockObject( NULL_BRUSH );
> pDC->Rectangle( 100,-100,400,-400 );
> pDC->SelectObject( pOldBrush );

Hope that helps,

ZOPPO
0
 
IssacJonesAuthor Commented:
Hi Zoppo

Nope, that doesn't work. The rectangle is complely black.

By the way, the rectangle covers a bitmap. Would that make a difference?

John
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
You could use a combination of MoveTo (moves cursor to a point without drawing a line) and DrawTo (draws line from where the cursor was up to  the new point).

The advantage of this technique is that it copes with any shape not just rectangles.
0
 
IssacJonesAuthor Commented:
Hiya Zoppo

Sorry you were right, I didn't use SelectStockObject.

now that I have it works ok.

John
0
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.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now