How to use the Gdk # library in C # (mono)?

I am a complete newbie with Gtk # and Gdk # and I completely lost how to get started.

All I'm trying to do is draw dots and lines in any widget / pixmap / image and then display them in my gtk application.
So far, I understand that I have to create a Gdk.drawable object to access the DrawPoints(Point[] points) and DrawLine(Point[] points) methods. However, to create an instance of the Drawable I need a Gdk.GC object. Both object constructors accept the IntPtr parameter, and I donโ€™t know what IntPtr I have to pass here ?! Or the GC constructor could also take a Drawable object, and the Drawable constructor might take a GC object ... I'm spinning here!

24 hours of online research was done and, apart from some python examples that use constructors that take no parameter, I could not find any resource to get started in C #. So, can someone show me how to properly use these GC and Drawable objects to draw a line using the DrawLine(Point[] points) method?

 GDKEXAMPLE(){ win = new Gtk.Window ("Gdk nightmare"); win.SetDefaultSize (400, 300); img=new Gtk.Image(); Drawable dr=new Drawable(null); //how to instantiate this object? Gdk.GC gc=new Gdk.GC(null); //how to instantiate this object? Point[] pts=new Point[3]; pts[0]=new Point(10,10); pts[1]=new Point(15,20); pts[2]=new Point(20,50); dr.DrawLines(gc,pts); Pixmap pxmp=new Pixmap(dr,100,100); img.SetFromPixmap(pxmp,pxmp); //Requests a pixmap and pixmap mask: what mask? img.QueueDraw(); win.Add(img); win.ShowAll(); } 

Can someone help me use the Gdk.GC and Gdk.drawable class and then display any dots or lines in Gtk widgets please? perhaps making the above code work? or any link to some tutorial in C # using this gdk library?

+6
source share
1 answer

So, after a lot of patience, I eventually found the answer to the question of how to get started with Gdk #.

In principle, only events can trigger a drawing. I tried calling the drawings directly from the methods, but this did not work. Here is the base code that I was missing in C #:

 using System; using Gdk; using Gtk; class GdkApp : Gtk.Window { public static void Main() { Application.Init(); new GdkApp(); Application.Run(); } public GdkApp() : base("Simple drawing") { SetDefaultSize(230, 150); DeleteEvent+=delegate {Application.Quit(); }; ShowAll(); } protected override bool OnExposeEvent (EventExpose evnt) { bool ok = base.OnExposeEvent (evnt); draw (); return ok; } void draw() { Gdk.GC gc = new Gdk.GC ((Drawable)base.GdkWindow); gc.RgbFgColor = new Gdk.Color (255, 50, 50); gc.RgbBgColor = new Gdk.Color (0, 0, 0); gc.SetLineAttributes (3, LineStyle.OnOffDash, CapStyle.Projecting, JoinStyle.Round); Gdk.Point[] pts = new Gdk.Point[8]; pts [0] = new Gdk.Point (10, 50); pts [1] = new Gdk.Point (15, 70); pts [2] = new Gdk.Point (20, 80); pts [3] = new Gdk.Point (25, 70); pts [4] = new Gdk.Point (30, 80); pts [5] = new Gdk.Point (40, 90); pts [6] = new Gdk.Point (55, 85); pts [7] = new Gdk.Point (75, 65); base.GdkWindow.DrawLines (gc, pts); } } 

Here we create the main window and record the entire drawing in the OnExposeEvent method. This method is called when any widget must (in whole or in part) be redrawn. There you need to create an instance of the Gdk.GC object (context) using the "Drawable" parameter. Not much choice: Gdk.Pixmap or Gdk.Window. I chose Gdk.Window, as it was the easiest surface. You can then customize the context object to decide how it will be drawn.

Finally, and directly from the Drawable object that you want to draw, you can call the drawing methods: here I just wanted to build a series of 8 connected points.

Hope this helps some other users understand the logic of this gdk # namespace.

+6
source

Source: https://habr.com/ru/post/907486/


All Articles