Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Gtk;
- using Cairo;
- class Simpleclock : DrawingArea
- {
- private static Gtk.Window win = null;
- static void Main()
- {
- Application.Init();
- new Simpleclock();
- Application.Run();
- }
- Simpleclock()
- {
- win = new Window("Simpleclock");
- win.SetDefaultSize(256, 256);
- win.DeleteEvent += new DeleteEventHandler(OnQuit);
- GLib.Timeout.Add(500, UpdateClock);
- win.Add(this);
- win.ShowAll();
- }
- void Draw(Context cr, uint width, uint height)
- {
- uint boxSize = Math.Min(width, height);
- DateTime date = DateTime.Now;
- cr.Save();
- cr.Color = new Color(1, 1, 1);
- cr.Rectangle(0, 0, width, height);
- cr.Fill();
- cr.Translate((width - boxSize) / 2.0, (height - boxSize) / 2.0);
- cr.Scale(boxSize / 2.0, boxSize / 2.0);
- cr.Translate(1.0, 1.0);
- cr.Rotate(-Math.PI / 2.0);
- DrawClockFace(cr);
- DrawHourHand(cr, (uint)date.Hour);
- DrawMinuteHand(cr, (uint)date.Minute);
- DrawSecondHand(cr, (uint)date.Second);
- DrawPin(cr);
- cr.Restore();
- }
- void DrawClockFace(Context cr)
- {
- cr.Save();
- cr.Color = new Color(0, 0, 0);
- cr.Arc(0.0, 0.0, 0.95, 0.0, 2.0 * Math.PI);
- cr.LineWidth = 0.01;
- cr.Stroke();
- cr.Restore();
- }
- void DrawHourHand(Context cr, uint hour)
- {
- double rot = (double)(hour % 12) / 12.0;
- cr.Save();
- cr.Rotate(rot * Math.PI * 2.0);
- cr.Rectangle(0.0, -0.1, 0.6, 0.2);
- cr.Color = new Color(0, 0, 0);
- cr.Fill();
- cr.Restore();
- }
- void DrawMinuteHand(Context cr, uint minute)
- {
- double rot = (double)minute / 60.0;
- cr.Save();
- cr.Rotate(rot * Math.PI * 2.0);
- cr.Rectangle(0.0, -0.05, 0.8, 0.1);
- cr.Color = new Color(0, 0, 0);
- cr.Fill();
- cr.Restore();
- }
- void DrawSecondHand(Context cr, uint second)
- {
- double rot = (double)second / 60.0;
- cr.Save();
- cr.Rotate(rot * Math.PI * 2.0);
- cr.Rectangle(0.0, -0.025, 0.9, 0.05);
- cr.Color = new Color(0, 0, 0);
- cr.Fill();
- cr.Restore();
- }
- void DrawPin(Context cr)
- {
- cr.Save();
- cr.Arc(0.0, 0.0, 0.1, 0.0, 2.0 * Math.PI);
- cr.Color = new Color(0, 0, 0);
- cr.Fill();
- cr.Restore();
- }
- private static bool UpdateClock()
- {
- win.QueueDraw();
- return true;
- }
- protected override bool OnExposeEvent(Gdk.EventExpose e)
- {
- using (Context cr = Gdk.CairoHelper.Create(e.Window))
- {
- int w, h;
- e.Window.GetSize(out w, out h);
- Draw(cr, (uint)w, (uint)h);
- }
- return true;
- }
- void OnQuit(object sender, DeleteEventArgs e)
- {
- Application.Quit();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment