[Mono-list] Basic Cario drawing

Marcin S msporysz06 at gmail.com
Sun Jul 8 18:58:07 UTC 2012


Hello,
I'm hoping this is a right list.
I'm relatively new to C# & mono, and not so quick at comprehending
some design aspects especially GUI related.
I  have an idea for project, but i've hit the wall pretty early. It is
Cairo related. First, for covering the basics I'm trying to draw
something easy on designated area after button is pressed, but program
is crashing after button is pressed with following error:

The program 'Plutno' received an X Window System error.
This probably reflects a bug in the program.
The error was 'BadDrawable (invalid Pixmap or Window parameter)'.
  (Details: serial 552 error_code 9 request_code 53 minor_code 0)

Short testing program code:

	public class Rysownik: Gtk.Window
	{
		VBox box;
		Button draw;
		DrawingArea can;
		Context ctx;
		
		
		public Rysownik (): base(Gtk.WindowType.Toplevel)
		{
			
			Resize(600,700);
			
			box = new VBox();
			draw = new Button("Draw!");
			canvas = new DrawingArea();
			
			draw.Clicked += OnDrawButton;
			
			canvas.ExposeEvent += OnCanExposeEvent;

			box.PackStart(can);
			box.PackStart(draw);
			
			Add (box);
			
			
		}

		void OnDrawButton (object sender, EventArgs e)
		{
			PointD p1,p2,p3,p4;
		    p1 = new PointD (5,5);
		    p2 = new PointD (6,6);
		    p3 = new PointD (7,7);
		    p4 = new PointD (9,10);
					
		    ctx.MoveTo (p1);
		    ctx.LineTo (p2);
		    ctx.LineTo (p3);
		    ctx.LineTo (p4);
		    ctx.LineTo (p1);
		    ctx.ClosePath ();
	            ctx.Stroke();
		}

		void OnCanExposeEvent (object o, ExposeEventArgs args)
		{
			DrawingArea ar = (DrawingArea) o;
			ctx = Gdk.CairoHelper.Create(ar.GdkWindow);
		}
		
		public override void Dispose ()
		{
			base.Dispose ();
			((IDisposable) ctx.Target).Dispose ();
   			((IDisposable) ctx).Dispose ();
		}
	}


If i move drawing code to OnCanExposeEvent it works, but this is "one
shot" action only when window is rendered on desktop, right?
I want to draw after button is pressed.
Tips please.


More information about the Mono-list mailing list