C# 请问如何获取当前窗体(可以拖动)的位置,从而去确定它所调用的窗体之间的相对位置。

2025-05-08 07:52:45
推荐回答(2个)
回答1:

new出这个窗体的时候,获取主窗体的坐标,this.Location.X,this.Location.Y;
当新窗体show之后,你说可以移动,没问题的,有一个locationChanged事件,这时把新窗体的坐标获取出来就可以了,再和之前主窗体的坐标可以比较了。

回答2:

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.LocationChanged += new System.EventHandler(this.Form1_LocationChanged);
}
private void Form1_LocationChanged(object sender, EventArgs e)
{
this.Text = string.Format("Left={0},Top={1}", this.Left, this.Top);
}
}