110,567
社区成员
发帖
与我相关
我的任务
分享
public partial class Form2 : Form
{
public Form2()
{
SetControlStyleMethod = typeof(Control).GetMethod("SetStyle",
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod);
SetChildControlNoFocus(this);
this.FormBorderStyle = FormBorderStyle.None;
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.SetStyle(ControlStyles.Selectable, false);
this.BackColor = Color.Red;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x21)
{
m.Result = new IntPtr(0x03);
return;
}
base.WndProc(ref m);
}
protected override bool ShowWithoutActivation
{
get
{
return true;
}
}
void SetChildControlNoFocus(Control ctrl)
{
foreach (Control c in ctrl.Controls)
{
SetControlNoFocus(c);
}
}
MethodInfo SetControlStyleMethod;
object[] SetControlStyleArgs = new object[] { ControlStyles.Selectable, false };
private void SetControlNoFocus(Control ctrl)
{
SetControlStyleMethod.Invoke(ctrl, SetControlStyleArgs);//设置控件无法获得焦点
SetChildControlNoFocus(ctrl); //同时设置其子控件
}
[DllImport("user32", EntryPoint = "SetWindowPos")]
static extern bool SetWindowPos(IntPtr hWnd, int hWndlnsertAfter, int X, int Y, int cx, int cy, int flags);
bool _TopMost = false;
public new bool TopMost
{
get
{
return _TopMost;
}
set
{
_TopMost = value;
SetWindowPos(Handle, TopMost ? -1 : -2, 0, 0, 0, 0, 19);
}
}
}
private void SetControlNoFocus(Control ctrl)
{
SetControlStyleMethod.Invoke(ctrl, SetControlStyleArgs);//设置控件无法获得焦点
if (ctrl.HasChildren)
{
SetChildControlNoFocus(ctrl); //同时设置其子控件
}
}