【问题标题】:Paint gradient for multiple cells in TableLayoutPanel在 TableLayoutPanel 中为多个单元格绘制渐变
【发布时间】:2014-03-25 22:58:32
【问题描述】:

我喜欢在 tablelayoutpanel 中绘制多个单元格。我知道如何将行和列绘制为下面的普通代码:

private void tableLayoutPane1_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
{
  if (e.Row == 3 || e.Row == 4 || e.Row == 5)
  { 
    LinearGradientBrush brush = new LinearGradientBrush(e.CellBounds, Color.White,   Color.Transparent, 90F);
    e.Graphics.FillRectangle(brush, e.CellBounds);
  }
}

但我想为 3 个单元格或行设置一个矩形渐变,另一方面为 3 个单元格或多个单元格设置一个合并渐变。

【问题讨论】:

    标签: c# winforms tablelayoutpanel


    【解决方案1】:

    这可以通过使用 Paint 事件而不是 CellPaint 事件来完成。诀窍是找出表格中每个单元格的位置。 Intellisense 隐藏了两个函数(GetRowHeights()GetColumnWidths()),它们可以获取行高和列宽,因此您可以自己计算位置:

    void tlp_Paint(object sender, PaintEventArgs e) {
      int[] rowHeights = tlp.GetRowHeights();
      int[] colmWidths = tlp.GetColumnWidths();
    
      int boxLeft = 0;
      int boxTop = 0;
      int boxRight = 0;
      int boxBottom = 0;
    
      Rectangle r = Rectangle.Empty;
      for (int y = 0; y < rowHeights.Length; ++y) {
        boxLeft = 0;
        boxRight = 0;
        boxBottom += rowHeights[y];
        for (int x = 0; x < colmWidths.Length; ++x) {
          boxRight += colmWidths[x];
          if (x == 1 && y == 3) {
            r.X = boxLeft;
            r.Y = boxTop;
          }
          if (x == 2 && y == 5) {
            r.Width = boxRight - r.Left;
            r.Height = boxBottom - r.Top;
          }
          boxLeft += colmWidths[x];
        }
        boxTop += rowHeights[y];
      }
    
      if (!r.IsEmpty) {
        e.Graphics.TranslateTransform(tlp.AutoScrollPosition.X,
                                      tlp.AutoScrollPosition.Y);
        using (var br = new LinearGradientBrush(
                              r,
                              Color.Red,
                              Color.Black,
                              LinearGradientMode.ForwardDiagonal)) {
          e.Graphics.FillRectangle(br, r);
        }
      }
    }
    

    我已经调用了e.Graphics.TranslateTransform,以防 TableLayoutPanel 控件有任何滚动条。

    结果:

    对于任何闪烁问题,请尝试从 TableLayoutPanel 继承,以便将其 DoubleBuffered 属性设置为 true。

    【讨论】:

    • 感谢您的回答,您的代码可以正常运行,但我认为存在以下两个问题:1)我最近用简短的代码编写了一个新的解决方案,请查看。 2)我们的两个从右到左模式的代码都有一点问题,需要对代码进行新的更正。谢谢
    【解决方案2】:

    概念答案

    单独设置每个单元格。第一个单元格应类似于渐变的前三分之一,第二个单元格应类似于中间部分,依此类推。

    使用System.Drawing.Color.FromArgb(int,int,int,int) 或其他最适合的重载之一手动设置颜色

    你的代码结构会有所改变:

    if(first cell)...
    if(second cell)...
    

    希望我能提供更多帮助,但自从我玩 winforms 以来,它对我来说已经很久了

    另一种方法可能是将您的单元格绘制成透明的,并在它们后面使用您想要的渐变绘制一个适当大小的矩形。

    对不起,我没有真正的代码。

    【讨论】:

      【解决方案3】:

      我找到了一个解决这个问题的方法,代码如下:

      int x, y, w, h;
      
      private void tableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
      {
        if (e.Row == 1 && e.Column == 0)
        {
          x = e.CellBounds.X;
          y = e.CellBounds.Y;
          w = e.CellBounds.Width;
          h = e.CellBounds.Height;
        }
      
        if (e.Row == 2 && e.Column == 0)
        {
          h = h + e.CellBounds.Height;
        }
      
        if (e.Row == 1 && e.Column == 1)
        {
          w = w + e.CellBounds.Width;
        }
      
        if (e.Row == 3 && e.Column == 3)
        {
          Rectangle rc = new Rectangle(x, y, w, h);
          LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Blue, Color.White, LinearGradientMode.Vertical);
          e.Graphics.FillRectangle(brush, rc);
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-08
        • 1970-01-01
        • 2014-01-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多