Creating custom container controls

J

Jay Dee

I have created a container that will position 4 panels that has 15
different layouts to choose from.

I have based it similar to a System.Windows.Forms.SplitContainer but
that has 4 panels instead of 2.

It douse not have the ability on fixing the splitter like the
SplitContainer class has, each splitter is based on a parentage of the
container. For what I wanted it for it did not need that capability so
I wasn’t too bothered.

I have also created the Designer class for the control Container and
the Containers panel class making it user friendly at design time.

I am very please with what I have achieved so far but I have become a
little stuck when I came to moving the splitters at run time with the
mouse.

What I aim to acheave is something like this

<code>

protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);

if (e.X > this.VerticalSplitter1Position -
(this.splitterThicknes / 2) &&
e.X < this.VerticalSplitter1Position +
(this.splitterThicknes / 2))
{
while (true)
{
// TO-DO
// Re-capture the state of the mouse.
e = new MouseEventArgs(); // TO-DO - Dont no how to re
capture the state of the mouse.

// If the left button is no longer pressed exit loop.
if (e.Button != MouseButtons.Left)
{
break;
}

this.VerticalSplitter1Position = e.X;

Application.DoEvents();
}
}
}

</code>

When the mouse is pressed while over one of the splitters it call the
OnMouseDown event.

Then it needs to wait until the user releases the mouse button before
re locating the splitter to the new position.

The problem I have is that I don’t know how to obtain the mouse
coordinates when the mouse leaves the area of the splitter when the
mouse moves over one of the containers panels within the container the
mouse events are no longer called.

Am I thinking completely wrong by thinking about a loop within the
OnMouseDown event and if so could anybody point me in the write
direction.

This is a link to my container control source code including a simple
form displaying the views that the container offers.

http://www.jdnd.co.uk/temp/QuodContainer.rar

Thank you all for any help and I hope this cold be useful to someone.
 
P

Pavel Minaev

I have created a container that will position 4 panels that has 15
different layouts to choose from.

I have based it similar to a System.Windows.Forms.SplitContainer but
that has 4 panels instead of 2.

It douse not have the ability on fixing the splitter like the
SplitContainer class has, each splitter is based on a parentage of the
container. For what I wanted it for it did not need that capability so
I wasn’t too bothered.

I have also created the Designer class for the control Container and
the Containers panel class making it user friendly at design time.

I am very please with what I have achieved so far but I have become a
little stuck when I came to moving the splitters at run time with the
mouse.

What I aim to acheave is something like this

<code>

protected override void OnMouseDown(MouseEventArgs e)
{
    base.OnMouseDown(e);

    if (e.X > this.VerticalSplitter1Position -
(this.splitterThicknes / 2) &&
        e.X < this.VerticalSplitter1Position +
(this.splitterThicknes / 2))
    {
        while (true)
        {
            // TO-DO
            // Re-capture the state of the mouse.
            e = new MouseEventArgs(); // TO-DO - Dont no how to re
capture the state of the mouse.

            // If the left button is no longer pressed exit loop.
            if (e.Button != MouseButtons.Left)
            {
                break;
            }

            this.VerticalSplitter1Position = e.X;

            Application.DoEvents();
        }
    }

}

</code>

When the mouse is pressed while over one of the splitters it call the
OnMouseDown event.

Then it needs to wait until the user releases the mouse button before
re locating the splitter to the new position.

The problem I have is that I don’t know how to obtain the mouse
coordinates when the mouse leaves the area of the splitter when the
mouse moves over one of the containers panels within the container the
mouse events are no longer called.

Am I thinking completely wrong by thinking about a loop within the
OnMouseDown event and if so could anybody point me in the write
direction.

Yes, you are. If you do that, you block the event loop from processing
new events. What you should do instead is set some bool flag in
MouseDown and return immediately, then provide appropriate handlers
for MouseMove (where you check for the flag and drag the splitter if
the flag is set), and MouseUp (where you reset the flag).

You do not need to do anything to receive MouseMove/MouseUp events
after you've handled MouseDown - WinForms automatically "captures" the
mouse in MouseDown. However, should you need to control this manually,
have a look at Control.Capture property.
 
J

Jay Dee

Fantastic, after reading your response it took me about 2 mints to
make it work.

Thank you

<code>

private bool verticalSplitter1Move;
private bool verticalSplitter2Move;
private bool horizontalSplitter1Move;
private bool horizontalSplitter2Move;

protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);

if (e.Button == MouseButtons.Left)
{
// Move VerticalSplitter1
if (e.X > this.VerticalSplitter1Position -
(this.splitterThicknes / 2) &&
e.X < this.VerticalSplitter1Position +
(this.splitterThicknes / 2))
{
this.verticalSplitter1Move = true;
}
// Move VerticalSplitter2
if (e.X > this.VerticalSplitter2Position -
(this.splitterThicknes / 2) &&
e.X < this.VerticalSplitter2Position +
(this.splitterThicknes / 2))
{
this.verticalSplitter2Move = true;
}
// Move HorizontalSplitter1
if (e.Y > this.HorizontalSplitter1Position -
(this.splitterThicknes / 2) &&
e.Y < this.HorizontalSplitter1Position +
(this.splitterThicknes / 2))
{
this.horizontalSplitter1Move = true;
}
// Move HorizontalSplitter2
if (e.Y > this.HorizontalSplitter2Position -
(this.splitterThicknes / 2) &&
e.Y < this.HorizontalSplitter2Position +
(this.splitterThicknes / 2))
{
this.horizontalSplitter2Move = true;
}
}
}

protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);

if (this.verticalSplitter1Move) this.VerticalSplitter1Position =
e.X;
if (this.verticalSplitter2Move) this.VerticalSplitter2Position =
e.X;
if (this.horizontalSplitter1Move) this.HorizontalSplitter1Position
= e.Y;
if (this.horizontalSplitter2Move) this.HorizontalSplitter2Position
= e.Y;
}

protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);

this.verticalSplitter1Move = false;
this.verticalSplitter2Move = false;
this.horizontalSplitter1Move = false;
this.horizontalSplitter2Move = false;
}

</code>
 

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