我怎样才能在窗体中放置一个checkBox而不中断控制循环? [英] How can I put a checkBox in the form and not to break the control loop?

查看:97
本文介绍了我怎样才能在窗体中放置一个checkBox而不中断控制循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在做一个小游戏,遇到了这个问题,我不知道该怎么解决.一切正常,直到我在表单上放置了一个复选框.如何同时使用checkBox和控件而不用checkBox破坏控件循环,该如何实现呢?

I have been working on a little game and I encountered with this problem and I can't figure out how to solve it. Everything worked fine until I put a checkBox on the form. How can I reach that to use the checkBox and the control at the same time and not to break the control loop with the checkBox.

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace Mozgás_Gyakorlás
{
    public partial class Form1 : Form
    {
        enum Position
        {
            Left, Right, Up, Down
        }
        public int x = 262;
        public int y = 318;
        private Position pozíció;
        public Form1()
        {
            InitializeComponent();
            pozíció = Position.Left;
        }

        public void pictureBox4_Paint(object sender, PaintEventArgs e)
        {
            timer2.Start();
            e.Graphics.FillRectangle((Brushes.Blue), x, y, 20, 20);
            checkBox1.PerformLayout();
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            if(pozíció == Position.Right)
            {
                x += 3;
            }
            if(pozíció == Position.Left)
            {
                x -= 3;
            }
            if(pozíció == Position.Up)
            {
                y -= 3;
            }
            if(pozíció == Position.Down)
            {
                y += 3;
            }
            pictureBox4.Invalidate();
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Up)
            {
                pozíció = Position.Up;
            }
            if(e.KeyCode == Keys.Down)
            {
                pozíció = Position.Down;
            }
            if(e.KeyCode == Keys.Left)
            {
                pozíció = Position.Left;
            }
            if(e.KeyCode == Keys.Right)
            {
                pozíció = Position.Right;
            }
        }

        private void checkBox1_Click(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                pictureBox4.Image = Image.FromFile(@"D:\Táblázat2.JPG");
            }
            else
            {
                pictureBox4.Image = null;
            }
        }

    }
}

推荐答案

以下几点可能会有所帮助.

Here's some points that might help.

  • Timer应该在窗体的构造函数/Load事件中或在Button单击中而不是在Paint事件中启动/停止.
  • The Timer should be started/stopped in the Form's constructor/Load event or on a Button click and not in the Paint event.
public Form1()
{
    InitializeComponent();
    pozíció = Position.Left;
    timer2.Start();
}

  • Timer.Tick事件:
    • The Timer.Tick event:
    • private void timer2_Tick(object sender, EventArgs e)
      {
          //Hint: You might want to keep the rectangle within the canvas.
          pozíció = x < 0 ? Position.Right : x + 21 > pictureBox4.ClientRectangle.Right ? Position.Left : pozíció;
          pozíció = y < 0 ? Position.Down : y + 21 > pictureBox4.ClientRectangle.Bottom ? Position.Up : pozíció;
      
          switch (pozíció)
          {
              case Position.Right:
                  x += 3;
                  break;
              case Position.Left:
                  x -= 3;
                  break;
              case Position.Up:
                  y -= 3;
                  break;
              case Position.Down:
                  y += 3;
                  break;
              default:
                  x = 0;
                  y = 0;
                  break;
          }
          pictureBox4.Invalidate();
      }
      

      • 您可以按如下方式编写enum块:
        • You can write the enum block as follows:
        • enum Position
          {
              Left = 37,
              Up,
              Right,
              Down,
          }
          

          其中37 Left 键的KeyCode,因此 Up = 38 Right = ,并且 Down = 40.因此,您可以收缩 KeyDown事件,如下所示:

          Where 37 is the KeyCode of the Left key, and consequently Up = 38, Right = 39, and Down = 40. So you can shrink the KeyDown event as follows:

          private void Form1_KeyDown(object sender, KeyEventArgs e)
          {
              if (e.KeyCode == Keys.Left ||
                  e.KeyCode == Keys.Up ||
                  e.KeyCode == Keys.Right ||
                  e.KeyCode == Keys.Down)
              {
                  pozíció = (Position)e.KeyCode;
                  pictureBox4.Invalidate();
              }
          }
          

          • 要显示/隐藏图像,请改用CheckedChanged:
            • To show/hide the image, handle the CheckedChanged instead:
            • private void checkBox1_CheckedChanged(object sender, EventArgs e)
              {
                  pictureBox4.Image = checkBox1.Checked ? Táblázat2 : null;
              }
              

              假定Táblázat2是在窗体的构造函数中分配的类级别变量= Image.FromFile(@"D:\Táblázat2.JPG");.

              Presuming that the Táblázat2 is a class level variable = Image.FromFile(@"D:\Táblázat2.JPG"); assigned in the Form's constructor.

              • Paint事件:
              private void pictureBox4_Paint(object sender, PaintEventArgs e)
              {
                  e.Graphics.FillRectangle(Brushes.Blue, x, y, 20, 20);
              }
              

              • 或者,在处理Paint事件时,您可以摆脱CheckBox控件的CheckedChanged事件并自己绘制图像:
                • Alternatively, and as you are handling the Paint event, you can get rid of the CheckedChanged event of the CheckBox control and draw the image yourself:
                • private void pictureBox4_Paint(object sender, PaintEventArgs e)
                  {
                      var g = e.Graphics;
                  
                      if (checkBox1.Checked)
                      {
                          var sz = Táblázat2.Size;
                          var r = e.ClipRectangle;
                  
                          g.SmoothingMode = SmoothingMode.HighQuality;
                  
                          g.DrawImage(Táblázat2,
                          new Rectangle((r.Width - sz.Width) / 2, (r.Height - sz.Height) /2, sz.Width, sz.Height),
                          new Rectangle(0, 0, sz.Width, sz.Height),
                          GraphicsUnit.Pixel);
                      }
                      g.FillRectangle(Brushes.Blue, x, y, 20, 20);
                  }
                  

                  以防万一计时器未启用:

                  and just in case the timer is not enabled:

                  private void checkBox1_CheckedChanged(object sender, EventArgs e)
                  {
                      pictureBox4.Invalidate();
                  }
                  

                  祝你好运.

                  这篇关于我怎样才能在窗体中放置一个checkBox而不中断控制循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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