WinForms 新窗体后台打开完美的解决

最近在做浏览器开发时,想要实现 IE 6那种多窗体,又允许后台打开而不抢占视野的方式。

WinForms 应用程序中想要后台打开一个新的窗体,而不(抢焦)、(遮挡)目前窗体。

需要注意的是,SW_SHOWNOACTIVATE打开的“不抢焦”窗体,是不会执行 Shown 和 Load。一定要注意!放置首页,如文章质量不够,有劳编辑移除:)

最简单的方法如下:

 

   public class TestForm : Form
{

const int SW_SHOWNOACTIVATE = 4;
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

protected override void OnCreateControl()
{
base.OnCreateControl();
// 在这里,可以增加其他标识来标识
if(this.Tag != null)
{
(this.Tag as Form).TopMost = false;
this.Tag = null;
}

}

private void NewWindow(bool focused)
{
var f = new TestForm();

if(focused) f.Show();
else
{
this.TopMost = true;
f.Tag = this;
ShowWindow(f.Handle, SW_SHOWNOACTIVATE);
}
}
}


 

posted @ 2012-01-19 11:04  Treenew Lyn  阅读(2565)  评论(0编辑  收藏  举报