Search code examples
c#linuxdrawcairogtk#

GTK# using DrawingArea


I want to write a Snake game using GTK# but for several days I can't figure out the way dealing with DrawingArea. In all the examples that I've found, there's subscribtion to the drawingArea.ExposeEvent event, a method like:

void OnDrawingAreaExposed (object source, ExposeEventArgs args)
{   
    DrawingArea darea = (DrawingArea) source;
    Cairo.Context ctx = Gdk.CairoHelper.Create (darea.GdkWindow);

    // drawing here ...

    ((IDisposable) ctx.Target).Dispose();
    ((IDisposable) ctx).Dispose();
}

But I have neither .ExposeEvent event available on the DrawingArea object, nor the ExposeEventArgs data type. I don't even know what it's about. I would like to deal with rendering, perhaps even in other way, for example to make a bitmap image then assign it to something like PictureBox from WinForms.

Also, I have to draw at set intervals, which means I need other GTK# methods too.

I used darea.GdkWindow to create the Cairo.Context, but it is maked as "deprecated". I've tried just creating a Cairo.Context and then drawing with the darea.Draw(cc) method, but that doesn't work either.


Solution

  • Use the DrawingArea.Drawn event.

    var drawing = new DrawingArea();
    drawing.Drawn += (s, e) =>
    {
        var cr = e.Cr;
        cr.Rectangle(100, 100, 100, 100);
        cr.SetSourceRGB(255, 0, 0);
        cr.Fill();
    };
    

    Reference: https://github.com/GtkSharp/GtkSharp/blob/develop/Source/Samples/Sections/Widgets/DrawingAreaSection.cs