如何更改按钮控件的默认背景颜色 [英] How to change default background color of button control

查看:410
本文介绍了如何更改按钮控件的默认背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对弱英语的评论。我在很多天里搜索goolge但我没有找到解决方案。我的问题如下。

当我从工具箱中绘制按钮到winform时,它的默认颜色就像灰色,悬停颜色就像橙色。

我的问题是我想要的更改按钮的默认背景颜色和按钮的悬停颜色。例如,每当我在winform上绘制新按钮时,背景颜色必须为蓝色,悬停颜色必须为绿色。当我在winform上绘制任何按钮时,背景颜色必须是蓝色,悬停颜色必须是绿色。

我希望你能理解我的问题。请帮帮我。

Appologise for weak english. I am searching in goolge from many days but i did not find solution. My question is below.
When i draw button from toolbox to winform, it,s default color is like gray and hover color is like orange.
My question is i want to change default background color of button and hover color of button also. For example whenever i draw new button on winform it,s background color must be blue and hover color must be green. When ever i draw any button on winform it,s background color must be blue and hover color must be green.
I hope you have understand my question. Please help me.

推荐答案

这是一个自定义按钮的例子:



Here is an example for customizing a Button :

Public Class customButtom
    Inherits Button

    Public Sub New()
        MyBase.BackColor = my_BackColor
    End Sub

    Shadows Property BackColor As Color
        Get
            Return my_BackColor
        End Get
        Set(value As Color)
            my_BackColor = value
            MyBase.BackColor = value
            Me.Invalidate()
        End Set
    End Property

    Private my_BackColor As Color = Color.Blue ' <- this is your Default-Value

    Property HoverBackColor As Color
        Get
            Return my_HoverBackColor
        End Get
        Set(value As Color)
            my_HoverBackColor = value
        End Set
    End Property

    Private my_HoverBackColor As Color = Color.Green ' <- this is your Default-Value




    Private Sub Button_MouseEnter(sender As Object, e As System.EventArgs) Handles Me.MouseEnter
        MyBase.BackColor = my_HoverBackColor
    End Sub

    Private Sub Button_MouseLeave(sender As Object, e As System.EventArgs) Handles Me.MouseLeave
        MyBase.BackColor = my_BackColor
    End Sub

End Class


默认背景颜色由控制面板控制,适用于所有应用程序。我认为你不想改变这个设置。因此,更改的范围应该在您的应用程序内。这意味着您需要通过代码控制背景颜色。



如果要更改表单内的控件,可以使用递归循环的方法表单中的所有控件。



该方法可能类似于

Default background color is controlled by the Control Panel and applies to all applications. I take it you don't want to change this setting. So the scope of the change should probably be inside your app. This means that you'd need to control the background color via code.

If you want to change the controls inside a form you can use a method which loops recursively through all the controls inside a form.

The method could look something like
/// <summary>
/// Changes recursively the color of all buttons inside provided control
/// </summary>
/// <param name="parentControl">Control whose controls to investigate</param>
public void ChangeBackgroundColor(System.Windows.Forms.Control parentControl) {
   // Loop through each control inside the given parent
   foreach (System.Windows.Forms.Control control in parentControl.Controls) {
      // Recursively update the color of the controls inside this control
      ChangeBackgroundColor(control);
      // If the contol is a button change the background color
      if (control.GetType() == typeof(System.Windows.Forms.Button)) {
         control.BackColor = System.Drawing.Color.Red;
      }
   }
}



调用方法如


And calling the method like

ChangeBackgroundColor(this);





希望这会有所帮助。



附加:



VB版可能看起来像



Hope this helps.

ADDITION:

VB version could look like

Private Sub ChangeBackgroundColor(ByRef parentControl As System.Windows.Forms.Control)
   ' Loop through each control inside the given parent
   For Each control As System.Windows.Forms.Control In parentControl.Controls
      ' Recursively update the color of the controls inside this control
      ChangeBackgroundColor(control)
      ' If the contol is a button change the background color
      If (TypeOf control Is System.Windows.Forms.Button) Then
         control.BackColor = System.Drawing.Color.Red
      End If
   Next
End Sub



和电话


and the call

ChangeBackgroundColor(Me)


这篇关于如何更改按钮控件的默认背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆