Visio 組合形狀預設只能整體選取操作。需對子形狀單獨移動(如換向閥左右切換動畫)時,關鍵在於 GroupSelectMode Cell:0=只選群組、1=先選群組再選子形狀、2=直接選子形狀。本文示範透過切換此屬性實現子形狀位移動畫。
在 ShapeSheet 的 Group Properties 區段中,GroupSelectMode 決定選取行為:
以巨集錄製驗證:設為 2 後,Selection.SubSelect 可單獨操作子形狀。
[CLSCompliant(false)]
public static void SetGroupSelectMode(Shape targetShape, int selectMode) {
targetShape.CellsSRC[
(short)VisSectionIndices.visSectionObject,
(short)VisRowIndices.visRowGroup,
(short)VisCellIndices.visGroupSelectMode
].FormulaU = selectMode.ToString();
}
else if (equipType == "4/2閥") {
// 1. 切換至子形狀選取模式
SetGroupSelectMode(shape, 2);
// 2. 遍歷子形狀,依設備類型判斷移動方向
foreach (Shape subShape in shape.Shapes) {
string subType = GetShapeCellValue(subShape, "設備類型");
if (subType == "閥A" || subType == "閥B") {
subShape.Text = subType; // 顯示標識
if (isPowerOff) continue; // 斷電時不動畫
try {
VisWindow.DeselectAll();
VisWindow.Select(subShape, (short)VisSelectArgs.visSubSelect);
if (subType == "閥A") {
VisWindow.Selection.Move(0.393701, 0, "inches"); // 右移
} else if (subType == "閥B") {
VisWindow.Selection.Move(-0.393701, 0, "inches"); // 左移
}
DoEventsSleep(50);
} catch {
VisWindow.DeselectAll();
}
}
}
// 3. 還原群組選取模式
SetGroupSelectMode(shape, 0);
}
設備類型 = "閥A" / "閥B",作為程式碼識別依據。inches,數值需經實測調整以對齊視覺效果。try/catch/finally 保證 DeselectAll 與模式還原一定執行,避免殘留選取狀態影響後續操作。同理可實現:摺疊臂展開/收合、多段管道順序通氣、機械手臂關節運動等所有「組合圖形內部結構變化」動畫。核心模式:SetGroupSelectMode(2) → 子形狀操作 → SetGroupSelectMode(0)。
此技術打破「群組不可拆分」的認知限制,讓 Visio 組合形狀具備骨骼動畫般的表現力。