using System; using System.Reflection; using System.Runtime.InteropServices; using Cairo; using Gtk; public class Example { static void Main () { Application.Init (); Gtk.Window w = new Gtk.Window ("Заокруглення сторін і кутів прямокутника"); DrawingArea a = new CairoGraphic (); Box box = new HBox (true, 0); box.Add (a); w.Add (box); w.Resize (500, 230); w.DeleteEvent += close_window; w.ShowAll (); Application.Run (); } static void close_window (object obj, DeleteEventArgs args) { Application.Quit (); } } public class CairoGraphic : DrawingArea { // визначення найменшого елемента послідовності // (використано при заокругленні кутів) static double min (params double[] arr) { int minp = 0; for (int i = 1; i < arr.Length; i++) if (arr[i] < arr[minp]) minp = i; return arr[minp]; } // створення контура прямокутника із заокругленими кутами static void DrawRoundedRectangle (Cairo.Context gr, double x, double y, double w, double h, double r) { gr.Save (); if ((r > h/2) || (r > w/2)) r = min (h/2, w/2); gr.MoveTo (x, y + r); gr.Arc (x + r, y + r, r, Math.PI, -Math.PI/2); gr.LineTo (x + w - r, y); gr.Arc (x + w - r, y + r, r, -Math.PI/2, 0); gr.LineTo (x + w, y + h - r); gr.Arc (x + w - r, y + h - r, r, 0, Math.PI/2); gr.LineTo (x + r, y + h); gr.Arc (x + r, y + h - r, r, Math.PI/2, Math.PI); gr.ClosePath (); gr.Restore (); } // створення контура прямокутника із заокругленими кутами і сторонами // за допомогою кривих Безьє static void DrawCurvedRectangle (Cairo.Context gr, double x, double y, double w, double h) { gr.Save (); gr.MoveTo (x, y + h / 2); gr.CurveTo (x, y, x, y, x + w / 2, y); gr.CurveTo (x + w, y, x + w, y, x + w, y + h / 2); gr.CurveTo (x + w, y + h, x + w, y + h, x + w / 2, y + h); gr.CurveTo (x, y + h, x, y + h, x, y + h / 2); gr.Restore (); } protected override bool OnExposeEvent (Gdk.EventExpose args) { using (Context g = Gdk.CairoHelper.Create (args.Window)) { DrawRoundedRectangle (g, 10, 10, 100, 200, 40); DrawCurvedRectangle (g, 130, 20, 350, 190); g.SetSourceColor(new Color (0, 1, 0, 1)); g.FillPreserve (); g.SetSourceColor(new Color (0, 0, 1, 0.5)); g.LineWidth = 15; g.Stroke (); } return true; } }