class Class1
{
private int a;
private int b;
private int x;
private int y;
public Class1(int a,int b, int x, int y)
{
this.a = a;
this.b = b;
this.x = x;
this.y = y;
}
public int A
{
get { return a; }
set { a = value; }
}
public int B
{
get { return b; }
set { b = value; }
}
public int X
{
get { return x; }
set { x = value; }
}
public int Y
{
get { return y; }
set { y = value; }
}
}
public partial class Form1 : Form
{
List<Class1> listan = new List<Class1>();
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
Font font = new Font("Arial", 16);
SolidBrush brush = new SolidBrush(Color.DeepSkyBlue);
Class1 classen = new Class1(new Random().Next(3,12), new Random().Next(3, 11), new Random().Next(0, 600),0);
MessageBox.Show("32");
g.DrawString(classen.A.ToString() + "X" + classen.B.ToString(), font, brush, classen.X, classen.Y);
}
private void timer1_Tick(object sender, EventArgs e)
{
for(int i = 0; i < listan.Count; i++)
{
listan[i].Y -= 1;
Invalidate();
}
}
}
我所要做的就是让字符串classen.A.ToString() + "X" + classen.B.ToString()
向下移动......我对C#不熟悉,编程一般,如果你能告诉我什么,我会非常感兴趣我做错了。谢谢!
“Lorem ipsum dolor sit amet,consectetur adipiscing elit,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.Ut enim ad minim veniam,quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.Duis aute irure dolor in代表在velitate velit esse cillum dolore eu fugiat nulla pariatur。Excepteur sint occaecat cupidatat non proident,sunt in culpa qui officia deserunt mollit anim id est laborum。“
忽略Lorem ipsum文字:p
答案 0 :(得分:0)
您的文字无法移动,因为timer1_Tick
在繁忙的循环中使您的表单listan.Count
无效,然后使用listan[listan.Count-1].Y
的最终值进行绘制,而不会更改它。< / p>
您需要在表单中声明一次Class1 rect
变量,在构造函数中初始化一次,然后在timer1_Tick
中执行以下操作:
rect.Y += 1;
Invalidate();
将您的计时器间隔设置为10毫秒。不要循环你的计时器回调。不要在Paint
事件中初始化类。您应该在类中的Paint
处理程序中声明您正在初始化的每个类,并在Form构造函数或OnLoad
处理程序中初始化它们一次。
另外,清理你的代码; Class1
是一个可怕的名字,看起来它与System.Forms.Rectangle
的做法不同。 A
,B
等等都是可怕的财产名称。
请勿执行任何可能导致表单失效的操作,例如在MessageBox.Show
处理程序中调用Paint
。
您需要在构造函数中初始化一个Random
; new Random().Next()
不符合您的想法。
画笔和字体是非托管资源,您不能在Paint
处理程序中重新分配它们;在Form构造函数或OnLoad
处理程序中初始化它们。
答案 1 :(得分:0)
这是您的代码,稍作修改。我评论了我的变化。
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// Instantiate an instance of your class.
Class1 classen = new Class1(new Random().Next(3, 12), new Random().Next(3, 11), new Random().Next(0, 600), 0);
// Add it to your list.
listan.Add(classen);
}
List<Class1> listan = new List<Class1>();
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
Font font = new Font("Arial", 16);
SolidBrush brush = new SolidBrush(Color.DeepSkyBlue);
//MessageBox.Show("32"); <-- don't do this, it will just popup every time your screen is invalidated.
// Loop through your list.
for (int i = listan.Count - 1; i >= 0; i--)
{
// Determine if your text has scrolled off the top of the screen.
if (listan[i].Y < -e.Graphics.MeasureString(listan[i].A.ToString(), font).Height)
{
// remove it from the list because it can't be seen anymore, and create a new one.
listan.RemoveAt(i);
Class1 classen = new Class1(new Random().Next(3, 12), new Random().Next(3, 11), new Random().Next(0, 600), new Random().Next(0, 600));
// Add it to your list.
listan.Add(classen);
}
else
{
// Draw the item, just like you were before.
g.DrawString(listan[i].A.ToString() + "X" + listan[i].B.ToString(), font, brush, listan[i].X, listan[i].Y);
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
for (int i = 0; i < listan.Count; i++)
{
listan[i].Y -= 1;
Invalidate();
}
}
}
}
class Class1
{
private int a;
private int b;
private int x;
private int y;
public Class1(int a, int b, int x, int y)
{
this.a = a;
this.b = b;
this.x = x;
this.y = y;
}
public int A
{
get { return a; }
set { a = value; }
}
public int B
{
get { return b; }
set { b = value; }
}
public int X
{
get { return x; }
set { x = value; }
}
public int Y
{
get { return y; }
set { y = value; }
}
}