Re-draw Control

A

--== Alain ==--

Hi,

I have a question about drawing windowed control.

For example, i have a windowed control, let's called it C1 (as container
for example).
its width = 300 px and height = 200 px.

C1 displays some other windowed controls, however, not all are displayed
initially when C1 is painted. for example C2 can be located on point
500,300 (x,y).

therefore, C2 is not displayed initially,

I have 4 buttons which allow me to scroll from right to left or bottom
to top the "canvas" (display surface) of C1.

However, i would like to understand how i can shift this canvas based on
button click ?

I mean if i click on "Right Button" 10 times, the drawing region of C1
should be re-draw to reflect this shift. So, C1 should be repaint to
display the region : 10,0,300,200 (x,y,width,height).

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.

thx,

Al.
 
P

Peter Duniho

[...]
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top