移动子表格与父表格

时间:2014-08-12 19:39:26

标签: c# winforms visual-studio-2010

我有2个表格:父母和支持儿童表格。

当最初显示Child表单时,我设置了一个名为“Zero Point”的东西,这是Child表单相对于Parent的位置,并由Parent之一的控件定义:

    private void miShowChild_Click(object sender, EventArgs e) {
        if ((m_childForm == null) || m_childForm .IsDisposed) {
            m_formLeft = this.Left + this.Control1.Left;
            m_formTop = this.Top + this.Control1.Top;
            m_childForm = new ChildForm1();
            m_childForm.Show(this);
            m_childForm.Parent_Shift(m_formTop, m_formLeft);
        }
        m_roleMap.TopLevel = true;
    }

当父表单移动到其他位置时,该信息将转换为子表单:

    private void Form1_MoveResize(object sender, EventArgs e) {
        if ((m_childForm != null) && !m_childForm.IsDisposed) {
            m_formLeft = this.Left + this.Control1.Left;
            m_formTop = this.Top + this.Control1.Top;
            m_childForm.Parent_Shift(m_formTop, m_formLeft);
        }
    }

这个逻辑似乎没问题,对吧?

那么,Child表单会发生什么?

首先,我已经定义了这些变量:

    private bool m_automate;
    private int m_left, m_top;
    private Point m_zero;

    private void ChildForm_Load(object sender, EventArgs e) {
        m_left = 0;
        m_top = 0;
        m_zero = Point.Empty;
    }

当使用Parent_Shift设置Child时,我想知道应该定义这个“Zero Point”的位置:

    public void Parent_Shift(int parentTop, int parentLeft) {
        m_automate = true;
        if (m_zero != Point.Empty) {
            this.Left = parentLeft - m_left;
            this.Top = parentTop - m_top;
        } else {
            this.Left = parentLeft;
            this.Top = parentTop;
        }
        m_zero = new Point(this.Left, this.Top);
        m_automate = false;
    }

当重新定位子窗体时,而不是因为父移动,那么我想记录离我的“零点”有多远:

    private void Form_MoveResize(object sender, EventArgs e) {
        if (!m_automate && this.Visible) {
            if (m_zero != Point.Empty) {
                m_left = m_zero.X - this.Left;
                m_top = m_zero.Y - this.Top;
            } else {
                m_zero = new Point(this.Left, this.Top);
            }
        }
    }

所有方法都在开火,但我似乎无法找到坐标系。

我可以手动移动子窗体,然后将父窗体和子窗体扭曲移动到其他位置 - 然后与父窗口成比例移动。

我希望Child保持相对于Parent的某些(X,Y)坐标。

移动子窗体时,需要将该相对坐标更新为离开子窗体的位置。

有谁知道如何解决我的问题?

1 个答案:

答案 0 :(得分:2)

将此代码放在您的父级中。您的子表单中不需要任何代码。

public partial class ParentForm : Form
{
    private bool updatingChildPosition = false;
    private Point childFormOffset = Point.Empty;
    private ChildForm child = null;

    public ParentForm()
    {
        InitializeComponent();

        this.child = new ChildForm();
        child.Show();
        child.Move += child_Move;

        UpdateChildFormOffset();
    }

    void child_Move(object sender, EventArgs e)
    {
        // Child form is moved, store it's new offset
        UpdateChildFormOffset();
    }

    private void UpdateChildFormOffset()
    {
        if (!updatingChildPosition)
        {
            this.childFormOffset = new Point(
                this.child.Location.X - this.Location.X,
                this.child.Location.Y - this.Location.Y);

        }
    }

    private void ParentForm_Move(object sender, EventArgs e)
    {
        // Updating child position
        this.updatingChildPosition = true;

        child.Location = new Point(
            this.Location.X + childFormOffset.X,
            this.Location.Y + childFormOffset.Y);

        this.updatingChildPosition = false;
    }
}