如何通过C#获取MS Visio中的形状对象的颜色

问题描述 投票:0回答:1

我有一个形状对象绘制为“组”,这样两个子形状是该组的直接子项。该组中的所有形状都有不同的颜色。

我想知道什么是可以帮助我获得形状对象(红色,绿色,白色)颜色的属性。

我知道这个形状有样式属性(Shape.Style),但是它没有给我颜色值。

enter image description here

Application visApp = new Application();

Document visDoc = visApp.Documents.Open(VisiofilePath);

var shp = visApp.ActivePage.Shapes.ItemFromID[1];

string shapeColor = string.Empty;

foreach (Visio.Shape s in shp.Shapes)
{
    if(s.Text == "Child Object 1")
     {
        //shapeColor = 
     }

     if(s.Text == "Child Object 2")
     {
        //shapeColor = 
     }        
}

任何帮助都感激不尽。

c# interop visio flowchart
1个回答
1
投票

获取填充颜色不受形状是否是组的一部分的影响。一旦你得到了正确形状的参考,你就可以看到各自的细胞。

Visio有两种设置填充颜色的主要方法 - 图案填充和渐变填充。后者是从2013年开始。

对于模式填充,您正在查看三个单元格:FillForegndFillBkgndFillPattern。大多数形状从固体填充(FillPattern 1)开始,这意味着只使用FillForegnd。对于其他模式类型,您要处理FillForegndFillBkgnd

对于渐变填充,FillGradientEnabled单元格设置为1,这导致Fill Gradient Stops部分先例。

在后台,Visio维护着Document.Colors系列。一些内置颜色可以通过索引访问:0 =黑色,1 =白色,2 =红色,3 =绿色等一直到23个。使用的任何其他自定义颜色都会添加到集合中,并且还会给出索引。这意味着,给定索引,您可以在Colors集合中查找颜色实例。

以下是一些代码,演示如何访问各种类型的着色。鉴于这四种形状:

enter image description here

前3个形状使用Pattern填充,而最后3个使用渐变填充。

  • Sheet.1使用索引单元格公式(3),
  • Sheet.2使用RGB功能,
  • Sheet.3使用模式(2),因此使用前景和背景单元格
  • Sheet.4使用渐变停止,因此前景和背景单元格将被忽略

...你可以使用下面的代码来读取工作中的颜色(注意这是使用LINQPad作为输出窗口使得更清楚地看到发生了什么:

void Main()
{
    var vApp = MyExtensions.GetRunningVisio();

    for (int i = 1; i <= 4; i++)
    {
        var shp = vApp.ActivePage.Shapes.ItemFromID[i];
        var colorInfos = new List<ColorInfo>();
        colorInfos.Add(new ColorInfo(shp.CellsU["FillForegnd"]));
        colorInfos.Add(new ColorInfo(shp.CellsU["FillBkgnd"]));
        new
        {
            shp.NameID,
            FillPattern = shp.CellsU["FillPattern"].ResultIU,
            FillGradientEnabled = Convert.ToBoolean(shp.CellsU["FillGradientEnabled"].ResultIU),
            PatternColors = colorInfos,
            GradientColors = FillGradientColors(shp) ?? "Default (10 stops all white)"
        }.Dump();
    }
}

private dynamic FillGradientColors(Visio.Shape shp)
{
    List<string> rgbs = null;
    var iSect = (short)Visio.VisSectionIndices.visSectionFillGradientStops;
    for (int i = 0; i < shp.RowCount[iSect]; i++)
    {
        var targetCell = shp.CellsSRC[iSect, (short)i, (short)Visio.VisCellIndices.visGradientStopColor];
        if (targetCell.IsInherited == 0)
        {
            if (rgbs is null)
            {
                rgbs = new List<string>();
            }
            rgbs.Add(ColorInfo.RgbString(targetCell));
        }
    }   
    return rgbs;    
}


public class ColorInfo
{
    private Visio.Cell _vCell;

    public ColorInfo(Visio.Cell vCell)
    {
        _vCell = vCell;
        RGB = RgbString(_vCell);
    }

    public string Name => _vCell.Name;
    public string RGB { get; set; }
    public string FormulaU => _vCell.FormulaU;

    public static string RgbString(Visio.Cell cell)
    {
        var colorIdx = cell.Result[(short)Visio.VisUnitCodes.visUnitsColor];
        var c = cell.Document.Colors.Item16[(short)colorIdx];
        return $"RGB({c.Red},{c.Green},{c.Blue})";
    }
}

...这会产生以下输出:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.