C# WinC窗体相对于另一个窗体定位窗体

C# WinC窗体相对于另一个窗体定位窗体,c#,winforms,C#,Winforms,在我的申请表中,我有一个主表格。单击“打开”按钮时,我想显示第二个无边框表单,其中包含文本加载。到目前为止,我一直在努力 但是我想要的是加载表单相对于主表单居中。我该怎么做 解决方案: private void tsbOpen_Click(object sender, EventArgs e) { if (_fileDialog.ShowOpenDialog() == DialogResult.OK) { _progress = new frmProgress()

在我的申请表中,我有一个主表格。单击“打开”按钮时,我想显示第二个无边框表单,其中包含文本加载。到目前为止,我一直在努力

但是我想要的是加载表单相对于主表单居中。我该怎么做

解决方案:

private void tsbOpen_Click(object sender, EventArgs e)
{
    if (_fileDialog.ShowOpenDialog() == DialogResult.OK)
    {
        _progress = new frmProgress(); // _progress is a member var
        backgroundWorker1.RunWorkerAsync("open");
        _progress.ShowDialog(this);

    }
}

您可以将StartPosition设置为CenterParent并将主窗体作为所有者传递。

您可以将StartPosition设置为CenterParent并将主窗体作为所有者传递。

获取主窗体坐标的位置及其大小,获取子窗体的大小并对其进行一些简单的数学运算。

获取主窗体的位置坐标和它的大小,取子窗体的大小,并在上面加上一些简单的数学运算。

Martijn试试这个

在该方法的开始处,输入如下代码

public sub Bah()
{
    if (me.InvokeRequired)
    {
        me.Invoke(new action(Bah));
        return
    }

    myform.showdialog...
}
不知道这段代码是否编译到100%,但你明白了

在该方法的开始处,输入如下代码

public sub Bah()
{
    if (me.InvokeRequired)
    {
        me.Invoke(new action(Bah));
        return
    }

    myform.showdialog...
}

不知道这段代码是否编译到100%,但你明白了

我创建了一个名为ProcessingRequest的子表单,并在其上添加了一些文本和动画gif

我在主窗体中有一个属性,用于计算子窗体应位于的位置

private Point ProcessingLocation { get { return new Point(this.Location.X + this.Width / 2 - new ProcessingRequest().Width / 2, this.Location.Y + this.Height / 2 - new ProcessingRequest().Height / 2); } }
我有一个类,它生成一个新线程来显示子窗体

    public class ShowProgress
    {
        static private System.Drawing.Point point;
        static private ProcessingRequest p;
        static public void ShowProgressForm(System.Drawing.Point myPoint)
        {
            point = myPoint;
            Thread t = new Thread(new ThreadStart(ShowProgress.ShowForm));
            t.IsBackground = true;
            t.SetApartmentState(ApartmentState.STA);
            t.Start();
        }
        static private void ShowForm()
        {
            p = new ProcessingRequest();
            p.StartPosition = FormStartPosition.Manual;
            p.Location = point;
            p.TopMost = true;
            Application.Run(p);
        }

        static public void CloseForm()
        {
            p.Invoke(new CloseDelegate(ShowProgress.CloseFormInternal));
        }

        static private void CloseFormInternal()
        {
            p.Close();
        }
    }
    public delegate void CloseDelegate();
然后在我的主要形式中,我简单地说

ShowProgress.ShowProgressForm(ProcessingLocation);
//heavy processing code goes here or whatever
ShowProgress.CloseForm();

我创建了一个名为ProcessingRequest的子表单,并在其上放置了一些文本和动画gif

我在主窗体中有一个属性,用于计算子窗体应位于的位置

private Point ProcessingLocation { get { return new Point(this.Location.X + this.Width / 2 - new ProcessingRequest().Width / 2, this.Location.Y + this.Height / 2 - new ProcessingRequest().Height / 2); } }
我有一个类,它生成一个新线程来显示子窗体

    public class ShowProgress
    {
        static private System.Drawing.Point point;
        static private ProcessingRequest p;
        static public void ShowProgressForm(System.Drawing.Point myPoint)
        {
            point = myPoint;
            Thread t = new Thread(new ThreadStart(ShowProgress.ShowForm));
            t.IsBackground = true;
            t.SetApartmentState(ApartmentState.STA);
            t.Start();
        }
        static private void ShowForm()
        {
            p = new ProcessingRequest();
            p.StartPosition = FormStartPosition.Manual;
            p.Location = point;
            p.TopMost = true;
            Application.Run(p);
        }

        static public void CloseForm()
        {
            p.Invoke(new CloseDelegate(ShowProgress.CloseFormInternal));
        }

        static private void CloseFormInternal()
        {
            p.Close();
        }
    }
    public delegate void CloseDelegate();
然后在我的主要形式中,我简单地说

ShowProgress.ShowProgressForm(ProcessingLocation);
//heavy processing code goes here or whatever
ShowProgress.CloseForm();

Thnx,但我显示了线程中的第二种形式。当我尝试form.Showthis时,我会出现以下错误:“跨线程操作无效:从创建控件“frmProcessBuilder”的线程以外的线程访问该控件。我无法使用showthi,因为在关闭此对话框之前,我的代码会停止。那不是我需要的。在加载完成之前,表单必须一直显示。@Martijn:看起来您有线程问题。从UI线程以外的其他线程访问表单时,必须使用Control.BeginInvoke。但是,线程是使用ShowDialog时代码停止的解决方案。然而,使用Show也不是解决方案:这样用户就可以操纵正在构造的表单了,我无法计算出来。现在我尝试使用ShowDialog。我已经移动了我的代码。现在我有了这个:frmProgress progress=新frmProgress;进步。展示这一点;backgroundWorker1.RunWorkerAsyncopen;进展。关闭;但是现在什么也没发生,因为有了ShowDialig。我该如何在do_Work事件中使用此选项?如果复制此代码,则会出现线程错误。请尝试将RunWorkerAsync调用放在ShowDialog之前。在Do_工作中,当您想要访问controls.Thnx时,必须使用Control.BeginInvoke,但我在线程中显示了第二种形式。当我尝试form.Showthis时,我会出现以下错误:“跨线程操作无效:从创建控件“frmProcessBuilder”的线程以外的线程访问该控件。我无法使用showthi,因为在关闭此对话框之前,我的代码会停止。那不是我需要的。在加载完成之前,表单必须一直显示。@Martijn:看起来您有线程问题。从UI线程以外的其他线程访问表单时,必须使用Control.BeginInvoke。但是,线程是使用ShowDialog时代码停止的解决方案。然而,使用Show也不是解决方案:这样用户就可以操纵正在构造的表单了,我无法计算出来。现在我尝试使用ShowDialog。我已经移动了我的代码。现在我有了这个:frmProgress progress=新frmProgress;进步。展示这一点;backgroundWorker1.RunWorkerAsyncopen;进展。关闭;但是现在什么也没发生,因为有了ShowDialig。我该如何在do_Work事件中使用此选项?如果复制此代码,则会出现线程错误。请尝试将RunWorkerAsync调用放在ShowDialog之前。在Do_工作中,当您想要访问controls.Thnx时,必须使用Control.BeginInvoke,但这不起作用。也许是因为我从一个线程运行这个代码?后台工作人员,但这不起作用。也许是因为我从一个线程运行这个代码?后台工作人员