Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DataGridView自定义单元格绘制的文本溢出_C#_Visual Studio_Winforms - Fatal编程技术网

C# DataGridView自定义单元格绘制的文本溢出

C# DataGridView自定义单元格绘制的文本溢出,c#,visual-studio,winforms,C#,Visual Studio,Winforms,这是我的细胞绘画方法 DataGridView grid = (DataGridView)sender; if (e.RowIndex == -1 || e.ColumnIndex == -1) { return; } if ((grid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null)) return; Brush gridBrush = new SolidBrush(GridList[0

这是我的细胞绘画方法

DataGridView grid = (DataGridView)sender;

        if (e.RowIndex == -1 || e.ColumnIndex == -1) { return; }
        if ((grid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null)) return;
        Brush gridBrush = new SolidBrush(GridList[0].GridColor),backColorBrush = new SolidBrush(e.CellStyle.BackColor);

        Pen gridLinePen = new Pen(gridBrush);

        // Erase the cell.
        e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

        // Draw the grid lines (only the right and bottom lines;
        // DataGridView takes care of the others).
        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,e.CellBounds.Bottom - 1);
        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,e.CellBounds.Top, e.CellBounds.Right - 1,e.CellBounds.Bottom);
        // Draw the text content of the cell, ignoring alignment.
        if (e.Value != null)
        {

            Brush brush = new SolidBrush(Color.Red);
            Brush brush1 = new SolidBrush(Color.Black);
            String s = (String)e.Value;

            System.Drawing.Rectangle rect = e.CellBounds;
            List<int> pos = null;
            if (grid.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag!=null){
                pos = (List<int>)grid.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag;
            }

            String[] arr = s.Split('\n');
            SizeF stringSize = TextRenderer.MeasureText(e.Graphics, arr[0], e.CellStyle.Font, e.CellBounds.Size);

            float wid = stringSize.Height;
            int X,Y;

            for(int i=0;i<arr.Length;i++){
                if (pos==null||pos.IndexOf(i)==-1)
                {
                    X = (e.CellBounds.X);
                    Y = (e.CellBounds.Y + i * ((int)stringSize.Height));
                    TextRenderer.DrawText(e.Graphics, arr[i], e.CellStyle.Font, new Point(X, Y), SystemColors.ControlText);
                    //e.Graphics.DrawString(arr[i], e.CellStyle.Font, brush1, new PointF(X, Y), StringFormat.GenericDefault);
                }
                else
                {
                    X = (e.CellBounds.X);
                    Y = (e.CellBounds.Y + i * (int)stringSize.Height );
                    Brush brushForBox = new SolidBrush(Color.FromArgb(100, 120, 50,0));
                    e.Graphics.FillRectangle(brushForBox, X, Y, e.CellBounds.Width, (int)stringSize.Height);
                    TextRenderer.DrawText(e.Graphics, arr[i], e.CellStyle.Font, new Point(X, Y), SystemColors.ControlText);
                    //e.Graphics.DrawString(arr[i], e.CellStyle.Font, brush, new PointF(X, Y), StringFormat.GenericDefault);
                }
            }                   
        }
        //grid.InvalidateCell(-1, e.RowIndex);
        e.Handled = true;
DataGridView grid=(DataGridView)发送方;
如果(e.RowIndex==-1 | | e.ColumnIndex==-1){return;}
if((grid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value==null))返回;
笔刷gridBrush=new SolidBrush(GridList[0].GridColor),backcolorbush=new SolidBrush(e.CellStyle.BackColor);
画笔网格线画笔=新画笔(网格画笔);
//删除单元格。
e、 图形。填充矩形(背景色画笔,例如单元格边界);
//绘制网格线(仅右侧和底部线;
//DataGridView负责处理其他问题)。
e、 Graphics.DrawLine(网格笔,e.CellBounds.Left,e.CellBounds.Bottom-1,e.CellBounds.Right-1,e.CellBounds.Bottom-1);
e、 Graphics.DrawLine(网格笔,e.CellBounds.Right-1,e.CellBounds.Top,e.CellBounds.Right-1,e.CellBounds.Bottom);
//绘制单元格的文本内容,忽略对齐方式。
如果(e.Value!=null)
{
笔刷=新的SolidBrush(颜色为红色);
笔刷笔刷1=新的SolidBrush(颜色为黑色);
字符串s=(字符串)e.Value;
System.Drawing.Rectangle=e.CellBounds;
列表pos=null;
if(grid.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag!=null){
pos=(List)grid.Rows[e.RowIndex]。单元格[e.ColumnIndex]。标记;
}
字符串[]arr=s.Split('\n');
SizeF stringSize=TextRenderer.MeasureText(e.Graphics,arr[0],e.CellStyle.Font,e.CellBounds.Size);
浮动宽度=stringSize.高度;
int X,Y;

对于(int i=0;i,
CellPainting
事件将允许您绘制
DataGridView
的整个可见区域,包括所有标题,仅不包括滚动条

它确实为您提供了
e.CellBounds
矩形中的
单元格
区域,但仍允许您在其外部绘制

要将绘图限制在
单元格
中,最简单的方法是将
e.Graphics.clipboonds
更改为单元格的边框;为了确保不会溢出到行标题中,我们将其限制为仅从行标题的左侧开始,可能如下所示:

int rhw = grid.RowHeadersWidth;
Rectangle clip = e.CellBounds;
if (e.CellBounds.X < rhw)
    clip = new Rectangle(rhw, clip.Y, clip.Width - rhw, clip.Height);
e.Graphics.SetClip(clip, CombineMode.Replace);
int rhw=grid.RowHeadersWidth;
矩形剪辑=e.CellBounds;
如果(e.CellBounds.X
现在你画的东西都不会溢出

注:

  • 您还可以为和设置目标矩形,但使用不同字体绘制会使这一点更加困难
  • 由于某些原因,剪裁区域似乎无法与
    textrender
    一起使用

另请注意:我无法再现流入标题的效果。我可以想象,如果顶部单元格不完全可见,它可能来自单元格的顶部,并处于负值。(不过,我的DGV只允许我按整行滚动。)要排除这些情况,您可能需要计算一个更好的剪切矩形,该矩形仅从标题单元格的正下方开始。

尝试使用DrawString重载或推荐(!)带有目标矩形的DrawText。当然,如果文本无法放入矩形,它现在将被切断。@TaW我尝试了DrawString,但对多行文本无效。我没有理解您的观点“带有目标矩形的DrawText”请您解释一下。我写这篇文章是为了突出显示datagridview单元格中的某一行文本。@ArunPratap您提到的问题是在两个单元格之间画更粗的线,这与我的问题很不相似。在我的情况下,我有一个文本溢出问题。您至少应该同时解决这两个问题首先。下溢发生在行标题下,而不是列标题下。事实上,DGV只允许整行滚动,但列不允许整行滚动。因此,当某些单元格内容位于行标题下,而您将鼠标悬停在行标题上方时,只需重新绘制单元格而不是标题(或其他方式)标题下的文本变得可见。我可以使用e.clipboonds,但问题是我找不到需要从哪个字符开始我的字符串。啊,明白了。我已经更新了答案以防止溢出。如果发现剪切正确,即文本被剪切到左侧。在侧注CombineMode.Union wworks比Replace好得多在我的例子中,我真的不明白为什么。嗯,这听起来是错误的。如果它工作的话很好,但是它真的不应该改变剪裁区域,因为单元格边界已经包含在旧的ClipRegion中了。另外:你说“工作得更好”是什么意思?我怀疑TextRenderer没有遵循b/c GDI兼容性模式: