c#中如何避免Button,CheckBox等的点击状态时的焦点矩形框问题:

来源:互联网 发布:社交软件市场分析 编辑:程序博客网 时间:2024/05/17 18:15

方法一:

    使用label控件替代;

方法二:

    转移焦点:

    利用一个label控件,将其BackColor设为透明,比如checkbox,在其enter事件中转移焦点即可,代码如下:

   

private void control_lose_focus()        {            lblHide.Focus();        }

private void chkAutoLogin_Enter(object sender, EventArgs e)        {            control_lose_focus(); //在enter的情况下转移焦点最佳        }

方法三:重绘控件:如button,代码如下:

1,第一种验证过:在重绘控件的构造函数中:

SetStyle(ControlStyles.Selectable, false);
2,没验证,但是也可以:

public class MyButton : System.Windows.Forms.Button    {        public bool Selectable        {            get { return this.GetStyle(System.Windows.Forms.ControlStyles.FixedHeight); }            set { this.SetStyle(System.Windows.Forms.ControlStyles.Selectable, value); }        }    }   
使用时:
button1.Selectable=false;  //设置不让捕获焦点;button1.Selectable=true;   //设置可以捕获焦点,就和.net的Button完全一样。
方法四:最绝:

private void SetButton(Button button)          {              MethodInfo methodinfo = button.GetType().GetMethod("SetStyle",BindingFlags.NonPublic                 | BindingFlags.Instance | BindingFlags.InvokeMethod);              methodinfo.Invoke(button,BindingFlags.NonPublic                 | BindingFlags.Instance | BindingFlags.InvokeMethod,null,new object[]                 {ControlStyles.Selectable,false},Application.CurrentCulture);          }  

点击后不得到焦点且不影响当前焦点所在位置,类似系统计算器中按钮的效果。

可以实现无焦点的按钮、复选框、单选框等控件。





 

0 0
原创粉丝点击