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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41223436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 11:43:15  来源:igfitidea点击:

vba control image transparency possible?

excelimagevbatransparency

提问by Princess.Bell

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? Thanks

如果可能,请somoene 告诉我如何做到这一点?谢谢

回答by Gary's Student

It took me a long time to get this to work (until I tried the 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 100to 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.

请记住命名形状并在所有引用该形状的代码中使用该名称。