Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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# 在c中的列标题上绘制矩形#_C#_Winforms - Fatal编程技术网

C# 在c中的列标题上绘制矩形#

C# 在c中的列标题上绘制矩形#,c#,winforms,C#,Winforms,我已经绘制了datagridview列标题,并在scroll事件中调用了重新绘制事件,但它似乎没有正确地重新绘制。绘制的矩形中的文本被剪切(参见第二幅图) 这是我的密码 void dataGridView1_Scroll(object sender, ScrollEventArgs e) { Rectangle rtHeader = this.dataGridView1.DisplayRectangle; rtHeader.Y += 0; rtHeader.He

我已经绘制了datagridview列标题,并在scroll事件中调用了重新绘制事件,但它似乎没有正确地重新绘制。绘制的矩形中的文本被剪切(参见第二幅图) 这是我的密码

 void dataGridView1_Scroll(object sender, ScrollEventArgs e)
 {
     Rectangle rtHeader = this.dataGridView1.DisplayRectangle;
     rtHeader.Y += 0;
     rtHeader.Height = this.dataGridView1.ColumnHeadersHeight;
 }
 Rectangle r1;
 void dataGridView1_Paint(object sender, PaintEventArgs e)
 {
     string[] monthes = { "APPLE", "MANGO", "CHERRY", "GRAPES", "PINEAPPLE" };
     for (int j = 0; j < this.dataGridView1.ColumnCount; )
     {
         r1 = this.dataGridView1.GetCellDisplayRectangle(j, -1, true);
         int w2 = this.dataGridView1.GetCellDisplayRectangle(j + 1, -1, true).Width;
         r1.X += -2;
         r1.Y += 30;
         r1.Width = r1.Width + w2 - 1;
         r1.Height = r1.Height / 3 - 2;
         e.Graphics.FillRectangle(new SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.BackColor), r1);
         StringFormat format = new StringFormat();
         format.Alignment = StringAlignment.Center;
         format.LineAlignment = StringAlignment.Center;
         e.Graphics.DrawRectangle(new Pen(Color.Black), r1);
         e.Graphics.DrawString(monthes[j / 2], this.dataGridView1.ColumnHeadersDefaultCellStyle.Font, new SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor), r1, format);
         j += 2;
     }
     string[] year = { "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY" };
     //for (int i = 0; i < this.dataGridView1.ColumnCount; )
     //{
           Rectangle rec = this.dataGridView1.GetCellDisplayRectangle(0, -1, true);
           int wid = this.dataGridView1.GetCellDisplayRectangle(1, -1, true).Width;
           rec.X += -2;
           rec.Y += 1;
           rec.Width = this.dataGridView1.Columns.GetColumnsWidth(DataGridViewElementStates.Visible);
           rec.Height = rec.Height / 3 - 2;
           e.Graphics.FillRectangle(new SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.BackColor), rec);
           StringFormat frm = new StringFormat();
           frm.Alignment = StringAlignment.Center;
           frm.LineAlignment = StringAlignment.Center;
           e.Graphics.DrawRectangle(new Pen(Color.Black), rec);
           e.Graphics.DrawString("Favourite fruits", new Font("Times new roman", 16, FontStyle.Regular), new SolidBrush(Color.CornflowerBlue), rec, frm);
 }
void dataGridView1\u滚动(对象发送方,ScrollEventArgs e)
{
矩形Rheader=this.dataGridView1.DisplayRectangle;
rHeader.Y+=0;
rHeader.Height=this.dataGridView1.ColumnHeaderSHight;
}
矩形r1;
void dataGridView1_Paint(对象发送器,PaintEventArgs e)
{
字符串[]月={“苹果”、“芒果”、“樱桃”、“葡萄”、“菠萝”};
对于(int j=0;j

<> P>

我理解你在这个问题中所指的问题是固定的,不是吗?(显然你已经删除了我建议的代码行)。只有代码答案应该被避免。请考虑解释你的答案。
private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
    {
        /*
        Rectangle rtHeader = this.dataGridView1.DisplayRectangle;
        rtHeader.Y += 0;
        rtHeader.Height = this.dataGridView1.ColumnHeadersHeight;
        this.dataGridView1.Invalidate(rtHeader);

        */
        this.dataGridView1.Invalidate();
    }
  if (e.RowIndex == -1 && e.ColumnIndex > -1)
            {
                e.PaintBackground(e.CellBounds, true);
                RenderColumnHeader(e.Graphics, e.CellBounds, e.CellBounds.Contains(hotSpot) ? hotSpotColor : backColor);
                RenderColumnHeaderBorder(e.Graphics, e.CellBounds, e.ColumnIndex);
                using (Brush brush = new SolidBrush(e.CellStyle.ForeColor))
                {
                    using (StringFormat sf = new StringFormat() { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center })
                    {
                        e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, brush, e.CellBounds, sf);
                    }
                }
                e.Handled = true;
            }
        }
        Color hotSpotColor = Color.LightGreen;//For hover backcolor
        Color backColor = Color.MediumSeaGreen;    //For backColor    
        Point hotSpot;
        private void RenderColumnHeader(Graphics g, Rectangle headerBounds, Color c)
        {
            int topHeight = 10;
            Rectangle topRect = new Rectangle(headerBounds.Left, headerBounds.Top + 1, headerBounds.Width, topHeight);
            RectangleF bottomRect = new RectangleF(headerBounds.Left, headerBounds.Top + 1 + topHeight, headerBounds.Width, headerBounds.Height - topHeight - 4);
            Color c1 = Color.FromArgb(180, c);
            using (SolidBrush brush = new SolidBrush(c1))
            {
                g.FillRectangle(brush, topRect);
                brush.Color = c;
                g.FillRectangle(brush, bottomRect);
            }
        }
        private void RenderColumnHeaderBorder(Graphics g, Rectangle headerBounds, int colIndex)
        {
            ControlPaint.DrawBorder3D(g, headerBounds, Border3DStyle.Raised, Border3DSide.All & ~Border3DSide.Middle);
        }
        //MouseMove event handler for your dataGridView1
        private void dataGridView1_MouseMove(object sender, MouseEventArgs e)
        {
            hotSpot = e.Location;
        }
        //MouseLeave event handler for your dataGridView1
        private void dataGridView1_MouseLeave(object sender, EventArgs e)
        {
            hotSpot = Point.Empty;
        }