简体   繁体   English

vba 控制图像透明度可能吗?

[英]vba control image transparency possible?

I have an image in my worksheet that i want to fade out.我的工作表中有一个要淡出的图像。 To do this i am tying to find a way of setting different stages of transparency for the image like so:为此,我想找到一种为图像设置不同透明度阶段的方法,如下所示:

Set myPicture = ActiveSheet.Pictures.Insert(pic)

With myPicture
.Transparency = 0.5
Application.Wait (Now + TimeValue("00:00:01"))
.Transparency = 0.3
Application.Wait (Now + TimeValue("00:00:01"))
.Transparency = 0.1
Application.Wait (Now + TimeValue("00:00:01"))
.Delete
End With

At the moment i get a object not supported error message which leads me to believe this may not be possible.目前我收到一条不支持对象的错误消息,这让我相信这可能是不可能的。

If possible, please can somoene show me how to do this?如果可能,请somoene 告诉我如何做到这一点? Thanks谢谢

It took me a long time to get this to work (until I tried the DoEvents ) 我花了很长时间才能DoEvents工作(直到我尝试了DoEvents

Sub FadeInFadeOut()
    Dim r As Range
    Set r = Selection
    ActiveSheet.Shapes("Rectangle 1").Select
    Selection.ShapeRange.Fill.Transparency = 1

    For i = 1 To 100
        Selection.ShapeRange.Fill.Transparency = 1 - i / 100
        DoEvents
    Next

    For i = 1 To 100
        Selection.ShapeRange.Fill.Transparency = i / 100
        DoEvents
    Next

    r.Select
End Sub

It works on an AutoShape I place on the sheet. 它适用于我在图纸上放置的自选图形。

NOTE: 注意:

You must adjust the 100 to adjust the fade-in / fade-out speed. 您必须将100调整为淡入/淡出速度。

EDIT#1: 编辑#1:

Here is some junk code (based on the Recorder) for dropping an AutoShape on a sheet and filling it with a Picture: 这是一些垃圾代码(基于记录器),用于将自选图形放到图纸上并用图片填充:

Sub PicturePlacer()
    Dim sh As Shape

    ActiveSheet.Shapes.AddShape(msoShapeRectangle, 312.75, 176.25, 266.25, 129.75). _
        Select

    Selection.Name = "Sargon"

    Application.CommandBars("AutoShapes").Visible = False
    Range("G4").Select
    ActiveCell.FormulaR1C1 = "123"
    Range("G5").Select
    ActiveSheet.Shapes("Sargon").Select
    Selection.ShapeRange.Fill.Transparency = 0.56
    Selection.ShapeRange.Line.Weight = 0.75
    Selection.ShapeRange.Line.DashStyle = msoLineSolid
    Selection.ShapeRange.Line.Style = msoLineSingle
    Selection.ShapeRange.Line.Transparency = 0#
    Selection.ShapeRange.Line.Visible = msoTrue
    Selection.ShapeRange.Line.ForeColor.SchemeColor = 64
    Selection.ShapeRange.Line.BackColor.RGB = RGB(255, 255, 255)
    Selection.ShapeRange.Fill.Visible = msoTrue
    Selection.ShapeRange.Fill.ForeColor.RGB = RGB(255, 255, 255)
    Selection.ShapeRange.Fill.BackColor.RGB = RGB(255, 255, 255)
    Selection.ShapeRange.Fill.UserPicture "C:\Users\garys\Pictures\babies.jpeg"
End Sub

Remember to Name the Shape and use that Name in all the codes that reference that Shape. 记住要命名形状,并在所有引用该形状的代码中使用该名称。

I just ran across this excellent routine last week and tried it.上周我刚刚遇到了这个出色的例程并尝试了它。 The only downfall I noticed is that because the shape is Selected, when the For ... Next loops run the Selection Handles are visible on the shape.我注意到的唯一缺点是因为形状被选中,当 For ... Next 循环运行时,选择手柄在形状上可见。 I also saw the question posted by Princess.Bell: "is there anyway to add a background image to the shape?"我也看到了Princess.Bell 贴的问题:“有没有办法给形状添加背景图片?” I have an update to this posting which addresses both issues.我对这篇文章有一个更新,它解决了这两个问题。 I also slowed the fade in and fade out by adjusting the "timer" in the For ... Next loops from 100 to 250. This allows the fade in and fade out process to take place over 0.5 second.我还通过将 For ... Next 循环中的“计时器”从 100 调整到 250 来减缓淡入和淡出。这允许淡入和淡出过程发生在 0.5 秒以上。

Sub FadeInFadeOut()
    Dim shp As Shape
    Set shp = Sheets("Sheet1").Shapes.AddShape(Type:=msoShapeRectangle, _
              Left:=35, Top:=117, Width:=72.75, Height:=25.5)

    Dim i As Integer

    With shp.Fill
        .Visible = msoTrue
        .UserPicture FileName  '==> C:\Users\Me\AppData\Local\Temp\SavedImage.jpg (image file)
        For i = 1 To 250  'Fade in shape/picture.
        .Transparency = 1 - i / 250
            DoEvents
    Next
        For i = 1 To 250  'Fade out shape/picture.
        .Transparency = i / 250
        DoEvents
        Next
    End With
    shp.Delete  'Discard the shape now that we're done using it.
    Range("C3").Select  'Position cursor.
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM