在主窗体的右上角打开子窗体

时间:2012-07-21 18:16:28

标签: c# winforms

我试图在主窗体的右上角打开一个(非装饰的)子窗体,无论主窗体是最大化还是正常大小。 但无论我如何尝试,我都不会把它打开到我想要的地方。

我发现了一篇帖子,描述了如何相对于表单中的另一个控件打开表单,但这也不起作用:

How to display a Modal form in a position relative to the a control in the parent window (opener)

现在尝试在Google上搜索几个小时以获得解决方案,但要么没有答案(双倍)或者我没有搜索紧密的单词组合(更有可能)。

有人可以请我指出类似的问题,或者帮助我如何实现我所希望的目标?

3 个答案:

答案 0 :(得分:2)

听起来我应该使用你锚定到顶部和右边的UserControl。但是让我们做一个表格。您需要连接其Load事件,以便在重新调整自身后将其移动到正确的位置。然后,您需要主窗体的LocationChanged和Resize事件,以便您可以将子窗体保留在正确的位置。

因此,带有样板Form1和Form2名称的示例程序以及Form1上显示子项的按钮可能如下所示:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        this.button1.Click += button1_Click;
        this.Resize += this.Form1_Resize;
        this.LocationChanged += this.Form1_LocationChanged;

    }

    Form child;

    private void button1_Click(object sender, EventArgs e) {
        if (child != null) return;
        child = new Form2();
        child.FormClosed += child_FormClosed;
        child.Load += child_Load;
        child.Show(this);
    }

    void child_FormClosed(object sender, FormClosedEventArgs e) {
        child.FormClosed -= child_FormClosed;
        child.Load -= child_Load;
        child = null;
    }

    void child_Load(object sender, EventArgs e) {
        moveChild();
    }

    void moveChild() {
        child.Location = this.PointToScreen(new Point(this.ClientSize.Width - child.Width, 0));
    }

    private void Form1_Resize(object sender, EventArgs e) {
        if (child != null) moveChild();
    }

    private void Form1_LocationChanged(object sender, EventArgs e) {
        if (child != null) moveChild();
    }

}

答案 1 :(得分:0)

尝试类似的东西:

 private void button1_Click(object sender, EventArgs e)
        {
            ChildForm win = new ChildForm();
            int screenHeight = Screen.PrimaryScreen.WorkingArea.Height;
            int screenWidth = Screen.PrimaryScreen.WorkingArea.Width;

            Point parentPoint = this.Location;

            int parentHeight = this.Height;
            int parentWidth = this.Width;

            int childHeight = win.Height;
            int childWidth = win.Width;

            int resultX = 0;
            int resultY = 0;

            if ((parentPoint.X + parentWidth + childWidth) > screenWidth)
            {
                resultY = parentPoint.Y;
                resultX = parentPoint.X - childWidth;
            }
            else
            {
                resultY = parentPoint.Y;
                resultX = parentPoint.X + parentWidth;
            }

            win.StartPosition = FormStartPosition.Manual;                   
            win.Location = new Point(resultX, resultY);  
            win.Show();
        }

答案 2 :(得分:0)

我认为你应该尝试这样的事情:

private void buttonOpen_Click(object sender, EventArgs e)
{
    Form2 frm2 = new Form2();
    frm2.Show();
    //"this" is the parent form
    frm2.DesktopLocation = new Point(this.DesktopLocation.X + this.Width - frm2.Width, this.DesktopLocation.Y);
}

简单,简单,有效(对我而言)。

相关问题