Hi
Thanks for your answer.
I am trying to do the following:
I have a form (IsMdiContainer = true).
On this Form I place a label, for example "Version 1.0".
What I now want is: When I open a child form and when
I move this over the Label, the Label should be behind this
Child form (not visible any more).
Now when I move the child form over the label,
the text of this label appears to be on the child form ...
Thanks for your help.
Best regards
Frank
- Show quoted text -
Frank,
My first impulse was to suggest that if your goal was to make the mdi
parent's Label contents visible regardless of the location of child
windows, perhaps you can put the Label's contents on a StatusStrip on
the parent form.
I also thought about what you'd have to do to get your idea (as I
understand it) to work. You'd basically have to perform some hit
testing to determine if a child form is over the Label on the mdi
parent. If so, you'd set the Visible property on your parent form
Label to false, and you'd set a similar Label on the child to true.
I tried a quick experiment and it appeared to work. What I did was
create a child form with an event that contains the form's location
and size in its parameters. This event fires on the form's
LocationChanged and ResizeEnd events. The mdi parent subscribes to
these events and each time they are raised, the parent sees if the
moved child form overlaps the parent's label. If so parent's Label's
Visible property is set to false and the child form's Label is set to
true, through a method on the child form.
Here's what I did:
1. Create a new project and create a mdi parent form.
2. Add a Label to this form. I called mine label1.
3. Add a child form to your project and add a label to it. I called
this form Child.cs and the Label label1.
4. On the Child form, I added this code:
internal delegate void MdiChildMovedHandler(object sender, Point
location, Size size);
internal event MdiChildMovedHandler MdiChildMoved;
private void Child_LocationChanged(object sender, EventArgs e) {
if (MdiChildMoved != null) {
MdiChildMoved(this, this.Location, this.Size);
}
}
internal void SetLabelVisible(bool setting) {
label1.Visible = setting;
}
private void Child_ResizeEnd(object sender, EventArgs e) {
if (MdiChildMoved != null) {
MdiChildMoved(this, this.Location, this.Size);
}
}
5. Add the following code to the parent form:
private void MdiChildMoved(object sender, Point location, Size size) {
//Console.WriteLine("ID: {4} -- x:{0}, y:{1}, w:{2}, h:{3}",
location.X, location.Y, size.Width, size.Height,
((Child)sender).Name);
Rectangle formRectangle = new Rectangle(location, size);
Rectangle labelRectangle = new Rectangle(label1.Location,
label1.Size);
label1.Visible = !LabelInsideChild(formRectangle, labelRectangle);
((Child)sender).SetLabelVisible(LabelInsideChild(formRectangle,
labelRectangle));
}
// Return true if the label rectangle is completely contained in the
child form's rectangle.
private bool LabelInsideChild(Rectangle labelRectangle, Rectangle
childFormRectangle) {
Point formTopLeftPoint = new Point(childFormRectangle.Left,
childFormRectangle.Top);
Point formTopRightPoint = new Point(childFormRectangle.Left +
childFormRectangle.Width, childFormRectangle.Top);
Point formBottomLeftPoint = new Point(childFormRectangle.Left,
childFormRectangle.Top + childFormRectangle.Height);
Point formBottomRightPoint = new Point(childFormRectangle.Left +
childFormRectangle.Width, childFormRectangle.Top +
childFormRectangle.Height);
// Check Top Left, Top Right, Bottom Left, Bottom Right.
Console.WriteLine(" TL: {0} [x:{1},y:{2}]",
PointInsideRectangle(labelRectangle, formTopLeftPoint),
childFormRectangle.Top, childFormRectangle.Left);
Console.WriteLine(" TR: {0} [x:{1},y:{2}]",
PointInsideRectangle(labelRectangle, formTopRightPoint),
childFormRectangle.Top, childFormRectangle.Left +
childFormRectangle.Width);
Console.WriteLine(" BL: {0} [x:{1},y:{2}]",
PointInsideRectangle(labelRectangle, formBottomLeftPoint),
childFormRectangle.Top + childFormRectangle.Height,
childFormRectangle.Left);
Console.WriteLine(" BR: {0} [x:{1},y:{2}]",
PointInsideRectangle(labelRectangle, formBottomRightPoint),
childFormRectangle.Top + childFormRectangle.Height,
childFormRectangle.Left + childFormRectangle.Width);
Console.WriteLine("======================");
return PointInsideRectangle(labelRectangle, formTopLeftPoint)
&& PointInsideRectangle(labelRectangle, formTopRightPoint)
&& PointInsideRectangle(labelRectangle, formBottomLeftPoint)
&& PointInsideRectangle(labelRectangle,
formBottomRightPoint);
}
// Return true if the Point is inside the Rectangle.
private bool PointInsideRectangle(Rectangle r, Point p) {
return (r.X < p.X) && (p.X < (r.X + r.Width)) && (r.Y < p.Y) && (p.Y
< (r.Y + r.Height));
}
Your child forms would subscribe to the MdiChildMoved event
Give this a try. It worked for me but you'll want to test and refine
things a bit. I tried to format my code so it wouldn't be too
difficult for you to copy and paste it. If you want I can email you
the project file (VS 2005).
-Jay