【问题标题】:How to add a triangle marker to any cell corner of SpreadsheetGear grid?如何在 SpreadsheetGear 网格的任何单元格角落添加三角形标记?
【发布时间】:2012-12-11 02:09:46
【问题描述】:

这是一个 SpreadsheetGear Grid 特定问题。我知道您可以向单元格添加评论,并且该单元格会自动在右上角获得红色三角形标记。但我需要在任何单元格角落添加一个小三角形(不同颜色)以表示一些特别的东西。有可能吗?

UPATE:这是我根据 Daniel 的建议在单元格的任何角落添加三角形的结果。

    public void AddTriangleShapeToCorner(IWorksheet worksheet, int row, int col, CellCorners cellCorner)
    {
        const double width = 5, height = 5;
        double xOffset = 0, yOffset = 0;

        IWorksheetWindowInfo windowInfo = worksheet.WindowInfo;

        if (cellCorner == CellCorners.TopRight || cellCorner == CellCorners.BottomRight)
        {
            col++;
            xOffset = width;
        }
        if (cellCorner == CellCorners.BottomLeft || cellCorner == CellCorners.BottomRight)
        {
            row++;
            yOffset = height;
        }
        double top = windowInfo.RowToPoints(row) - yOffset;
        double left = windowInfo.ColumnToPoints(col) - xOffset;

        IShape shape = worksheet.Shapes.AddShape(AutoShapeType.RightTriangle, left, top, width, height);
        shape.Line.Visible = false;         // line at top-left corner is not sharp, so turn it off.
        shape.Placement = Placement.Move;   // make the shape move with cell. NOTE: it doesn't work for top-right and bottom-right corners.
        if (cellCorner == CellCorners.TopLeft || cellCorner == CellCorners.TopRight)
            shape.VerticalFlip = true;
        if (cellCorner == CellCorners.TopRight || cellCorner == CellCorners.BottomRight)
            shape.HorizontalFlip = true;
    }

【问题讨论】:

    标签: grid spreadsheetgear


    【解决方案1】:

    您可以使用 IShapes AddShape 方法。对于类型,您可以使用 AutoShapeType.RightTriangle。

    这是一个例子:

    private void AddTriangleShape(SpreadsheetGear.IWorksheet worksheet, int iRow, int iCol)
    {
      SpreadsheetGear.IWorksheetWindowInfo windowInfo = worksheet.WindowInfo;
    
      // Calculate the left, top, width and height of the button by 
      // converting row and column coordinates to points.  Use fractional 
      // values to get coordinates in between row and column boundaries.
      double left = windowInfo.ColumnToPoints(iCol);
      double top = windowInfo.RowToPoints(iRow + 0.5);
      double right = windowInfo.ColumnToPoints(iCol + 0.1);
      double bottom = windowInfo.RowToPoints(iRow + 0.9);
      double width = right - left;
      double height = bottom - top;
    
      worksheet.Shapes.AddShape(SpreadsheetGear.Shapes.AutoShapeType.RightTriangle, left, top, width, height);
    }
    

    【讨论】:

    • 非常感谢您提供此解决方法。它可以工作,但有两个问题: 1. 可以选择、移动或删除三角形。我想禁用所有这些操作。形状上有一个 Locked 属性,但它仅在工作表被锁定时才有效,这不是我想要的。 2. 形状固定在它所在的单元格上。如果将三角形设置为右上角,并将列的大小调整到右侧,形状将不会移动。您知道如何解决这些问题吗?
    • AddTriangleShapeToCorner 方法做得很好!对于您的两个问题,我没有任何很好的解决方案,但是我有一些想法可以帮助您考虑解决方案。对于问题 1,WorkbookView 类有一个名为 ShapeSelectionChanging 的事件,您可以在其中取消选择 (e.Cancel = true;)。这将阻止形状被选择或删除,但不幸的是它不会阻止形状被移动。
    • 对于问题 2,WorkbookView 类还有一个名为 RangeChanged 的​​事件,您可以在其中获取列宽 (e.Range.ColumnWidth)。使用它,您可以尝试移动该列中的 RightTriangle 对象。
    • 对于第 2 期,我的想法与您的想法相似,现在可以使用了。对于问题 1,您的 ShapeSelectionChanging 事件提示确实有帮助。正如您所指出的,剩下的唯一问题是用户仍然可以移动形状。不幸的是,Shape 对象不公开任何事件。非常感谢您的大力帮助。
    猜你喜欢
    • 1970-01-01
    • 2020-09-12
    • 2018-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    • 1970-01-01
    相关资源
    最近更新 更多