Functions For Helping FillRect

William Poetra Yoga H williampoetra at yahoo.com
Sun Oct 24 04:20:53 CDT 2004


FillRect is used in many places for drawing the GUI. But one thing I think many
developers miss is that it doesn't fill the right and bottom borders of the
rectangle, very much like LineTo doesn't draw the destination point (as per the
MSDN docs).

But when handling RECT structures we usually think about the objects, not how
the objects are going to be painted on-screen. For example, if we wanted to
draw a red rectangle at (10,10) with width of 50 pixels and height of 75
pixels, we do (assuming hdc and hbr is already set correctly):

RECT r;
r.left = 10;
r.top = 10;
r.right = r.left + 50;
r.bottom = r.top + 75;
FillRect(hdc, &r, hbr);

This _will_ draw it all right, but there are conceptual errors:
1. The rectangle that we have here is not 50x75, but actually 51x76. But
FillRect doesn't fill the right and bottom borders, so it appears correctly.
2. If we thought that FillRect fills the whole rectangle, then actually the
above code doesn't do what we want; it should actually draw a 51x76 rectangle.

So I think we have to make helper functions:

RECT ToFill(RECT r)
{
  ++r.right;
  ++r.bottom;

  return r;
}

RECT ToFillLP(LPRECT lpr)
{
  RECT r = *lpr;

  return ToFill(r);
}

LPRECT LPToFill(RECT r)
{
  ++r.right;
  ++r.bottom;

  return &r;
}

LPRECT LPToFillLP(LPRECT lpr)
{
  RECT r = *lpr;

  return LPToFill(r);
}

So that:
1. Drawing with FillRect will be correct.
2. We can forget about FillRect's peculiar way of doing things.
3. We can concentrate on the actual rectangles.

So instead of the above code, we do:

RECT r;
r.left = 10;
r.top = 10;
r.right = r.left + 50 - 1;
r.bottom = r.top + 75 - 1;
FillRect(hdc, LPToFill(r), hbr);

What do you all think?

Anyway, for LineTo we can just add "+ 1" at the arguments, no need for a helper
function.



		
__________________________________
Do you Yahoo!?
Take Yahoo! Mail with you! Get it on your mobile phone.
http://mobile.yahoo.com/maildemo 



More information about the wine-devel mailing list