2

嗨,我正在使用 c# 创建 visio2013 的形状。现在我需要使用 c# 用一些颜色填充形状我尝试了以下代码,但没有任何意义:( :(

   Visio.Cell cqellname;
            cqellname = shape.get_CellsSRC(
            (short)Visio.VisSectionIndices.visSectionObject,
            (short)Visio.VisRowIndices.visRowFill,
            (short)Visio.VisCellIndices.visFillBkgnd);
            cqellname.FormulaU = "rgb(255,0,0)";

上面的代码会抛出一个错误,因为Cell 是 Guarded。

  shape.get_CellsSRC((short)Visio.VisSectionIndices.visSectionObject,
        (short)Visio.VisRowIndices.visRowFill,
   (short)Visio.VisCellIndices.visFillBkgnd).FormulaForceU = "RGB(" + R + "," + G + "," + B + ")";

尝试了上述方法,它不会引发任何异常,但形状没有任何变化。

我已经从stackoverflow尝试过这个解决方案,但它也不起作用

我可以在 shapesheet FillForeGnd 和 FillBkGnd中看到我分配的值,但形状没有填充我给的颜色。

在此处输入图像描述

任何人都可以告诉我如何做到这一点..?

4

2 回答 2

3

正如我在上面的答案中提到的(您已将其标记为无用),您的目标是错误的形状。

既然您已经编辑了您的问题以包含更多详细信息,那么您正在处理的形状很清楚。

您定位的形状似乎是“BPMN 基本形状”模具中的“折叠子流程”。这是一个组形状,顶层没有几何形状,所以改变它的颜色,就像你在你的问题中所做的那样,没有视觉上的变化。要解决这个问题,您需要找到用于显示背景的子形状。碰巧在这个特定的母版中,包含您需要定位的填充的子形状的索引比父形状大一,因此您可以在代码中使用它。该形状缺少任何其他清晰的特征(例如用户单元格),这些特征会成为更好的候选者,因此请注意,此方法仅适用于该特定形状。

鉴于您似乎正在使用此模板进行大量工作,我的方法是创建模板的副本并对母版进行一些修改以使这种交互更容易一些,但我希望同时以下代码回答了您的问题。

如果是这样,请将其标记为已回答。

public void OnCheckFillBPMN()
{
    Color fillColor = Color.FromArgb(1, 255, 0, 0);
    CollapsedSubProcessFill(this.Application.ActiveWindow.Selection.PrimaryItem, fillColor);
}

private void CollapsedSubProcessFill(Visio.Shape vShpIn, Color fillColor)
{
    if (vShpIn != null && vShpIn.Master != null && vShpIn.Master.NameU.StartsWith("Collapsed Sub-Process"))
    {
        //The sub-shape that provides the fill colour in 
        //the 'Collapsed Sub-Process' master is the first index
        //after the group shape. 
        //If you want to use a different method to locate the 
        //correct sub-shape then do that here.
        var targetSubShpId = vShpIn.ID + 1;
        var targetShp = TryGetShapeInCollection(vShpIn.Shapes, targetSubShpId);
        if (targetShp != null)
        {
            var targetCell = targetShp.get_CellsSRC(
                (short)Visio.VisSectionIndices.visSectionObject,
                (short)Visio.VisRowIndices.visRowFill,
                (short)Visio.VisCellIndices.visFillForegnd);
            targetCell.FormulaU = "RGB(" + fillColor.R
                                    + ',' + fillColor.G
                                    + ',' + fillColor.B + ')';
        }
    }
}


private Visio.Shape TryGetShapeInCollection(Visio.Shapes vShps, int shpId)
{
    try
    {
        if (vShps != null)
        {
           var targetShp = vShps.ItemFromID[shpId];
           return targetShp; 
        }  
    }
    catch (System.Runtime.InteropServices.COMException ex)
    {
        if (ex.ErrorCode == -2032465756) //Invalid sheet identifier
        {
            return null;
        }
    }
    return null;
}
于 2015-10-20T11:09:02.537 回答
0

如果 FormulaForceU 有效,但您没有视觉上的变化,那么我认为您没有设置正确的单元格。通常您会设置 FillForegnd 单元格 ( visFillForegnd),除非设置了不同的模式。另请注意(对于 Visio 2013+)如果FillGradientEnabled设置为 true,这将覆盖纯色。

最后要记住的一件事是,您要定位的形状可能没有几何形状,或者它的NoFill单元格设置为 true,实际上您应该定位一个子形状/子形状。

无论哪种情况,您都应该打开 ShapeSheet并查看形状处于什么状态。

于 2015-10-15T14:55:35.820 回答