在Gtk中的Application.Run()之后无法渲染形状

时间:2019-06-29 21:41:54

标签: c# animation system.drawing gtk#

我对使用C#相当陌生,并且正在MonoDevelop中使用Gtk创建应用程序。我可以制作简单的静态图形,但无法创建绘制多个对象的循环。

我目前使用System.Timers设置了间隔,以每六十分之一秒为间隔。这仅用于移动矩形并将其弹离边缘。但是,在调用Application.Run()之后似乎什么也没画。

主要方法:

public static void Main()
{
    Application.Init();
    Window w = new Window("Drawing");
    w.SetDefaultSize(width, height);

    canvas = new DrawingArea();
    canvas.ExposeEvent += ExposeHandler;
    canvas.SizeAllocated += SizeAllocatedHandler;

    SetupPensBrushes();

    Box box = new HBox(true, 0)
    {
        canvas
    };
    w.Add(box);
    w.ShowAll();
    w.DeleteEvent += Quit;

    Application.Run();
}

间隔和绘图:

public static void ExposeHandler(object obj, ExposeEventArgs args)
{
    ev = args.Event;
    window = ev.Window;
    ctx = Gtk.DotNet.Graphics.FromDrawable(window);
    FillRect(new SolidBrush(Color.Black), 0, 0, width, height); // (Custom function for a rectangle) This works and creates a black background 
    interval.Interval = 1000 / 30;
    interval.Elapsed += ElapsedEvt;
    interval.Start(); // 'ElapsedEvt' includes moving and drawing an object, but it cannot be seen on the screen. 
}

public class Obj
{
    public int x;
    public int y;
    public int w;
    public int h;
    public int xv = rnd.Next(-width / 50, width / 50);
    public int yv = rnd.Next(-height / 50, height / 50);

    public Obj(int x, int y, int w, int h)
    {
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;
    }

    public void Move()
    {
        x += xv;
        y += yv;
    }

    public void Show()
    {
        FillRect(Brushes["LightBlue"], x, y, w, h);
    }
}

public static void ElapsedEvt(object sender, ElapsedEventArgs e)
{
    boxObj.Move();
    boxObj.Show();
    // boxObj has been defined already as a new Obj()
}

编辑:每次经过间隔后,就使用canvas.QueueDraw()解决了该问题。但是,即使在处理完所有内容之后,在程序运行约10秒钟后仍存在较大的滞后。

0 个答案:

没有答案