Files
KJD/TEMUKJD/Extension/ControlExtension.cs

63 lines
2.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TemuKJD.Extension
{
public static class ControlExtension
{
public static Point GetRelativePosition(this Control control, Form form)
{
Point location = control.Location;
Control parent = control.Parent;
while (parent != null && parent != form)
{
location.X += parent.Location.X;
location.Y += parent.Location.Y;
parent = parent.Parent;
}
return location;
}
public static Point GetRelativePosition(this Control control, Control parentControl)
{
if (control == null || parentControl == null)
return new Point(0, 0);
Point location = control.Location;
Control parent = control.Parent;
while (parent != null && parent != parentControl)
{
location.X += parent.Location.X;
location.Y += parent.Location.Y;
parent = parent.Parent;
}
return location;
}
public static Point GetRelativePosition(this Control control)
{
Point location = control.Location;
Control parent = control.Parent;
while (parent != null && !(parent is Form))
{
location.X += parent.Location.X;
location.Y += parent.Location.Y;
parent = parent.Parent;
}
return location;
}
public static Control GetParentNotTable(this Control contorl)
{
Control parent = contorl.Parent;
while (parent != null && (parent is TableLayoutPanel || parent is FlowLayoutPanel))
{
parent = parent.Parent;
}
return parent;
}
}
}