2017-09-10 135 views
-2

什么即时试图做的,是通过所有可能的颜色改变窗口2BackColor(上复选框的点击),以平稳淡出,所以从RGB:0,0, 0一直到255,255,255。谢谢。遍历所有可能的RGB组合

这是即时通讯做什么:

Private Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox2.CheckedChanged 
    Dim lngR As Long 
    Dim lngG As Long 
    Dim lngB As Long 

    For lngR = 0 To 255 
     For lngG = 0 To 255 
      For lngB = 0 To 255 
       Me.BackColor = RGB(lngR, lngG, lngB) 
       Sleep(30) 'change this value to change speed 
      Next lngB 
     Next lngG 
    Next lngR 
End Sub 

和错误即时得到的是:Value of Integer cannot be converted to Color.

+2

因为你不是在说“这就是我想做的事,我该怎么做”。这是一个地方,你说“这就是我想要做的,这就是我试图做到这一点,这是当我尝试时会发生什么,我该如何解决它”。如果你还没有做过适当的研究并做出了尝试,那么在这里张贴是不成熟的。 – jmcilhinney

+0

我的错误,对不起。更新了我的帖子。 – csAlphyy

+0

你的嵌套循环开始了。使用一个循环并将每个rgb组件设置为相同的值。 –

回答

0

我通过改变背景色后加入刷新()得到的颜色变化。这会减慢循环,看到颜色变化的程度,但是循环约为1660万次(如果您不是一直重新绘制,而是重新绘制非常慢),并且会导致用户入睡。

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click 
     Dim intR As Int32 
     Dim intG As Int32 
     Dim intB As Int32 
     'Alpha channel is implicitly opaque (255)) 
     Dim x As Integer = 0 
     For intR = 0 To 254 Step 20 
      For intG = 0 To 254 Step 20 
       For intB = 0 To 254 Step 20 
        Threading.Thread.Sleep(10) 'change this value to change speed, 500 is 1/2 second 
        '16.6 million loops Wow - cut that down by using Step 
        Me.BackColor = Color.FromArgb(intR, intG, intB) 
        Refresh() 
        x += 1 
       Next 
      Next intG 
     Next intR 
     MessageBox.Show($"Red = {intR}, Green = {intG}, Blue = {intB}{vbCrLf}The value of x is {x}") 
    End Sub