0

我的代码不起作用!我已经用 /** 注释掉了我的问题。
当我在传递 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 异常仍然发生。所有代码都是上面的当前状态。

4

1 回答 1

0

真是一团糟....

处理 frmPendulum() 的 Paint() 事件并将其传递e.Graphics给 DrawPendulum()。
frmPendulum 应该有自己的 Timer 来简单地刷新自己。

在 Pendulum 中,直接绘制到传递的 Graphics。摆脱位图和句柄代码。这些变量都应该在类级别,以便可以在 DrawPendulum() 中访问它们。Pendulum 不应在 Tick() 事件中绘制自身。

所以它应该看起来更像这样:

public partial class frmPendulum : Form
{

    private Timer timer;
    private Pendulum p = null;

    public frmPendulum()
    {
        InitializeComponent();

        this.Shown += new EventHandler(frmPendulum_Shown);
        this.Paint += new PaintEventHandler(frmPendulum_Paint);
    }

    void frmPendulum_Shown(object sender, EventArgs e)
    {
        p = new Pendulum(this.ClientRectangle.Width, this.ClientRectangle.Height);
        timer = new Timer() { Interval = 100 };
        timer.Tick += delegate(object s2, EventArgs e2)
        {
            this.Refresh();
        };
        timer.Start();
    }

    void frmPendulum_Paint(object sender, PaintEventArgs e)
    {
        if (p != null)
        {
            p.DrawPendulum(e.Graphics);
        }
    }

}

public 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;

    int originX = 0;
    int originY = 0;
    int bobX; // = frmWidth / 2;
    int bobY; // = (int)length;

    Timer timer;

    public Pendulum(int frmWidth, int frmHeight)
    {
        timer = new Timer() { Interval = 50 };

        timer.Tick += delegate(object sender, EventArgs e)
        {
            originX = frmWidth / 2;
            originY = 0;

            //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; 
        };

        timer.Start();
    }

    public void DrawPendulum(Graphics G)
    {
        G.DrawLine(Pens.Black, originX, originY, bobX, bobY);
        G.FillEllipse(Brushes.Blue, bobX - 8, bobY, 20, 20); //-8 for tidyness!
    }

}

在此处输入图像描述

于 2013-10-07T18:19:07.467 回答