如何在表单中放置一个复选框而不破坏控制循环?
How can I put a checkBox in the form and not to break the control loop?
我一直在做一个小游戏,我遇到了这个问题,我不知道如何解决它。一切正常,直到我在表单上放置一个复选框。我怎样才能达到同时使用复选框和控件而不用复选框打破控制循环。
这是我的代码:
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;
}
}
}
}
以下几点可能对您有所帮助。
Timer
在表单的 constructor/Load 事件中或在 Button
单击时应该是 started/stopped 而不是在 Paint
事件中。
public Form1()
{
InitializeComponent();
pozíció = Position.Left;
timer2.Start();
}
Timer.Tick
事件:
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
块:
enum Position
{
Left = 37,
Up,
Right,
Down,
}
其中37
是Left键的KeyCode
,因此Up = 38
、右 = 39
、下 = 40
。所以你可以 shrink KeyDown
事件如下:
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();
}
}
- 要show/hide图像,请改为处理
CheckedChanged
:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
pictureBox4.Image = checkBox1.Checked ? Táblázat2 : null;
}
假设 Táblázat2
是一个 class 级变量 = Image.FromFile(@"D:\Táblázat2.JPG");
在表单的构造函数中赋值。
Paint
事件:
private void pictureBox4_Paint(object sender, PaintEventArgs e)
{
e.Graphics.FillRectangle(Brushes.Blue, x, y, 20, 20);
}
- 或者,当您处理
Paint
事件时,您可以摆脱 CheckBox
控件的 CheckedChanged
事件并自己绘制图像:
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);
}
以防计时器未启用:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
pictureBox4.Invalidate();
}
祝你好运。
我一直在做一个小游戏,我遇到了这个问题,我不知道如何解决它。一切正常,直到我在表单上放置一个复选框。我怎样才能达到同时使用复选框和控件而不用复选框打破控制循环。
这是我的代码:
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;
}
}
}
}
以下几点可能对您有所帮助。
Timer
在表单的 constructor/Load 事件中或在Button
单击时应该是 started/stopped 而不是在Paint
事件中。
public Form1()
{
InitializeComponent();
pozíció = Position.Left;
timer2.Start();
}
Timer.Tick
事件:
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
块:
enum Position
{
Left = 37,
Up,
Right,
Down,
}
其中37
是Left键的KeyCode
,因此Up = 38
、右 = 39
、下 = 40
。所以你可以 shrink KeyDown
事件如下:
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();
}
}
- 要show/hide图像,请改为处理
CheckedChanged
:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
pictureBox4.Image = checkBox1.Checked ? Táblázat2 : null;
}
假设 Táblázat2
是一个 class 级变量 = Image.FromFile(@"D:\Táblázat2.JPG");
在表单的构造函数中赋值。
Paint
事件:
private void pictureBox4_Paint(object sender, PaintEventArgs e)
{
e.Graphics.FillRectangle(Brushes.Blue, x, y, 20, 20);
}
- 或者,当您处理
Paint
事件时,您可以摆脱CheckBox
控件的CheckedChanged
事件并自己绘制图像:
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);
}
以防计时器未启用:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
pictureBox4.Invalidate();
}
祝你好运。