问题
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.
Here is my code:
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;
}
}
}
}
回答1:
Here's some points that might help.
- The
Timer
should be started/stopped in the Form's constructor/Load event or on aButton
click and not in thePaint
event.
public Form1()
{
InitializeComponent();
pozíció = Position.Left;
timer2.Start();
}
- 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();
}
- You can write the
enum
block as follows:
enum Position
{
Left = 37,
Up,
Right,
Down,
}
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();
}
}
- 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;
}
Presuming that the Táblázat2
is a class level variable = Image.FromFile(@"D:\Táblázat2.JPG");
assigned in the Form's constructor.
- The
Paint
event:
private void pictureBox4_Paint(object sender, PaintEventArgs e)
{
e.Graphics.FillRectangle(Brushes.Blue, x, y, 20, 20);
}
- Alternatively, and as you are handling the
Paint
event, you can get rid of theCheckedChanged
event of theCheckBox
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();
}
Good luck.
来源:https://stackoverflow.com/questions/59337141/how-can-i-put-a-checkbox-in-the-form-and-not-to-break-the-control-loop