【问题标题】:How to compare colors in vb.net如何在 vb.net 中比较颜色
【发布时间】:2014-05-06 03:14:07
【问题描述】:

我正在使用 Vb.Net Forms 应用程序做一些 UI 工作,在一个我需要匹配一些颜色并根据它更改 UI 的地方,目前我很少匹配颜色值,

像 A_Control.Background = B_Control.Background

我已经完成了将颜色转换为字符串然后进行匹配的工作,但在某些情况下这并不有效,我想实现一个直接与颜色变量一起工作的函数。

喜欢:

Dim somecolor As New Color()
somecolor = Color.FromArgb(255, 98, 153, 192)
Dim str As String = somecolor.R.ToString() + somecolor.G.ToString() + somecolor.B.ToString()

通过这样做,我可以在字符串中获得 RGB 值,然后我可以转换为整数以匹配任何其他颜色,有没有更好的方法来做到这一点?

【问题讨论】:

    标签: vb.net string winforms colors


    【解决方案1】:

    这是我测试过的简单解决方案..

    Public Function MatchColor() As Boolean
    Dim cl As New Color()
    'color to match
    cl = Color.FromArgb(255, 98, 153, 192)
    If Me.BackColor.Equals(cl) Then
        ' match the color with current forms background // you can use any
        Return True
    Else
        Return False
    End If
    End Function
    

    这里是函数调用

    Private Sub button3_Click(sender As Object, e As EventArgs)
    Dim match As Boolean = MatchColor()
    End Sub
    

    希望这对你有帮助。

    【讨论】:

      【解决方案2】:

      您需要一个带有计时器(时间)和彩色对话框(cdcolor)的表单。开始:

      Imports System.Drawing
      Public Class Form1
          Dim ccolor1 As Color = Color.(pick your color)
          Dim ccolor2 As Color = Color.(pick your color)
          Dim t = 0
      

      之后,您进行颜色更改:

      Private Sub time_Tick(sender As Object, e As EventArgs) Handles time.Tick
              t += 1
              Select Case t
                  Case 0
                      Me.BackColor = ccolor1
                  Case 1
                      Me.BackColor = ccolor2
                  Case 2
                      Me.BackColor = ccolor3
                  Case 3
                      t = 0
              End Select
          End Sub
      

      【讨论】:

        猜你喜欢
        • 2012-03-14
        • 2013-02-22
        • 2015-11-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-23
        • 2012-09-05
        相关资源
        最近更新 更多