Drawing over child controls

G

Guest

Hello,

I am trying to get a form to draw arrows linking 2 list boxes to each other.
I can write the code to draw the arrows / make them follow the boxes around
when they are dragged around the form, however, the form always draws the
arrow under the child controls.
I was wondering if anyone could tell me how I would make the arrows draw
over the list boxes, instead of pass under them?

The only idea I had was to call base.onPaint first, then draw the arrows,
but this does not work. [That may sound basic, but I haven't had the need to
do anything like this before, so I don't know much about it].
Any help would be greatly appreciated.

Thanks
 
C

Chris Jobson

R2D2 said:
Hello,

I am trying to get a form to draw arrows linking 2 list boxes to each
other.
I can write the code to draw the arrows / make them follow the boxes
around
when they are dragged around the form, however, the form always draws the
arrow under the child controls.
I was wondering if anyone could tell me how I would make the arrows draw
over the list boxes, instead of pass under them?

The only idea I had was to call base.onPaint first, then draw the arrows,
but this does not work. [That may sound basic, but I haven't had the need
to
do anything like this before, so I don't know much about it].
Any help would be greatly appreciated.

Thanks

I can think of two ways to do this (but I don't recommend either of them,
and I'm sure someone else can come up with something better).

1. Use a transparent panel as the topmost control on the form and draw the
lines on this panel. There's an article on creating a transparent panel at
http://www.codeproject.com/useritems/TrulyTransPanel.asp. The snag with this
is that clicking on a control selects the transparent panel rather than the
control you clicked on. I think I've seen somewhere another version of
atransparent panel that gets round this (rather like the forms designer in
VS), but I'm not sure where.

2. Remove the WS_CLIPCHILDREN style from the form so that the form is
allowed to draw over the controls on it. This can be done as follows:

const int WS_CLIPCHILDREN = 0x02000000;
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.Style &= ~WS_CLIPCHILDREN;
return cp;
}
}

Unfortunately there is still a problem because when the form is drawn it
seems to do its own painting first and then paint the controls. The way
round this I've found is to fire a 1ms timer in the paint event, then draw
the lines in the timer event, e.g.

private void Form1_Paint(object sender, PaintEventArgs e)
{
timer1.Enabled = true; // timer1.Interval = 1
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
Graphics g = CreateGraphics();
g.DrawLine(Pens.Black, 0, 0, ClientSize.Width, ClientSize.Height);
}

However, I suspect there's a better way round this problem - any ideas
anyone?

Chris Jobson
 

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