C#怎么做一个永远没有焦点的窗口 类似输入法控制栏
[删除(380066935@qq.com或微信通知)]
在窗体加入如下代码,防止窗体获得焦点
private const int WM_MOUSEACTIVATE = 0x21;
private const int MA_NOACTIVATE = 3;
protected override void WndProc(ref Message m)
if (m.Msg == WM_MOUSEACTIVATE)
m.Result = new IntPtr(MA_NOACTIVATE);
protected override bool ShowWithoutActivation
然后再加入如下代码,防止窗体的控件获得焦点
void SetChildControlNoFocus(Control ctrl)
foreach (Button c in ctrl.Controls)
MethodInfo SetControlStyleMethod;
object[] SetControlStyleArgs = new object[] { ControlStyles.Selectable, false };
private void SetControlNoFocus(Button ctrl)
SetControlStyleMethod.Invoke(ctrl, SetControlStyleArgs);
SetChildControlNoFocus(ctrl);
最后就是在窗体的构造函数部分调用如下代码,完成初始化参数的任务
SetControlStyleMethod = typeof(Button).GetMethod("SetStyle",
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod);
SetChildControlNoFocus(this);
this.DoubleBuffered = true;
SetStyle(ControlStyles.Selectable, false);
this.FormBorderStyle = FormBorderStyle.None;
this.ShowInTaskbar = false;
最后这个窗体,就永远无法获得焦点了