public partial class Form1 : Form
{
Timer timer;
bool resetting = false;
Form2 frm2;
public Form1()
{
InitializeComponent();
frm2 = new Form2();
frm2.Show();
timer = new Timer();
timer.Interval = 50;
timer.Tick += new EventHandler(timer1_Tick);
}
private Form mMainForm;
private List<Form> mConnectedForms = new List<Form>();
private Point mMainLocation;
public Form1(Form mainForm)
{
this.mMainForm = mainForm;
this.mMainLocation = new Point(this.mMainForm.Location.X, this.mMainForm.Location.Y);
this.mMainForm.LocationChanged += new EventHandler(Form1_LocationChanged);
}
public void ConnectForm(Form form)
{
if (!this.mConnectedForms.Contains(form))
{
this.mConnectedForms.Add(form);
}
}
private void button1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_Activated(object sender, EventArgs e)
{
if (!resetting)
{
timer.Start();
frm2.Focus();
this.Focus();
resetting = true;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
timer.Stop();
resetting = false;
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
}
private void Form1_LocationChanged(object sender, EventArgs e)
{
Point relativeChange = new Point(this.mMainForm.Location.X - this.mMainLocation.X, this.mMainForm.Location.Y - this.mMainLocation.Y);
foreach (Form form in this.mConnectedForms)
{
form.Location = new Point(form.Location.X + relativeChange.X, form.Location.Y + relativeChange.Y);
}
this.mMainLocation = new Point(this.mMainForm.Location.X, this.mMainForm.Location.Y);
}
}
اینم فرم 2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
}
}