使用SetParent冻结父窗口 [英] Using SetParent freeze the parent window
问题描述
我有一个使用C#编写的应用程序,该应用程序创建一个表单并将其堆叠在另一个应用程序窗口的前面.我通过使用 SetParent
来做到这一点.但是,(新)父窗口冻结.
I have an application in C# which creates a form and stack it in front of another app's window.
I do this by using SetParent
. However, the (new) parent window freezes.
我该如何解决?这是线程问题吗?
How can I solve that? Is this a matter of threading?
这有效:
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();
}
但这(带有经过计时器的事件)不是:
But this (with a timer elapsed event) is not:
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());
}
}
这两个样本之间有什么区别?第一个工作正常,第二个冻结当前的窗口.
What is the difference between these two samples? The first one is working fine, the second one is freezing the aprent window.
推荐答案
正如我刚才所说,您将需要为表单创建一个消息泵.试试
As I just stated, you'll need to create a message pump for your form. Try
Thread thread = new Thread( () =>
{
var formCover = new FormCover();
Application.Run(formCover);
});
thread.ApartmentState = ApartmentState.STA;
thread.Start();
然后,您应该可以设置表单的父级.
Then you should be able to set the parent of your form.
请参见此处以供进一步参考.
See here for further reference.
这篇关于使用SetParent冻结父窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!