111,094
社区成员




[ToolboxItem(true)]
public partial class UserControl1 : UserControl
{
Point _location = new Point(0, 0);
public UserControl1()
{
SetStyle(ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
InitializeComponent();
}
~UserControl1()
{
if (Parent != null)
RemoveBinding(Parent);
}
protected override void OnCreateControl()
{
base.OnCreateControl();
if (Parent != null)
AddBinding(Parent);
}
protected virtual void AddBinding(object parent)
{
if (parent is Control)
{
var cn = (Control)parent;
cn.LocationChanged += UserControl1_LocationChanged;
if (cn.Parent != null)
AddBinding(cn.Parent);
}
}
protected virtual void RemoveBinding(object parent)
{
if (parent is Control)
{
var cn = (Control)parent;
cn.LocationChanged -= UserControl1_LocationChanged;
if (cn.Parent != null)
RemoveBinding(cn.Parent);
}
}
private void UserControl1_LocationChanged(object sender, EventArgs e)
{
_location = ((Control)sender).Location;
this.Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.DrawString(string.Format("{0},{1}", _location.X, _location.Y), this.Font, Brushes.Black, new Point(this.Width/2, this.Height /2));
}
protected override void OnLocationChanged(EventArgs e)
{
base.OnLocationChanged(e);
_location = this.Location;
this.Invalidate();
}
}
[ToolboxItem(true)]
public partial class UserControl1 : UserControl
{
Point _location = new Point(0, 0);
public UserControl1()
{
SetStyle(ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
InitializeComponent();
}
~UserControl1()
{
if (this.ParentForm != null)
ParentForm.LocationChanged -= Parentform_LocationChanged;
}
private void Parentform_LocationChanged(object sender, EventArgs e)
{
_location = ((Form)sender).Location;
this.Invalidate();
}
protected override void OnCreateControl()
{
base.OnCreateControl();
if (this.ParentForm != null)
ParentForm.LocationChanged += Parentform_LocationChanged;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.DrawString(string.Format("{0},{1}", _location.X, _location.Y), this.Font, Brushes.Black, new Point(this.Width/2, this.Height /2));
}
protected override void OnLocationChanged(EventArgs e)
{
base.OnLocationChanged(e);
_location = this.Location;
this.Invalidate();
}
}