vb.net渐变色,vb颜色变化代码
vb.net怎么实现单击数字区域的按钮使按钮变红色,单击运算符按钮变蓝色同时数字区按钮还原?紧急在线等…
您好,这种方法可以通过按钮的MouseDown和MouseUp事件来实现,比如说,当单击按钮RedColor(RedColor为这个按钮的Name属性的属性值)的时候,要使按钮的颜色变成红色,这可以在代码视图中的控件选择下拉列表中选择RedColor这个控件,然后在右边的事件下拉列表中选择MouseDown事件(当按下鼠标时发生),这时,Visual Studio会自动生成如下代码:
兴和网站制作公司哪家好,找创新互联公司!从网页设计、网站建设、微信开发、APP开发、响应式网站设计等网站项目制作,到程序开发,运营维护。创新互联公司从2013年开始到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联公司。
Private Sub RedColor_MouseDown(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles RedColor.MouseDown
End Sub
接下来就在当前生成的代码中写入如下代码:
RedColor.BackColor = Color.FromArgb(255, 0, 0)
这句代码的作用是利用Color下的FromArgb方法来设置RGB颜色参数,在RGB颜色系统中,R代表红色,G代表绿色,B代表蓝色,根据这三种颜色的不同程度的调和,就会得到不同的颜色,如果您要得到深色的天蓝色,则可以设置FromArgb(0, 191, 255),这个颜色转换为HTML中的RGB颜色就是#00BFFF。
当然颜色还原最好是写在MouseUp事件里面,这样就不必在单击其他按钮时来还原上一次按下的按钮的颜色,在这里,为了方便描述,我们假设按钮的默认颜色为白色,也就是FromArgb方法设置为FromArgb(255, 255, 255)的时候,所标示的颜色就是白色
所以选择了RedColor按钮的MouseUp事件后,Visual Studio会自动生成下列代码:
Private Sub RedColor_MouseUp(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles RedColor.MouseUp
End Sub
MouseUp的含义是当鼠标弹起时发生,所以在这个事件就可以设置按钮颜色的还原,即添加下列代码来实现:
RedColor.BackColor = Color.FromArgb(255, 255, 255)
当然,这就编写好了一个按钮按下时变色,回弹时还原的功能,至于另外一个按钮的功能,这与这个按钮的功能一样,只是代码有一些细微的差别而已,假设这个按钮的Name属性的属性值为BlueColor,相应的代码对应如下:
Private Sub BlueColor_MouseDown(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles BlueColor.MouseDown
BlueColor.BackColor = Color.FromArgb(0, 0, 255) '蓝色的FromArgb
End Sub
Private Sub BlueColor_MouseDown(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles BlueColor.MouseDown
BlueColor.BackColor = Color.FromArgb(255, 255, 255)
End Sub
另外,为了避免代码的冗余,可以将RedColor和BlueColor的MouseUp事件进行合并,这合并后的代码如下:
Private Sub BlueColor_MouseDown(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles BlueColor.MouseDown, RedColor.MouseUp
BlueColor.BackColor = Color.FromArgb(255, 255, 255)
RedColor.BackColor = Color.FromArgb(255, 255, 255)
End Sub
当然,这个你只要分析了控件事件代码的基本结构,一切就一目了然了,控件事件代码的基本结构如下:
[作用域] Sub 事件名([参数列表]) Handles 控件事件1 [, 控件事件2, 控件事件3...]
[事件代码区域]
End Sub
如果此回答对您有帮助,别忘了采纳哦,如果没有解决您的问题,您可以在互联网上寻找更多关于此问题的解决方案,谢谢!
VB.NET如何实现图片渐显?
'这个拿去试一试,两个图片框,一个用来适应图片,一个用来适应窗体
'两个按钮控件,一个用来显示图片,一个用来隐藏图片
'两个时钟控件一个用来控制显示速度,一个用来选择图片
'图片文件夹自己建,放在文件夹当前目录下
'试过了,没问题.要还需要别的显示模式,提出要求
Dim Pic_num As Long
Dim Pic_Name() As String
Dim pic_star As Long
Dim p_width As Single
Dim p_height As Single
Dim bili_w As Single
Dim bili_h As Single
Private Sub Command1_Click() '加载并显示图片
If Pic_num 0 Then
Timer1.Enabled = True
Timer1.Interval = 2000
Else
MsgBox ("没有图片显示!")
End If
End Sub
Private Sub Command2_Click()
Picture1.Picture = Nothing '隐藏图片
Picture2.Cls
Timer1.Enabled = False
Timer2.Enabled = False
End Sub
Private Sub Form_Load()
Dim L_name As String
Pic_num = 0
ReDim Pic_Name(Pic_num)
L_name = Dir(App.Path "\pic\*.JPG")
Do While L_name ""
ReDim Preserve Pic_Name(Pic_num)
Pic_Name(Pic_num) = L_name
Pic_num = Pic_num + 1
L_name = Dir
Loop
L_name = Dir(App.Path "\pic\*.BMP")
Do While L_name ""
ReDim Preserve Pic_Name(Pic_num)
Pic_Name(Pic_num) = L_name
Pic_num = Pic_num + 1
L_name = Dir
Loop
Command1.Caption = "循环显示图片"
Command2.Caption = "隐藏图片"
Picture1.AutoSize = True
Picture1.AutoRedraw = True
Picture1.Visible = False
Picture2.AutoSize = False
Picture2.AutoRedraw = True
Picture2.Visible = True
Timer1.Interval = 10
Timer1.Enabled = False
Timer2.Interval = 50
Timer2.Enabled = False
If Pic_num 0 Then
Picture1.Picture = LoadPicture(App.Path "\pic\" Pic_Name(L_id))
Picture2.PaintPicture Picture1.Picture, 0, 0, Picture2.ScaleWidth, Picture2.ScaleHeight, 0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight
Else
MsgBox ("没有图片显示!")
End If
End Sub
Private Sub Timer1_Timer()
Dim L_id As Long
Randomize
L_id = Int(Pic_num * Rnd)
Picture1.Picture = LoadPicture(App.Path "\pic\" Pic_Name(L_id))
bili_w = Picture1.ScaleWidth / Picture2.ScaleWidth
bili_h = Picture1.ScaleHeight / Picture2.ScaleHeight
p_width = Picture2.Width / 100
p_height = Picture2.Height / 100
pic_star = 0
Timer1.Enabled = False
Timer2.Enabled = True
End Sub
Private Sub Timer2_Timer()
If pic_star 101 Then
pic_star = pic_star + 1
Picture2.PaintPicture Picture1.Picture, 0, 0, 100 * p_width, pic_star * p_height, 0, 0, bili_w * pic_star * p_width, bili_h * pic_star * p_height
Else
pic_star = 0
Timer1.Enabled = True
Timer2.Enabled = False
End If
End Sub
vb.net 如何使文字能渐变颜色,就是颜色慢慢变淡然后在慢慢恢复?
也容易,如果是黑白三个颜色加上相同的渐变量,彩色的是起始颜色的三个分量与终止颜色的对应三个分量的差值,再除于相同的份数,就得出三原色各自的步进量。
窗体上放个图片框试试下面代码:
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
Dim startColor As Color = Color.Red
Dim endColor As Color = Color.Green
Dim s As String = "vb.net 如何使文字能渐变颜色,就是颜色慢慢变淡然后在慢慢恢复?"
Dim Steps As Integer = s.Length \ 2
Dim StepR As Integer = (CInt(endColor.R) - startColor.R) \ Steps
Dim StepG As Integer = (CInt(endColor.G) - startColor.G) \ Steps
Dim StepB As Integer = (CInt(endColor.B) - startColor.B) \ Steps
Dim R As Integer = startColor.R
Dim G As Integer = startColor.G
Dim B As Integer = startColor.B
Dim drawFont As New System.Drawing.Font("Arial", 16)
Dim X As Integer = 50
For i As Integer = 1 To Steps
Dim drawBrush As New SolidBrush(Color.FromArgb(R, G, B))
e.Graphics.DrawString(s.Substring(i - 1, 1), drawFont, drawBrush, X, 50.0)
X += 18
R += StepR
G += StepG
B += StepB
Next
For i As Integer = 1 To Steps
Dim drawBrush As New SolidBrush(Color.FromArgb(R, G, B))
e.Graphics.DrawString(s.Substring(i + Steps - 1, 1), drawFont, drawBrush, X, 50.0)
X += 18
R -= StepR
G -= StepG
B -= StepB
Next
End Sub
文章标题:vb.net渐变色,vb颜色变化代码
转载注明:http://cdiso.cn/article/dssigod.html