111,126
社区成员
发帖
与我相关
我的任务
分享
//program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new parent());
}
}
}
//parent.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public class parent : Form
{
public parent()
{
this.Load += new EventHandler(parent_Load);
}
child child = new child();
void parent_Load(object sender, EventArgs e)
{
child.DragPointChanged += new child.DragPointChangedEventHandler(child_DragPointChanged);
child.Show();
}
Point dragPoint = new Point(10, 10);
public Point DragPoint
{
get { return dragPoint; }
set
{
if (dragPoint != value)
{
dragPoint = value;
child.SetDragPointLocation(dragPoint);
}
}
}
bool isDragging = false;
void child_DragPointChanged(object send, DragPointChangedEventArgvs e)
{
dragPoint = e.CurrentPoint;
this.Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.FillEllipse(new SolidBrush(Color.Red), DragPoint.X - 5, DragPoint.Y - 5, 10, 10);
base.OnPaint(e);
}
protected override void OnMouseDown(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && isDragging == false)
{
isDragging = true;
DragPoint = e.Location;
this.Invalidate();
}
else
{
isDragging = false;
}
base.OnMouseDown(e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && isDragging == true)
{
DragPoint = e.Location;
this.Invalidate();
}
else if (e.Button != MouseButtons.Left)
{
isDragging = false;
}
base.OnMouseMove(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
isDragging = false;
base.OnMouseUp(e);
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public class child : Form
{
NumericUpDown numericUpDownX = new NumericUpDown();
NumericUpDown numericUpDownY = new NumericUpDown();
public child()
{
this.Controls.Add(numericUpDownX);
this.Controls.Add(numericUpDownY);
this.numericUpDownX.Location = new Point(10, 10);
this.numericUpDownY.Location = new Point(10, 50);
this.numericUpDownX.Maximum = 100000;
this.numericUpDownX.Minimum = -100000;
this.numericUpDownY.Maximum = 100000;
this.numericUpDownY.Minimum = -100000;
this.numericUpDownX.ValueChanged += new EventHandler(numericUpDownX_ValueChanged);
this.numericUpDownY.ValueChanged += new EventHandler(numericUpDownY_ValueChanged);
this.numericUpDownX.Value = 10;
this.numericUpDownY.Value = 10;
}
public delegate void DragPointChangedEventHandler(object send, DragPointChangedEventArgvs e);
public event DragPointChangedEventHandler DragPointChanged;
private void OnDragPointChanged(DragPointChangedEventArgvs e)
{
if (DragPointChanged != null)
DragPointChanged(this, e);
}
void numericUpDownY_ValueChanged(object sender, EventArgs e)
{
OnDragPointChanged(new DragPointChangedEventArgvs(new Point((int)numericUpDownX.Value, (int)numericUpDownY.Value)));
}
void numericUpDownX_ValueChanged(object sender, EventArgs e)
{
OnDragPointChanged(new DragPointChangedEventArgvs(new Point((int)numericUpDownX.Value, (int)numericUpDownY.Value)));
}
public void SetDragPointLocation(Point location)
{
this.numericUpDownX.Value = location.X;
this.numericUpDownY.Value = location.Y;
}
}
public class DragPointChangedEventArgvs
{
public Point CurrentPoint { get { return _currentPoint; } }
private Point _currentPoint;
public DragPointChangedEventArgvs(Point pt)
{
_currentPoint = pt;
}
}
}