【问题标题】:Moving Child Form with Parent Form使用父表单移动子表单
【发布时间】:2014-08-12 19:39:26
【问题描述】:

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

当子窗体最初显示时,我设置了一个称为“零点”的东西,这是子窗体相对于父窗体的位置,并由父窗体上的控件之一定义:

    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 设置子级时,我想知道应该在哪里定义这个“零点”:

    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) 坐标处。

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

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

【问题讨论】:

    标签: c# winforms visual-studio-2010


    【解决方案1】:

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

    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;
        }
    }
    

    【讨论】:

    • 我对 ChildForm 的构造函数做了一个小的修改。事实证明,我毕竟不需要对 ParentForm 的引用。查看我所做的更改。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 2015-12-11
    • 1970-01-01
    • 2020-04-05
    • 2014-09-05
    相关资源
    最近更新 更多