On Sun, 11 Feb 2007 08:11:17 -0800, --== Alain ==-- <no****@noemail.com
wrote:
[...]
I suppose that i have to use the paint event with clip, cliprectangle
and bound. Moreover, i should compare repaint window surface (clipped
region) to something...but to what ?
I would be very glad if i can get some tutorial or example on similar
topics. It will help me in my understanding.
You really only need two things:
* to keep track of the offset related to scrolling
* UI to change that offset
You already have the buttons, though typically an application would use
the built-in scroll bars. If you look up ScrollableControl, scroll bars,
etc. in the MSDN documentation you'll find a variety of examples and
references discussing the topic.
Generally speaking, in your example you do not need to worry about
clipping. Drawing should be clipped to the control already when the
control receives its paint message. So all you have to do is make sure
you offset your drawing by the correct amount when repainting the control,
and make sure that the control is repainted (invalidated) when any
scrolling occurs.
So in your example where you have a 300x200 control (call that the
parent), and a contained control at (500,300) (call that the child), you
won't see that child control unless you've scrolled the parent container
by at least 200 pixels horizontally and 100 pixels vertically.
There are a couple of different ways to handle the scrolling values, but
one of the most straightforward is to simply subract the scrolled amount
from all drawing coordinates before drawing. If you're dealing with
actual child controls, then you need to move all of the child controls by
the scrolled amount. Again, using your example, if your parent gets
scrolled by (200,100), then you subtract that from the child's nominal
position of (500,300) to get a new position of (300,200), placing it just
at the lower right corner of the parent. Obviously scrolling more will
move the child more and reveal more of it than just the one pixel.
Basically, you always make sure that the position of the child controls
reflects the current status of the scrolling UI, any parts that you paint
yourself explicitly you offset by the current status of the scrolling UI,
and any time that the scrolling state has changed, you just force a
redraw, which will cause all of the necessary repainting to occur (with
needed clipping).
As it happens, .NET has a container control that is appropriate if all
you're doing is containing other .NET controls. It will handle all the
scrolling and whatnot for you. I don't recall the exact name off the top
of my head, but I think it's something like "UserControl" or something.
If you're using Visual Studio, just look in the control toolbox (category
"Containers" I think) and you should be able to find it. It will handle
the UI for scrolling the container and everything for you.
Hope that helps...
Pete