当表单调整大小时,C#get formate无效? [英] C# get form to invalidate when form is resized?

查看:60
本文介绍了当表单调整大小时,C#get formate无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



所以我刚刚挑战创建一个棋盘格相同的矩形/正方形交替颜色所以我设法做到了(我知道)我的代码可能非常难看)但我在技术上已经完成了挑战,但我很好奇我注意到当用户调整表单大小拖动它时,表单不断重新绘制自己,几乎就像是图像的口吃。我想知道我写的代码也许我可以抛出一些类型的代码,使其只有在用户完成大小调整后才会失效?



hi all,

so i have just had a challenge to create a checkerboard 64 equal rectangles/squares alternating in color so i managed to do that (i know my code is probably very ugly) but i technically have done the challenge but being curious i notice when the user resizes the form dragging it the form constantly repaints itself giving almost like a stutter of the image. i was wonderin with the code i wrote maybe i could throw some type of code in to make it only invalidate once the user finishes resizing?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;



public class checkerboard : Form
{
    public checkerboard()
    {
        Size = new Size(400, 400);
        Text = "CheckerBoard";
        BackColor = Color.Red;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        Graphics g = e.Graphics;

        int h = DisplayRectangle.Height;
        int w = DisplayRectangle.Width;

        for (int i = 0; i < 8; i = i + 2)

            for (int j = 0; j < 8; j = j + 2)
            {
                Color Black = Color.Black;
                Brush brush = new SolidBrush(Black);
                g.FillRectangle(brush, i * w / 8, j * h / 8, w / 8, h / 8);
            }


        for (int i = 1; i < 8; i = i + 2)
            for (int j = 0; j < 8; j = j + 2)
            {
                Color White = Color.White;
                Brush brush = new SolidBrush(White);
                g.FillRectangle(brush, i * w / 8, j * h / 8, w / 8, h / 8);
            }
        for (int i = 1; i < 8; i = i + 2)

            for (int j = 1; j < 8; j = j + 2)
            {
                Color Black = Color.Black;
                Brush brush = new SolidBrush(Black);
                g.FillRectangle(brush, i * w / 8, j * h / 8, w / 8, h / 8);
            }
        for (int i = 0; i < 8; i = i + 2)
            for (int j = 1; j < 8; j = j + 2)
            {
                Color White = Color.White;
                Brush brush = new SolidBrush(White);
                g.FillRectangle(brush, i * w / 8, j * h / 8, w / 8, h / 8);
            }
                base.OnPaint(e);
               
            }

    static void Main()
    {
        Application.Run(new checkerboard());
    }
}

推荐答案

您可以在Form Resize处理程序中调用Invalidate方法。除此之外,您还可以将代码从OnPaint移动到OnPainBackground以避免闪烁。检查代码修改。

You can call Invalidate method inside the Form Resize handler. In addition to this move your code from OnPaint to OnPainBackground to avoid flickering. Check the code modifications.
public checkerboard()
{
  Size = new Size(400, 400);
  Text = "CheckerBoard";
  BackColor = Color.Red;
  DoubleBuffered = true;
  Resize += new System.EventHandler(this.Form1_Resize);
}

protected override void OnPaintBackground(PaintEventArgs e)
{
   Graphics g = e.Graphics;

   int h = DisplayRectangle.Height;
   int w = DisplayRectangle.Width;

     for (int i = 0; i < 8; i = i + 2)
     for (int j = 0; j < 8; j = j + 2)
     {
        Color Black = Color.Black;
        Brush brush = new SolidBrush(Black);
        g.FillRectangle(brush, i * w / 8, j * h / 8, w / 8, h / 8);
        brush.Dispose(); // added this line to clear object else this will produce resource leak
     }


    for (int i = 1; i < 8; i = i + 2)
    for (int j = 0; j < 8; j = j + 2)
    {
      Color White = Color.White;
      Brush brush = new SolidBrush(White);
      g.FillRectangle(brush, i * w / 8, j * h / 8, w / 8, h / 8);
       brush.Dispose(); // added this line to clear object else this will produce resource leak
    }
    for (int i = 1; i < 8; i = i + 2)

    for (int j = 1; j < 8; j = j + 2)
    {
      Color Black = Color.Black;
      Brush brush = new SolidBrush(Black);
      g.FillRectangle(brush, i * w / 8, j * h / 8, w / 8, h / 8);
      brush.Dispose(); // added this line to clear object else this will produce resource leak
    }
    for (int i = 0; i < 8; i = i + 2)
    for (int j = 1; j < 8; j = j + 2)
    {
      Color White = Color.White;
      Brush brush = new SolidBrush(White);
      g.FillRectangle(brush, i * w / 8, j * h / 8, w / 8, h / 8);
       brush.Dispose();
     }          
 }
private void Form1_Resize(object sender, EventArgs e)
{
  this.Invalidate();
} 


这篇关于当表单调整大小时,C#get formate无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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