是否有更优雅的方式来安排我的表格?

时间:2012-02-11 09:53:30

标签: c# winforms

我是一个相当新手的程序员,最近提出了一个适用于我的项目的解决方案,但是我一直在寻找改进代码的方法。

所以基本上,我有一个弹出窗口的设置表单,我正在寻找一种方法将它放在我的主窗体旁边但不覆盖它,也没有部分显示在主窗体的屏幕上。我想出了这个,但它不是非常动态,因为它只检查4个不同的位置,如果它们都不起作用,它使用默认值,即中心屏幕。

这就是我所拥有的:

private void Place_Form(Form formToPlaceNextTo, Form formToPlace)
    {
        Point alignRightTop = new Point(m_parent.Location.X + m_parent.Width, m_parent.Location.Y);
        Point alignRightBottom = new Point(m_parent.Location.X + m_parent.Width, (m_parent.Location.Y + m_parent.Height) - this.Height);
        Point alignLeftTop = new Point(m_parent.Location.X - this.Width, m_parent.Location.Y);
        Point alignLeftBottom = new Point(m_parent.Location.X - this.Width, (m_parent.Location.Y + m_parent.Height) - this.Height);

        if (Screen.FromControl(formToPlace).WorkingArea.Contains(new Rectangle(alignRightTop.X, alignRightTop.Y, this.Width, this.Height)))
        {
            this.Location = alignRightTop;
            return;
        }
        if (Screen.FromControl(formToPlace).WorkingArea.Contains(new Rectangle(alignRightBottom.X, alignRightBottom.Y, this.Width, this.Height)))
        {
            this.Location = alignRightBottom;
            return;
        }
        if (Screen.FromControl(formToPlace).WorkingArea.Contains(new Rectangle(alignLeftTop.X, alignLeftTop.Y, this.Width, this.Height)))
        {
            this.Location = alignLeftTop;
            return;
        }
        if (Screen.FromControl(formToPlace).WorkingArea.Contains(new Rectangle(alignLeftBottom.X, alignLeftBottom.Y, this.Width, this.Height)))
        {
            this.Location = alignLeftBottom;
            return;
        }
    }

有任何建议或首选编码技术吗?

1 个答案:

答案 0 :(得分:0)

正如Snowbear所建议的,我使用另一个SE网站提出同样的问题并得到了很好的回应。

https://codereview.stackexchange.com/questions/8881/is-there-a-more-elegant-way-of-arranging-my-forms

相关问题