0

我有一个 C# 应用程序,它创建一个表单并将其堆叠在另一个应用程序窗口的前面。我通过使用SetParent. 但是,(新)父窗口冻结。

我该如何解决?这是线程的问题吗?

这是有效的:

private void Test(object sender, EventArgs e)
        {
            FormCover cov = new FormCover();
            IntPtr hwnd = Win32Utils.FindWindowByCaptionStart(IntPtr.Zero, TrackerName, null);

            Win32Utils.SetParent(cov.Handle, hwnd);
            cov.SetDesktopLocation(0, 0);

            cov.Show();
        }

但这(带有计时器已过事件)不是

public partial class Form1 : Form
    {

FormCover cover;

void tmrCheck_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            ShowCover();
        }

private void ShowCover()
        {
            cover = new FormCover();
            IntPtr hwnd = Win32Utils.FindWindowByCaptionStart(IntPtr.Zero, TrackerName, null);

            cover.CoverInitialize(hwnd);
            cover.Activate();
        }
}
//------

public partial class FormCover : Form
    {
        public delegate void IntPtrDlg(IntPtr param);

        public FormCover()
        {
            InitializeComponent();
        }

        internal void CoverInitialize(IntPtr hwdnParent)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new IntPtrDlg(CoverInitialize), new object[] { hwdnParent });
            }
            else
            {
                Win32Utils.SetParent(this.Handle, hwdnParent);
                this.SetDesktopLocation(0, 0);
            }
        }

        internal void CoverActivate(IntPtr handleFormulario)
        {
            if (!Visible)
                this.Show();
        }

        internal void CoverFinalize()
        {
            Hide();
            Win32ParentUtils.SetParent(Handle, new IntPtr());
        }
    }

这两个样本有什么区别?第一个工作正常,第二个正在冻结aprent窗口。

4

1 回答 1

1

正如我刚才所说,您需要为您的表单创建一个消息泵。尝试

Thread thread = new Thread( () =>
{
     var formCover = new FormCover();
     Application.Run(formCover);
});
thread.ApartmentState = ApartmentState.STA;
thread.Start();

然后你应该能够设置你的表单的父级。

请参阅此处以获取更多参考。

于 2013-02-20T09:32:47.757 回答