显示后面一切形式,并没有偷焦点? [英] Show a Form behind everything else, and without stealing focus?

查看:157
本文介绍了显示后面一切形式,并没有偷焦点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个关于计算器上如何显示没有偷焦点形式的现有问题。答案是覆盖 ShowWithoutActivati​​on 键,返回true:

 保护覆盖BOOL ShowWithoutActivati​​on
{
  获得{返回true; }
}
 

此作品不够好。

现在我想多走一步。我想表演一个表格(即使其可见),但它是的背后的其他形式的z顺序。

可能在.net中?

如果没有,可以用P /调用?

奖金颤振

调用 SendToBack()不工作:

  RunnerForm FRM =新RunnerForm();
// frm.Show();
   frm.Visible = TRUE;
   frm.SendToBack();
 

解决方案

的PInvoke使用的 SetWindowPos功能

 公共静态类HWND {
  公共静态只读的IntPtr
  NOTOPMOST =新的IntPtr(-2),
  BROADCAST =新的IntPtr(0xFFFF的),
  TOPMOST =新的IntPtr(-1),
  TOP =新的IntPtr(0),
  BOTTOM =新的IntPtr(1);
}

公共静态类SWP {
  公共静态只读INT
  NOSIZE = 0×0001,
  NOMOVE = 0×0002,
  NOZORDER =×0004,
  NOREDRAW =×0008,
  NOACTIVATE = 0×0010,
  并条机= 0×0020,
  FRAMECHANGED = 0×0020,
  的ShowWindow = 0x0040,
  HIDEWINDOW = 0x0080,会
  NOCOPYBITS = 0100,
  NOOWNERZORDER = 0x0200,
  NOREPOSITION = 0x0200,
  NOSENDCHANGING =的0x0400,
  DEFERERASE =为0x2000,
  ASYNCWINDOWPOS = 0x4000的;
}

[的DllImport(user32.dll中)
公共静态的extern BOOL SetWindowPos(IntPtr的的HWND,IntPtr的hWndInsertAfter,INT X,INT Y,INT CX,INT CY,诠释和uFlags);

私人无效的button1_Click(对象发件人,EventArgs的){
  RunnerForm FRM =新RunnerForm();
  SetWindowPos(frm.Handle,HWND.BOTTOM,0,0,0,0,SWP.SHOWWINDOW | SWP.NOMOVE | SWP.NOOWNERZORDER | SWP.NOSIZE | SWP.NOACTIVATE);
}
 

There's an existing question on StackOverflow on how to show a form without stealing focus. The answer is override ShowWithoutActivation and return true:

protected override bool ShowWithoutActivation
{
  get { return true; }
}

This works well enough.

Now i want to go one step further. i want a show a Form (i.e. make it visible), but have it be behind other forms in the z-order.

Possible in .net?

If not, possible with P/Invoke?

Bonus Chatter

Calling SendToBack() doesn't work:

   RunnerForm frm = new RunnerForm();
// frm.Show();
   frm.Visible = true;
   frm.SendToBack();

解决方案

A little bit of PInvoke using SetWindowPos function

public static class HWND {
  public static readonly IntPtr
  NOTOPMOST = new IntPtr(-2),
  BROADCAST = new IntPtr(0xffff),
  TOPMOST = new IntPtr(-1),
  TOP = new IntPtr(0),
  BOTTOM = new IntPtr(1);
}

public static class SWP {
  public static readonly int
  NOSIZE = 0x0001,
  NOMOVE = 0x0002,
  NOZORDER = 0x0004,
  NOREDRAW = 0x0008,
  NOACTIVATE = 0x0010,
  DRAWFRAME = 0x0020,
  FRAMECHANGED = 0x0020,
  SHOWWINDOW = 0x0040,
  HIDEWINDOW = 0x0080,
  NOCOPYBITS = 0x0100,
  NOOWNERZORDER = 0x0200,
  NOREPOSITION = 0x0200,
  NOSENDCHANGING = 0x0400,
  DEFERERASE = 0x2000,
  ASYNCWINDOWPOS = 0x4000;
}

[DllImport("user32.dll")]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);

private void button1_Click(object sender, EventArgs e) {
  RunnerForm frm = new RunnerForm();
  SetWindowPos(frm.Handle, HWND.BOTTOM, 0, 0, 0, 0, SWP.SHOWWINDOW | SWP.NOMOVE | SWP.NOOWNERZORDER | SWP.NOSIZE | SWP.NOACTIVATE);
}

这篇关于显示后面一切形式,并没有偷焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆