如何在控件中设置颜色渐变的颜色 [英] How to set the colors of a color gradient in a control

查看:87
本文介绍了如何在控件中设置颜色渐变的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个应用程序,其中TabPage的颜色由颜色渐变组成,并且将有多个按钮设置TabPage的颜色.我希望单击按钮以更改TabPage的颜色时,这是下面的代码,但是单击按钮时颜色不会更改.任何人都可以分辨出问题所在,或者该怎么办?


I am creating an application in which the color of TabPage is composed of a color gradient, and there will be several buttons that set the color of TabPage. I want that when the button is clicked to change the color of TabPage, this is my code below but when I click the button color is not changed. Anyone could tell what is wrong, or how can I do this?


Dim cor1 As Color = Color.SkyBlue
  Dim cor2 As Color = Color.Lime
  Private Sub TabPageExecutarPrograma_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles TabPageExecutarPrograma.Paint
    Dim FormGraphics As Graphics = e.Graphics
    Dim GradientBrush As Brush
    GradientBrush = New LinearGradientBrush(New Point(0, 0), New Point(0, Me.Height), cor1, cor2)
    FormGraphics.FillRectangle(GradientBrush, ClientRectangle)
  End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
   cor1 = Color.Blue
   cor2 = Color.Green
End Sub

推荐答案

我假设cor1cor2是控制形式的成员,所以您在按钮单击和Paint处理程序之间共享这些值.

都正确,但是……您认为控件如何知道何时更改颜色? 缺少的部分是:单击一个按钮,更改颜色后也会调用 TabPageExecutarPrograma.Invalidate.

无效是一种机制,它通过发送WM_PAINT事件来触发要渲染的区域或矩形而触发渲染的机制(没有参数意味着使整个工作区无效).这是实现,动画,交互式绘图以及图形中任何其他动态更改的方式.

—SA
I assume cor1 and cor2 are members of the Form of Control, so you share these values between button click and Paint handler.

All correct, but… how do you think control knows when the colors are changed? The missing piece is: one button click, after the colors are changed also call TabPageExecutarPrograma.Invalidate.

Invalidation is the mechanism that trigger rendering via sending the WM_PAINT event with the region or rectangle to be invalidated (no parameters means to invalidate full client area). This is the way to implement, animation, interactive drawing and any other dynamic changes in graphics.

—SA


这篇关于如何在控件中设置颜色渐变的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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