New妙用和WinForm的Enabled=false时字体不可改变的解决方案

this.btnOk.Enabled=false;
this.btnOk.ForeColor = Color.Red;

当我们程序背景色为黑色或灰色时,禁用一个按钮后,此按钮的字体会变成灰色,背景也会变成灰色,就和背景混在一块看不清按钮上面的字是什么了,上面的代码想表达的意思是,在btnOk按钮禁用后,让其Enabled=false时显示的字体为红色,避免和背景混为一体,看不清按钮上的字体,解决方案大概是:button子类+new关键字+重写事件

代码如下:

/// <summary>
    /// 自定义按钮
    /// </summary>
    public class YButton : Button
    {
        /// <summary>
        /// 自定义按钮
        /// </summary>
        public YButton()
        {
            this.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.FlatAppearance.BorderColor = Color.White;
            this.FlatAppearance.BorderSize = 1;
            this.BackgroundImageLayout = ImageLayout.Stretch;
            this.TextAlign = ContentAlignment.MiddleCenter;

            this.Font = new Font("黑体",9f);
            this.ForeColor = Color.White;
            //this.BackgroundImage = WApp.util.Ui.assistant.UIResurece.Own.ImgButtonNormal;
            this.Size = new Size(102,32);
        }

        private Graphics g = null;

        /// <summary>
        /// 此组件的前景色,用于显示文本
        /// </summary>
        [Browsable(true), Category("外观"), Description("此组件的前景色,用于显示文本")]
        public new Color ForeColor
        {
            get { return base.ForeColor; }
            set
            {
                if (this.enabled)
                    base.ForeColor = value;
            }
        }

        private bool enabled = true;
        /// <summary>
        /// 指示是否已启用该控件,如果要使用原有的Enabled禁用控件,需要设置EnabledSet达到目的,EnabledSet级别高于此属性级别
        /// </summary>
        [Browsable(true), Category("行为"), Description("指示是否已启用该控件")]
        public new bool Enabled
        {
            get { return enabled; }
            set
            {
                enabled = value;
                if (value == false)
                    base.ForeColor = Color.FromArgb(150, 150, 150);
                else
                    this.ForeColor = Color.White;
            }
        }

        /// <summary>
        /// Enabled其否启用该控件
        /// </summary>
        public bool EnabledSet
        {
            get { return base.Enabled; }
            set { base.Enabled = value; }
        }

        protected override void OnClick(EventArgs e)
        {
            if (this.enabled)
                base.OnClick(e);
        }

        protected override void OnDoubleClick(EventArgs e)
        {
            if (this.enabled)
                base.OnDoubleClick(e);
        }

        protected override void OnMouseClick(MouseEventArgs e)
        {
            if (this.enabled)
                base.OnMouseClick(e);
        }

        protected override void OnMouseDoubleClick(MouseEventArgs e)
        {
            if (this.enabled)
                base.OnMouseDoubleClick(e);
        }

        /// <summary>
        /// 鼠标进入变换背景图片
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseEnter(EventArgs e)
        {
            if (this.enabled)
            {
                base.OnMouseEnter(e);
                //this.BackgroundImage = WApp.util.Ui.assistant.UIResurece.Own.ImgButtonSelected;
            }
        }

        /// <summary>
        /// 鼠标离开变换背景图片
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseLeave(EventArgs e)
        {
            if (this.enabled)
            {
                base.OnMouseLeave(e);
                //this.BackgroundImage = WApp.util.Ui.assistant.UIResurece.Own.ImgButtonNormal;
            }
        }
    }


这样,使用时使用YButton我们自定义的按钮即可。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值