1

I am trying to draw the intersection of one blue rectangle and one yellow rectangle

 ,-------------------,
 |                   |
 |     Blue          |
 |           ,-------+---------,
 |           | Green |         |
 '-----------+-------, Yellow  |
             |_________________|

using the methods CDC::Polygon and CDC::SetBkMode(TRANSPARENT)

but all I get is this :

 ,-------------------,
 |                   |
 |     Blue          |
 |           ,-------+---------,
 |           |                 |
 '-----------+      Yellow     |
             |_________________|

Please give me a simple solution sticking with the MFC.

Thanks.

Michel Hua
  • 1,614
  • 2
  • 23
  • 44

1 Answers1

3

You cannot do this, regardless of whether SetBkMode is TRANSPARENT or OPAQUE since Polygon uses the currently selected brush to fill the polygon's interior. Instead what you should do is this:

Paint one rectangle first, paint the other rectangle next and then calculate the intersection of the two rectangles using CRect::IntersectRect (see http://msdn.microsoft.com/en-us/library/262w7389(v=vs.100).aspx).

If the intersection is non-empty, calculate the resulting "color blend" and create the appropriate brush and, using it, draw a third rectangle using.

For more information on how to blend the colors, check out Algorithm for Additive Color Mixing for RGB Values right here on StackOverflow.

Community
  • 1
  • 1
Nik Bougalis
  • 10,495
  • 1
  • 21
  • 37
  • I think you meant brush instead of pen, right? The pen is used for the outline and the brush is used for the inside. `SetBkMode` is only effective for text backgrounds. – Mark Ransom Nov 13 '12 at 15:51