Residuum
Residuum

Reputation: 12064

Drawn content lost on window overlap in GTK#

I am using Cairo in a GTK# application for drawing. When another window covers part of the drawn content, the overlapped part of the drawn content is lost. Is there a way to make it permanent?

Here is my simplified method for drawing the content:

void UpdateConnectionLines ()
{
    GdkWindow myWindow = GetGdkWindow();
    myWindow.Clear ();
    using (Context g = Gdk.CairoHelper.Create (myWindow))
    {
        g.Save ();
        g.MoveTo (0, 20);
        g.LineTo (100, 20);
        g.Restore ();
        g.Color = new Color (0, 0, 0);
        g.LineWidth = 1;
        g.Stroke();
    }
}

Upvotes: 2

Views: 719

Answers (2)

Residuum
Residuum

Reputation: 12064

Evaluating John Koerner's answer, I have found a solution, that works for every GTK# widget. I use the generic WidgetEvent ExposeEvent (thanks, ptomato) and redraw.

I append my event handler with

this.ExposeEvent += new global::Gtk.ExposeEventHandler (this.Handle_ExposeEvent);

and then the handler just calls my method:

protected virtual void Handle_ExposeEvent (object o, Gtk.ExposeEventArgs args)
{
    UpdateConnectionLines();
}

EDIT:

Actually, I have not RTFM correctly, as it explicitely states:

The best place to create and use the Context is the ExposeEvent for the given widget. Usually you'll want to use the Gtk.DrawingArea for this task. An example implementation of the Expose event method:

Upvotes: -1

John Koerner
John Koerner

Reputation: 38087

If you are drawing directly on the form, then you need to do it in the Form's paint event, to ensure it is there every time the form get's painted (i.e. when another window covers it and then moves, when it is resized, ...)

Upvotes: 3

Related Questions