嗨,我正在使用 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)";
上面的代码会抛出错误,因为单元格受到保护。
shape.get_CellsSRC((short)Visio.VisSectionIndices.visSectionObject,
(short)Visio.VisRowIndices.visRowFill,
(short)Visio.VisCellIndices.visFillBkgnd).FormulaForceU = "RGB(" + R + "," + G + "," + B + ")";
尝试了上面的方法,它没有抛出任何异常,但形状没有任何变化。
我已经尝试过这个解决方案 from stackoverflow而且它也不起作用
我可以在形状表FillForeGnd和FillBkGnd中看到我分配的值,但形状没有填充我指定的颜色。
谁能告诉我如何做到这一点..?
最佳答案
正如我在上面的答案中提到的(您已将其标记为无用),您的目标形状是错误的。
既然您已经编辑了问题以包含更多详细信息,那么您正在处理的形状就很清楚了。
您的目标形状似乎是“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;
}
关于c# - 在c#中用颜色填充visio的形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33139939/