Quote:
Originally Posted by
ttrz
First of all, when will iPnBeg be less than 0 in this case. Here cyChar is initialized to tm.tmHeight + tm.tmExternalLeading. It will never be zero.
The value of ps.rcPaint.top represents the start of the invalid rectangle (y coord). The lowest value it can have is 0. (Which would mean the invalid rectangle starts at the very top of the client area).
ps.rcPaint.top could never be negative, and cyChar could never be negative. Therefore the expression ps.rcPaint.top / cyChar could never be negative.
Then we add iVert. The range for most standard scroll bars I have come across begin at 0. So, the lowest value that iVert could possibly have is 0.
So, the first question is why do we need max here, if the lowest possible value the expression can evaluate to is 0?
Is any of those variables are signed integers, then regardless of what you're stating, it is possible to have negative numbers. To prevent this from happening, the max() is used to enforce that the smallest value is 0. If all of those variables involved are unsigned, then of course you can't get a negative number. That is the
Quote:
Now, forgetting the max part of things, I am trying to understand the logic behind it. rcPaint.top / cyChar seems like another way of saying "How many rows of characters fit from the top of the client area to the top of the invalid rectangle?".
For this example, let's say that cyChar = 5, there are 75 lines to print (NUMLINES), and the page size is 50. The scroll bar ranges from 0 (showing lines 0 through 49) to 25 (showing lines 25 through 74).
Let's say the scroll bar is currently at position 20, meaning it is showing lines 20 though 69.
Let's also say the top of the invalid rectangle is (y coord) 10 and the bottom of the invalid rectangle (y coord) is 20.
So we have:
iPnBeg = (20 + 10 / 5) = 22
Now looking at iPnEnd, it is the smaller value between 74 and (20 + 20 / 5). That is, 74 and 24. So it will be 24.
So, in this case the loop will be:
for(i = 22; i <= 24; i++)
This is the only place where iPnBeg and iPnEnd are used. Why does it go through the loop 3 times, when there is only room to print 2 rows?
Why can't we do:
int y = (bottom - top) / cyChar; //evaluates to 2 in this example
for(int i = 0; i < y; i++)
//print the 2 rows
Sorry if I messed up anything fundamental in my explanation there. I've been staring at this for a while and I think my brain stopped working.
Use your debugger -- that's what it's there for, so that you understand why things work the way they work. If you're using Visual Studio, you can change those loop limits in the debugger to see how your program behaves.