为什么矩形在闪烁? [英] why Rectangle is blinking ?

查看:103
本文介绍了为什么矩形在闪烁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用cell_painting事件绘制Rectangle(大小为3个单元格).当我将项目从listBox拖动到dataGridView时,矩形闪烁!!!!!!
如何填充Rectangle并绘制不会在dataGridView中闪烁的字符串?

i use cell_painting event to draw Rectangle (in size of 3 cells). when i drag item from the listBox to the dataGridView the the Rectangle is blinking !!!
how i can fill Rectangle and draw string that it will not blink in the dataGridView ?

private List<Point> GeneralCourses = new List<Point>();


private void dataGridView1_DragDrop(object sender, DragEventArgs e)
        {
            Point p = this.dataGridView1.PointToClient(new Point(e.X, e.Y));
            DataGridView.HitTestInfo info = this.dataGridView1.HitTest(p.X, p.Y);
            if (info.RowIndex != -1 && info.ColumnIndex != -1)
            {
                Object value = (Object)e.Data.GetData(typeof(string));

                this.dataGridView1.Rows[info.RowIndex].Cells[info.ColumnIndex].Value = value;
                GeneralCourses.Add(new Point(info.RowIndex, info.ColumnIndex));
             }
        }


protected void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (GeneralCourses.Contains(new Point(e.RowIndex, e.ColumnIndex)))
            {
                e.Handled = true;
                dataGridView1.Invalidate();

                Rectangle rectDest = Rectangle.Empty;
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Center;
                sf.LineAlignment = StringAlignment.Center;
                sf.Trimming = StringTrimming.EllipsisCharacter;
                string str;

                Pen p = new Pen(Color.Blue);
                str = "string example";

 rectDest = new Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Size.Width * 3, e.CellBounds.Height);
                e.Graphics.DrawRectangle(p, rectDest);
                e.Graphics.FillRectangle(Brushes.LightBlue, rectDest);
                e.Graphics.DrawString(str, new Font("Arial", 10, FontStyle.Regular), Brushes.Black, rectDest, sf);
                p.Dispose();
                sf.Dispose();
            }
}

推荐答案

尝试将Invalidate调用从CellPainting事件中删除-它使DataGridView重新绘制,这将导致另一个CellPainting事件,从而使DataGridView无效.哪...

如果这样做后仍然闪烁,请尝试将FormDoubleBuffered属性设置为True.
Try taking the Invalidate call out of the CellPainting Event - it causes the DataGridView to repaint, which causes another CellPainting event, which Invalidates the DataGridView which...

If it still blinks when you have done that, try setting the FormDoubleBuffered property to True.


这篇关于为什么矩形在闪烁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆