C#GDI+如何控制矩形移动
题目是按下数字键8,矩形向下移动,数字键6向右移动,数字键4向左移动,数字键2向下移动namespaceWindowsFormsApplication4{publicpa...
题目是按下数字键8,矩形向下移动,数字键6向右移动,数字键4向左移动,数字键2向下移动
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender,EventArgs e)
{
}
Rectangle r; Rectangle r1; int x = 10; int y = 10;
protected override void OnPaint (PaintEventArgs e)
{
Graphics g = e.Graphics;
Rectangle r = new Rectangle(10, 10, 20, 20);
SolidBrush s = new SolidBrush(Color.Red);
g.FillRectangle(s, r);
Graphics g1 = e.Graphics;
Rectangle r1 = new Rectangle(200, 10, 10, 10);
SolidBrush s1 = new SolidBrush(Color.Blue);
g.FillRectangle(s1, r1);
Draw(x,y);
}
private void Form1_KeyPress(object sender,KeyPressEventArgs e)
{
if (e.KeyChar == '2') {MoveDown();}
if (e.KeyChar == '4') {MoveLeft();}
if (e.KeyChar == '6') {MoveRight();}
if (e.KeyChar == '8') {MoveUp();}
}
public void MoveDown()
{
Erase(x,y); y = y + 5; Draw(x,y);
}
public void MoveLeft()
{
Erase(x,y); x = x - 5; Draw(x,y);
}
public void MoveRight()
{
Erase(x,y); x = x + 5; Draw(x,y);
}
public void MoveUp()
{
Erase(x,y); y = y - 5; Draw(x,y);
}
public void Erase(int x,int y)
{
Graphics g = this.CreateGraphics();
SolidBrush E = new SolidBrush(Color.White);
g.FillRectangle(E,new Rectangle(x, y, 20, 20));
E.Dispose();
}
public void Draw(int x,int y)
{
Graphics g = this.CreateGraphics();
SolidBrush D = new SolidBrush(Color.Red);
g.FillRectangle(D,new Rectangle( x, y, 20, 20));
D.Dispose();
}
}
}
这是我的代码,可是按键运行的时候矩形不动。。。。。请问应该如何修改代码?谢谢! 展开
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender,EventArgs e)
{
}
Rectangle r; Rectangle r1; int x = 10; int y = 10;
protected override void OnPaint (PaintEventArgs e)
{
Graphics g = e.Graphics;
Rectangle r = new Rectangle(10, 10, 20, 20);
SolidBrush s = new SolidBrush(Color.Red);
g.FillRectangle(s, r);
Graphics g1 = e.Graphics;
Rectangle r1 = new Rectangle(200, 10, 10, 10);
SolidBrush s1 = new SolidBrush(Color.Blue);
g.FillRectangle(s1, r1);
Draw(x,y);
}
private void Form1_KeyPress(object sender,KeyPressEventArgs e)
{
if (e.KeyChar == '2') {MoveDown();}
if (e.KeyChar == '4') {MoveLeft();}
if (e.KeyChar == '6') {MoveRight();}
if (e.KeyChar == '8') {MoveUp();}
}
public void MoveDown()
{
Erase(x,y); y = y + 5; Draw(x,y);
}
public void MoveLeft()
{
Erase(x,y); x = x - 5; Draw(x,y);
}
public void MoveRight()
{
Erase(x,y); x = x + 5; Draw(x,y);
}
public void MoveUp()
{
Erase(x,y); y = y - 5; Draw(x,y);
}
public void Erase(int x,int y)
{
Graphics g = this.CreateGraphics();
SolidBrush E = new SolidBrush(Color.White);
g.FillRectangle(E,new Rectangle(x, y, 20, 20));
E.Dispose();
}
public void Draw(int x,int y)
{
Graphics g = this.CreateGraphics();
SolidBrush D = new SolidBrush(Color.Red);
g.FillRectangle(D,new Rectangle( x, y, 20, 20));
D.Dispose();
}
}
}
这是我的代码,可是按键运行的时候矩形不动。。。。。请问应该如何修改代码?谢谢! 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询