首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# Visio 二次開發:動態仿真實現(三)——線路顏色變化與活塞動畫核心代碼

     編輯:關於C語言

線路顏色變化與動畫核心邏輯

private void RunColorChanging(Visio.Application visApp)
{
    Visio.Cell cell = visApp.ActiveDocument.Pages[1].PageSheet.get_Cells("Scratch.A1");
    int intValue = Convert.ToInt32(VisioUtility.FormulaStringToString(cell.Formula));
    
    if (intValue == 1) cell.Formula = "0";
    else { cell.Formula = "1"; isMovie = !isMovie; }
    
    int sequence = 1;
    foreach (int shapeId in AnalyzeShapeIdList)
    {
        Visio.Shape shape = VisDocument.Pages[1].Shapes.get_ItemFromID(shapeId);
        if (shape == null) continue;
        
        if (intValue == 0) // 通電/有油狀態
        {
            shape.Text = sequence++.ToString("D2");
            VisioUtility.SetShapeLineColor(shape, VisDefaultColors.visDarkGreen);
            Application.DoEvents();
            Thread.Sleep(500 * minFlowValue);
        }
        else // 斷電/無油狀態
        {
            shape.Text = "";
            VisioUtility.SetShapeLineColor(shape, VisDefaultColors.visBlack);
            Application.DoEvents();
        }
        
        // 活塞類設備動畫處理
        string equipType = VisioUtility.GetShapeCellValue(shape, "設備類型");
        if (!string.IsNullOrEmpty(equipType) && 
            (equipType == "單作用" || equipType == "雙作用"))
        {
            string minValue = "Width*0.25";
            string maxValue = "Width*0.75";
            string cellName = "Controls.Row_1.X";
            
            try
            {
                if (shape.get_CellExistsU(cellName, (short)VisExistsFlags.visExistsAnywhere) != 0)
                {
                    short i = shape.get_CellsRowIndex(cellName);
                    Visio.Cell typeCell = shape.get_CellsSRC(
                        (short)VisSectionIndices.visSectionControls, i, 
                        (short)VisCellIndices.visCtlX);
                    
                    if (intValue == 0) // 啟動活塞動畫執行緒
                    {
                        ThreadParameterInfo param = new ThreadParameterInfo();
                        param.Cell = typeCell;
                        param.ScratchCell = cell;
                        Thread thread = new Thread(new ParameterizedThreadStart(HuoSaiMoving));
                        thread.Start(param);
                    }
                    else // 重置活塞位置
                    {
                        typeCell.Formula = VisioUtility.StringToFormulaForString(minValue);
                        Application.DoEvents();
                    }
                }
            }
            catch (Exception ex) { LogHelper.Error(ex); }
        }
    }
}

關鍵技術點解析

  • Scratch 儲存格:作為全域狀態旗標(0/1 切換),控制動畫方向
  • 拓撲順序遍歷:依 AnalyzeShapeIdList 循序更新,呈現流體傳遞動畫
  • DoEvents + Sleep:強制 UI 刷新並控制動畫速度,實現逐幀顯示效果
  • 活塞多執行緒動畫:通電時啟動獨立執行緒循環移動控制點(Controls.Row_1.X),斷電時重置位置
  • 顏色語義化:綠色(有電/有油)、黑色(無電/無油)直觀反映狀態
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved