最近在做浏览器开发时,想要实现 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);
}
}
}


 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-29
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-08
  • 2022-12-23
  • 2021-08-01
  • 2021-12-03
  • 2021-12-06
  • 2022-02-02
  • 2021-10-03
相关资源
相似解决方案