1. 程式人生 > >New妙用和WinForm的Enabled=false時字型不可改變的解決方案

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我們自定義的按鈕即可。