我在C#中有一个应用程序,它创建一个窗体并将它堆叠到另一个应用程序窗口的前面。 我通过使用SetParent
来做到这一点。但是,(新)父窗口冻结。使用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());
}
}
的是这两个样本之间的区别?第一个工作正常,第二个冻结初始窗口。
我想你不能只是像这样创建一个窗口......窗口需要消息泵,当它不是模态窗口时。 – 2013-02-20 09:30:44
尝试在GUI线程上运行整个ShowCover方法(通过使用(Begin)Invoke方法),就像在CoverInitialize中做的那样 – Bond 2013-02-20 09:34:15