MSVisio字符自定义颜色

时间:2019-02-21 08:07:57

标签: c# .net vba visio

我知道,我可以通过以下方式更改形状中所有文本的颜色:

Shape.CellsU["Char.Color"].FormulaForceU = "RGB(255,255,255)";

还有一种方法可以通过这种方式更改某些字符的颜色:

Characters.Begin = 2;
Characters.End = 5;    
Characters.set_CharProps((short)MSVisio.VisCellIndices.visCharacterColor, (short)MSVisio.VisDefaultColors.visRed;);

但是我找不到任何将自定义RGB(或HEX或任何类型)颜色值传递给特定形状的字符的方法。我不想把形状切成小形状。

可以帮忙吗?谢谢

1 个答案:

答案 0 :(得分:1)

通过CharProps属性,您可以为预定义的文档颜色集合设置索引。对于自定义RGB,您可以通过首先使用CharPropsRow获取行索引来设置相应的单元格公式,如下所示:

    var shp = vApp.ActiveWindow.Selection.PrimaryItem;
    var shpChars = shp.Characters;

    shpChars.Begin = 2;
    shpChars.End = 5;
    //shpChars.set_CharProps((short)Visio.VisCellIndices.visCharacterColor, (short)Visio.VisDefaultColors.visRed);

    var targetRow = shpChars.CharPropsRow[0];
    shp.CellsSRC[(short)Visio.VisSectionIndices.visSectionCharacter, 
                 targetRow,
                 (short)Visio.VisCellIndices.visCharacterColor].FormulaU = "RGB(40,220,40)";

,这应该会给您类似的结果: enter image description here

[更新]上面假设您要定位和更改现有格式。要添加新运行,可以先使用CharProps添加行,然后使用CharPropsRow来定位该新运行。因此,您可以在新页面上运行此代码:

    var vPag = vApp.ActivePage;
    var shp = vPag.DrawRectangle(3,3,5,4);
    shp.Text = "GoodMorning";

    var shpChars = shp.Characters;

    shpChars.Begin = 0;
    shpChars.End = 4;
    var targetRow = shpChars.CharPropsRow[(short)Visio.VisCharsBias.visBiasLetVisioChoose];
    shp.CellsSRC[(short)Visio.VisSectionIndices.visSectionCharacter,
                 targetRow,
                 (short)Visio.VisCellIndices.visCharacterColor].FormulaU = "RGB(220,40,40)";

    shpChars.Begin = 4;
    shpChars.End = 11;
    shpChars.set_CharProps((short)Visio.VisCellIndices.visCharacterColor, (short)Visio.VisDefaultColors.visBlack);
    targetRow = shpChars.CharPropsRow[(short)Visio.VisCharsBias.visBiasLetVisioChoose];
    shp.CellsSRC[(short)Visio.VisSectionIndices.visSectionCharacter,
                 targetRow,
                 (short)Visio.VisCellIndices.visCharacterColor].FormulaU = "RGB(40,200,40)";

...这将导致以下结果:

enter image description here