我的代码不起作用!我已经用 /** 注释掉了我的问题。
当我在传递 IntPtr 句柄时关闭钟摆表单时出现 OutOfMemory 异常,并且它变得很大。
我也有一个问题,使摆锤每次摆动和松动速度。他们是我理论中的一个问题,还是我做了一些愚蠢的事情。
下面是我的钟摆类的代码:
using System.Drawing; //new
using System.Windows.Forms; //new
class Pendulum
{
int length = 50;
double angle = Math.PI /2;
double aAcc = -9.81;
double aVel = 0;
double gravity = 0.1;
double mass = 0.2;
Timer timer;
public Pendulum(int frmWidth, int frmHeight, IntPtr handle)
{
timer = new Timer() { Interval = 30 };
timer.Tick += delegate(object sender, EventArgs e)
{
int originX = frmWidth / 2;
int originY = 0;
int bobX; // = frmWidth / 2;
int bobY; // = (int)length;
//to be relative to origin we go:
bobX = originX + (int)(Math.Sin(angle) * length);
bobY = originY + (int)(Math.Cos(angle) * length);
aAcc = -9.81 / length * Math.Sin(angle);
aVel += aAcc * gravity * mass;
angle += aVel * gravity; //angle += aVel;
//aVel = aVel -0.09; /** SPEED UP DRAMATICALLY! SPINS WEIRDLY! **/
DrawPendulum(originX,originY,bobX,bobY,frmWidth,frmHeight, handle);
};
timer.Start();
}
public void DrawPendulum(int originX, int originY, int bobX, int bobY, int frmWidth, int frmHeight, IntPtr handle)
{
using(Bitmap dblBuffer = new Bitmap(frmWidth, frmHeight))
using(Graphics g = Graphics.FromImage(dblBuffer))
using(Graphics f = Graphics.FromHwnd(handle))
{
g.DrawLine(Pens.Black, originX, originY, bobX, bobY);
g.FillEllipse(Brushes.Blue, bobX - 8, bobY, 20, 20); //-8 for tidyness!
f.Clear(Color.White);
f.DrawImage(dblBuffer, new Point(0, 0));
}
}
}
public partial class frmPendulum : Form
{
Pendulum p;
public frmPendulum()
{
frmLogin frm = new frmLogin();
frm.Close();
int frmWidth = this.Width;
int frmHeight = this.Height;
IntPtr Handle = this.Handle;
p = new Pendulum(frmWidth, frmHeight, Handle);
InitializeComponent();
}
private void frmPendulum_Load(object sender, EventArgs e)
{
}
public void TimerDispose()
{
timer.Dispose();
}
}
public partial class frmLogin : Form
{
public frmLogin()
{
InitializeComponent();
}
private void frmLogin_Load(object sender, EventArgs e)
{
}
private void btnSubmit_Click(object sender, EventArgs e)
{
frmPendulum f = new frmPendulum();
f.Show();
//this.Hide();
}
}
编辑:为了尝试处理我已将 Pendulum p 设为全局的计时器,以便它可以用于运行我在下面的 pendulum 类中放置的新方法:
public void TimerDispose()
{
timer.Dispose();
}
这也意味着将计时器设为全局。但是 OutOfMemory 异常仍然发生。所有代码都是上面的当前状态。