using Gtk; using Cairo; using System; class Burning : DrawingArea { // Числа, які буде відображено внизу. string[] num = new string[] {"75","150","225","300","375","450","525","600","675"}; public Burning() : base() {SetSizeRequest(-1, 30);} protected override bool OnExposeEvent(Gdk.EventExpose args) { Cairo.Context cr = Gdk.CairoHelper.Create(args.Window); cr.LineWidth = 1.0; cr.SelectFontFace("DejaVu Serif Condensed", FontSlant.Normal, FontWeight.Normal); cr.SetFontSize(18); int width = Allocation.Width; // Отримання батьківського засобу SharpApp parent = (SharpApp) GetAncestor (Gtk.Window.GType); // Отримання поточного значення з батьківського засобу int cur_width = parent.CurValue; int step = (int) Math.Round(width / 10.0); // Визначення загального розміру для малювання за значенням, що надходить від повзунка int till = (int) ((width / 750.0) * cur_width); // Визначення точки переповнення, з якої малюють червоним кольором. int full = (int) ((width / 750.0) * 700); if (cur_width >= 700) { cr.SetSourceRGB(1.0,1.0,0.0); // Жовтий колір cr.Rectangle(0, 0, full, 30); // Позначення області заповнення до переповнення cr.Clip(); cr.Paint(); cr.ResetClip(); cr.SetSourceRGB(1.0,0.0,0.0); // Червоний колір cr.Rectangle(full,0,till-full,30);// Позначення області переповнення cr.Clip(); cr.Paint(); cr.ResetClip(); } else { cr.SetSourceRGB(1.0,1.0,0.0); // Жовтий колір cr.Rectangle(0, 0, till, 30); // Позначення області заповнення до переповнення cr.Clip(); cr.Paint(); cr.ResetClip(); } cr.SetSourceRGB(0.0,0.0,0.0); // Чорний колір for (int i=1; i<=num.Length; i++) // Відображення вертикальних рисок і елементів масиву num { cr.MoveTo(i*step, 0); cr.LineTo(i*step, 5); cr.Stroke(); TextExtents extents = cr.TextExtents(num[i-1]); // Для подальшого правильного розташування cr.MoveTo(i*step-extents.Width/2, 25); cr.TextPath(num[i-1]); cr.Stroke(); } ((IDisposable) cr.GetTarget()).Dispose(); // Вилучення сміття ((IDisposable) cr).Dispose(); return true; } } class SharpApp : Window { int cur_value = 0; Burning burning; public SharpApp() : base("Відображення шкали") { SetDefaultSize(450, 100); SetPosition(WindowPosition.Center); DeleteEvent += delegate {Application.Quit();}; VBox vbox = new VBox(false, 2); HScale scale = new HScale(0, 750, 1); scale.SetSizeRequest(430, 35); scale.ValueChanged += OnChanged; // Обробник події пересування повзунка Fixed fix = new Fixed(); fix.Put(scale, 10, 10); vbox.PackStart(fix); burning = new Burning(); vbox.PackStart(burning, false, false, 0); Add(vbox); ShowAll(); } void OnChanged(object sender, EventArgs args) // Обробник події пересування повзунка { Scale scale = (Scale) sender; // Отримання значення зі шкали cur_value = (int) scale.Value; // Збереження отриманого значення для подальшого використання burning.QueueDraw(); // Перемалювання засобу } public int CurValue {get {return cur_value;}} public static void Main() { Application.Init(); new SharpApp(); Application.Run(); } }