【问题标题】:VB.NET Fading out button properlyVB.NET 淡出按钮正确
【发布时间】:2015-06-10 12:49:26
【问题描述】:

如何正确淡出/淡入 VB.NET 中的按钮?我可以使用以下方法淡出/淡入标签:

Controls(i).ForeColor = Color.FromArgb(255, alpha, alpha, alpha)

(其中Controls(i) 是来自For Next 循环的标签,通过Me.Controls 中的所有控件循环;alpha 是RGB 的值,也来自For Next 循环)。

这对我来说对按钮有用,因为更改 ForeColor 会使其余按钮的 UI 可见!

因此,我尝试使用保存的按钮的Resource 图像(来自屏幕截图)并创建淡入/淡出版本以显示为PictureBox 中的图像:

Public Function SetImageOpacity(ByVal imgPic As Image, ByVal imgOpac As Double) As Image
    Dim bmpPic As New Bitmap(imgPic.Width, imgPic.Height)
    Dim grPic As Graphics = Graphics.FromImage(bmpPic)
    Dim imgAtt As New ImageAttributes()
    Dim cmxPic As New ColorMatrix()
    cmxPic.Matrix33 = imgOpac
    imgAtt.SetColorMatrix(cmxPic, ColorMatrixFlag.[Default], ColorAdjustType.Bitmap)
    grPic.DrawImage(imgPic, New Rectangle(178, 144, bmpPic.Width, bmpPic.Height), imgPic.Width, imgPic.Height, imgPic.Width, imgPic.Height, GraphicsUnit.Pixel, imgAtt)
    grPic.Dispose()
    imgAtt.Dispose()
    Return bmpPic
End Function

(其中imgPic 是按钮的Resource 图像,imgOpac 是 1 到 0 的不透明度,其中 0 是透明的)。

然后我使用这张图片来设置我的PictureBox 的图片:

picbox.Image = SetImageOpacity(My.Resources.Nextbutton, 1)

但是,我得到一个小故障,即使PictureBox 位于坐标178, 144,它显示的图像显示在表单的左边缘(即错误的 X 坐标),Y 坐标正确!

我觉得这可能与我对 .DrawImage(...) 的调用有关(在函数的第 8 行) - 但 the MSDN docs on this subject 对我来说非常不清楚。

如果以前有人问过,请链接!

【问题讨论】:

    标签: vb.net winforms button picturebox fade


    【解决方案1】:

    Graphics grPic 中的坐标是相对于图像本身的客户端 坐标,而不是相对于窗体的坐标。所以 (0, 0) 是图像的左上角,无论该图像最终在某种控件中显示在哪里。

    尝试将其更改为 (0, 0):

    grPic.DrawImage(imgPic, New Rectangle(0, 0, bmpPic.Width, bmpPic.Height), imgPic.Width, imgPic.Height, imgPic.Width, imgPic.Height, GraphicsUnit.Pixel, imgAtt)
    

    【讨论】:

    • 非常感谢,我去试试!
    【解决方案2】:

    为什么不直接使用 for 循环将按钮控件的不透明度以 0.01 为增量从 1 更改为 0?

    【讨论】:

    • 正如我在回答中解释的那样,这并没有按我的预期工作 - @Idle_Mind 用我的 SetImageOpacity 子解释了这个问题。无论如何感谢您的回答!
    猜你喜欢
    • 2015-03-31
    • 2021-03-31
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多