更改按钮边框和禁用的外观

时间:2012-05-02 06:59:18

标签: c# winforms custom-controls

我的表格中有2个按钮。两者都有背景图片,平面样式是“平面”。

  1. 点击 TAB 键时,当焦点在按钮上时,我看到内部是纯色矩形,而不是实心边框我想在按钮上有虚线边框。

  2. 单击该按钮时,某些活动正在进行,并且该按钮被禁用。禁用时,按钮文本颜色变为灰色。我不希望它改变文字颜色。我相信这是Windows标准行为,但是如何在选择按钮时不更改文本颜色?

  3. 我找不到任何设置这些点的属性。我该怎么做才能实现目标。任何帮助都非常感谢。

    我还创建了一个自定义按钮,但无法摆脱上述问题。

    我的自定义按钮的绘画方法:

       public partial class MyButton : Button  {
    
        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);
            // Set custom fore color    
            base.ForeColor = Color.FromArgb(0, 255, 254, 255);    
    
            // set the same forecolor even if the button is disabled  
            if (base.Enabled == false)
            {
                base.ForeColor = Color.FromArgb(0, 255, 254, 255);
            }
    
        }  //OnPaint method ends
      }  // class ends
    

    (1)&更新的解决方案(2) 在onpaint()中添加以下内容:

        private void init()
        {
            base.ForeColor = Color.FromArgb(0, 255, 254, 255);
            base.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 254);   // Transparent border
        }
    
        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);
    
            if (base.ContainsFocus)
            {
                // Draw inner dotted rectangle when button is on focus
                Pen pen = new Pen(Color.Gray, 3);
                Point p = base.Location;
                pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
                Rectangle rectangle = new Rectangle(4, 4, Size.Width - 8, Size.Height - 8);
                ControlPaint.DrawFocusRectangle(pevent.Graphics, rectangle);
            }
    
            // Draw the string to screen
            SizeF sf = pevent.Graphics.MeasureString(displayText, this.Font, this.Width);
            Point ThePoint = new Point();
            ThePoint.X = (int)((this.Width / 2) - (sf.Width / 2));
            ThePoint.Y = (int)((this.Height / 2) - (sf.Height / 2));
            //pevent.Graphics.DrawString(displayText, Font, new SolidBrush(this.ForeColor), ThePoint);
            //pevent.Graphics.DrawString(displayText, Font, new SolidBrush(Color.FromArgb(0, 255, 254, 255)), ThePoint);
            pevent.Graphics.DrawString(displayText, Font, new SolidBrush(Color.Black), ThePoint);
            //this.Text = "";
         }//OnPaint ends
    
        // Avoids the inner solid rectangle shown on focus
        protected override bool ShowFocusCues
        {
            get
            {
                return false;
            }
        }
    

    这成功地在我的按钮中绘制了一条虚线,表示焦点。为了摆脱默认的实线,我覆盖了ShowFocusCues()。

    这帮助我完全解决了1。但是对于第2点,没有得到100%的结果。我在MyButton类中创建了一个属性“displayText”。在OnPaint()中,如果我将ForeColor,Color.FromARGB提供给SolidBrush,它不会显示任何文本。但是,如果我传递Color.Black,那么它接受它并以黑色显示文本。为什么它不接受ForeColor,或自定义颜色,只接受默认颜色'?这个简单的任务我在哪里仍然出错。我通过在表单中​​实现button_paint尝试了同样的方法,但我看到了相同的结果。

    知道我哪里还会出错?

1 个答案:

答案 0 :(得分:1)

     private void button3_Paint(object sender, PaintEventArgs e)
            {

                SolidBrush br = new SolidBrush(Color.Blue);
                Pen pen = new Pen(br);
                pen.Width = 3;
                pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
                  // to draw left border
                e.Graphics.DrawLine(pen, new Point(0, 0), new Point(0, this.button3.Width));
                SolidBrush drawBrush = new SolidBrush(ForeColor); //Use the ForeColor property
                // Draw string to screen.
                e.Graphics.DrawString("Sample", Font, drawBrush, 5f, 3f);
                this.button3.Text = "";
    }