CGC_Codes

Clock

Jul 3rd, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.08 KB | None | 0 0
  1. using System;
  2. using Gtk;
  3. using Cairo;
  4.  
  5. class Simpleclock : DrawingArea
  6. {
  7.  
  8.     private static Gtk.Window win = null;
  9.  
  10.    
  11.     static void Main()
  12.     {
  13.         Application.Init();
  14.         new Simpleclock();
  15.         Application.Run();
  16.     }
  17.  
  18.    
  19.     Simpleclock()
  20.     {
  21.         win = new Window("Simpleclock");
  22.         win.SetDefaultSize(256, 256);
  23.         win.DeleteEvent += new DeleteEventHandler(OnQuit);
  24.         GLib.Timeout.Add(500, UpdateClock);
  25.         win.Add(this);
  26.         win.ShowAll();
  27.     }
  28.  
  29.    
  30.     void Draw(Context cr, uint width, uint height)
  31.     {
  32.         uint boxSize = Math.Min(width, height);
  33.  
  34.         DateTime date = DateTime.Now;
  35.  
  36.         cr.Save();
  37.         cr.Color = new Color(1, 1, 1);
  38.         cr.Rectangle(0, 0, width, height);
  39.         cr.Fill();
  40.  
  41.        
  42.         cr.Translate((width - boxSize) / 2.0, (height - boxSize) / 2.0);
  43.        
  44.         cr.Scale(boxSize / 2.0, boxSize / 2.0);
  45.        
  46.         cr.Translate(1.0, 1.0);
  47.  
  48.         cr.Rotate(-Math.PI / 2.0);
  49.  
  50.        
  51.         DrawClockFace(cr);
  52.         DrawHourHand(cr, (uint)date.Hour);
  53.         DrawMinuteHand(cr, (uint)date.Minute);
  54.         DrawSecondHand(cr, (uint)date.Second);
  55.         DrawPin(cr);
  56.         cr.Restore();
  57.     }
  58.  
  59.    
  60.     void DrawClockFace(Context cr)
  61.     {
  62.         cr.Save();
  63.         cr.Color = new Color(0, 0, 0);
  64.         cr.Arc(0.0, 0.0, 0.95, 0.0, 2.0 * Math.PI);
  65.         cr.LineWidth = 0.01;
  66.         cr.Stroke();
  67.         cr.Restore();
  68.     }
  69.  
  70.    
  71.     void DrawHourHand(Context cr, uint hour)
  72.     {
  73.         double rot = (double)(hour % 12) / 12.0;
  74.         cr.Save();
  75.         cr.Rotate(rot * Math.PI * 2.0);
  76.         cr.Rectangle(0.0, -0.1, 0.6, 0.2);
  77.         cr.Color = new Color(0, 0, 0);
  78.         cr.Fill();
  79.         cr.Restore();
  80.     }
  81.  
  82.    
  83.     void DrawMinuteHand(Context cr, uint minute)
  84.     {
  85.         double rot = (double)minute / 60.0;
  86.         cr.Save();
  87.         cr.Rotate(rot * Math.PI * 2.0);
  88.         cr.Rectangle(0.0, -0.05, 0.8, 0.1);
  89.         cr.Color = new Color(0, 0, 0);
  90.         cr.Fill();
  91.         cr.Restore();
  92.     }
  93.  
  94.    
  95.     void DrawSecondHand(Context cr, uint second)
  96.     {
  97.         double rot = (double)second / 60.0;
  98.         cr.Save();
  99.         cr.Rotate(rot * Math.PI * 2.0);
  100.         cr.Rectangle(0.0, -0.025, 0.9, 0.05);
  101.         cr.Color = new Color(0, 0, 0);
  102.         cr.Fill();
  103.         cr.Restore();
  104.     }
  105.  
  106.    
  107.     void DrawPin(Context cr)
  108.     {
  109.         cr.Save();
  110.         cr.Arc(0.0, 0.0, 0.1, 0.0, 2.0 * Math.PI);
  111.         cr.Color = new Color(0, 0, 0);
  112.         cr.Fill();
  113.         cr.Restore();
  114.     }
  115.  
  116.    
  117.     private static bool UpdateClock()
  118.     {
  119.         win.QueueDraw();
  120.         return true;
  121.     }
  122.  
  123.    
  124.     protected override bool OnExposeEvent(Gdk.EventExpose e)
  125.     {
  126.         using (Context cr = Gdk.CairoHelper.Create(e.Window))
  127.         {
  128.             int w, h;
  129.             e.Window.GetSize(out w, out h);
  130.             Draw(cr, (uint)w, (uint)h);
  131.         }
  132.         return true;
  133.     }
  134.  
  135.    
  136.     void OnQuit(object sender, DeleteEventArgs e)
  137.     {
  138.         Application.Quit();
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment