Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.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#_.net_Winforms_Onpaint - Fatal编程技术网

C# 喷漆/仅喷漆时的奇怪行为

C# 喷漆/仅喷漆时的奇怪行为,c#,.net,winforms,onpaint,C#,.net,Winforms,Onpaint,我有一个网格和一个四边形。网格在其中布局为四边形 class Grid : System.Windows.Forms.Control { protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); } protected override void OnLayout(LayoutEventArgs levent)

我有一个网格和一个四边形。网格在其中布局为四边形

class Grid : System.Windows.Forms.Control
    {
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
        }

        protected override void OnLayout(LayoutEventArgs levent)
        {
            base.OnLayout(levent);

            int numControls = this.Controls.Count;

            if (numControls < 1)
            {
                return;
            }

            int size = this.Width / numControls;

            int i = 0;

            foreach (Control ctrl in this.Controls)
            {
                ctrl.Size = new Size(size, this.Height);
                ctrl.Location = new Point(i * size, 0);
                i++;
            }
        }
}
以我的形式

        this.quad1 = new Quad();
        this.SuspendLayout();

        grid1 = new Grid();
        grid1.Size = new Size(200, 200);

        for (int i = 0; i < 4; i++)
        {
            Grid grid_j = new Grid();
            for (int j = 0; j < 4; j++)
            {
                grid_j.Controls.Add(new Quad());
            }
            grid1.Controls.Add(grid_j);
        }

        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(284, 261);
        this.Controls.Add(grid1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);

protected override void OnLayout(System.Windows.Forms.LayoutEventArgs levent)
        {
             base.OnLayout(levent);

             this.grid1.Size = new Size(this.ClientRectangle.Width, this.ClientRectangle.Height);
             this.grid1.Location = new Point(0, 0);
        }

这是正确的方法吗?另外,何时必须调用进一步的“更新”

Invalidate()
告诉控件自行重新绘制,并且在
OnPaint
之前图形不会被清除。Invalidate是否会传播到子级?(它会传播给父母吗?)我相信ti会传播给孩子(而不是父母)。我不是100%确定,但你可以用一个简单的绘图来测试。默认情况下,绘图是优化的,只重绘控件调整大小时显示的部分。换句话说,它不会覆盖矩形的一条边,从而产生涂抹效果。此行为由ControlStyles.ResizerDraw样式控制。对于控件,默认情况下它处于禁用状态。打开它最简单的方法是添加
this.ResizeRedraw=true到四元类构造函数。这增加了明显闪烁的几率,如果这是一个问题,那么也添加
This.DoubleBuffered=true
        this.quad1 = new Quad();
        this.SuspendLayout();

        grid1 = new Grid();
        grid1.Size = new Size(200, 200);

        for (int i = 0; i < 4; i++)
        {
            Grid grid_j = new Grid();
            for (int j = 0; j < 4; j++)
            {
                grid_j.Controls.Add(new Quad());
            }
            grid1.Controls.Add(grid_j);
        }

        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(284, 261);
        this.Controls.Add(grid1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);

protected override void OnLayout(System.Windows.Forms.LayoutEventArgs levent)
        {
             base.OnLayout(levent);

             this.grid1.Size = new Size(this.ClientRectangle.Width, this.ClientRectangle.Height);
             this.grid1.Location = new Point(0, 0);
        }
foreach (Control ctrl in this.Controls)
            {
                ctrl.Size = new Size(size, this.ClientRectangle.Height-10);
                ctrl.Location = new Point(i * size, 0);
                ctrl.Invalidate();
                i++;
            }