【问题标题】:Child form of invisible parent never being shown?永远不会显示隐形父母的子形式?
【发布时间】:2025-12-18 01:20:03
【问题描述】:

我有一个主窗体,它是不可见的,有时会创建一个子窗体。该子窗体在 Designer.cs 中如下所示:

        this.listBox1 = new System.Windows.Forms.ListBox();
        this.SuspendLayout();
        // 
        // listBox1
        // 
        this.listBox1.FormattingEnabled = true;
        //this.listBox1.Location = new System.Drawing.Point(0, 0);
        this.listBox1.Name = "listBox1";
        this.listBox1.Size = new System.Drawing.Size(255, 147);
        this.listBox1.TabIndex = 0;
        // 
        // Form2
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.AutoSize = true;
        this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
        this.ClientSize = new System.Drawing.Size(502, 384);
        this.ControlBox = false;
        this.Controls.Add(this.listBox1);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        this.Name = "Form2";
        this.Text = "Form2";

在主窗体中,我按如下方式创建 Form2:

    Form2 a = new Form2(new Point(0, Screen.PrimaryScreen.WorkingArea.Height / 2)); //This is the location
                        Thread thtt = new Thread((ThreadStart)delegate()
                            {
                                a.CreateControl();
                                a.Show();
                                TimeSpan slept = TimeSpan.Zero;
                                while (!a.Created && slept < TimeSpan.FromMilliseconds(2000))
                                {
                                    Thread.Sleep(99);
                                    slept += TimeSpan.FromMilliseconds(99);
                                }
                                if (!a.Created)
                                {
                                    MessageBox.Show("after 2 sec no creation?");
                                    System.Diagnostics.Debugger.Break();
                                }
                                else
                                {
                                    //a.Show();
                                    a.Invoke((MethodInvoker)delegate()
                                    {

                                        a.TopMost = true;
                                        a.Location = new Point(0, Screen.PrimaryScreen.WorkingArea.Height / 2);
                                        Cursor.Position = new Point(a.Location.X + 10, a.Location.Y + 10);
                                        a.AddAndAct(minimized);

                                    });
                                }
                                aa = a;
                            });
                        thtt.IsBackground = true;
                        thtt.Start();

我遇到的问题是,Form2 实际上是在闪烁,但随后神奇地消失了。有任何建议。

谢谢你的建议,亚历克斯

【问题讨论】:

  • 请问您为什么要创建一个隐形父母?第一个表单的 Visible 属性是否实际上设置为 false?父窗体调用上面的代码并创建子窗体的代码中的触发器是什么?
  • 触发器是一个热键,并且父项 Visible 设置为 false。正在创建父表单,但在某些时候 Hide();正在调用它,所以它不再可见。调用热键时,将调用事件处理程序,然后创建客户端。
  • 其实已经关闭了!我只是看了一下 EnumThreadWindows,窗口显示 1 秒,然后一言不发地关闭。很奇怪……
  • 嗨,Alex - 我将尝试在测试应用程序中重复它。同时,如果您在 Form2 的 form_load 事件中弹出一个消息框(试图强制它至少与消息框一样长时间保持打开状态),会发生什么情况。在您关闭消息框之前,我应该认为这会起作用,但也许我会感到惊讶。

标签: c# .net windows winforms invisible


【解决方案1】:

Alex - 我认为这是你的问题:

How to create a form on its own thread and keep it open throughout application lifetime

接受的答案是非常正确的,因为我也在我的应用程序中使用它,并且忘记了它。 =)

“您需要在新创建的线程上启动一个消息循环。您可以通过调用 Application.Run(form) 来做到这一点。”

【讨论】: