Blog Stats
  • Posts - 62
  • Articles - 11
  • Comments - 9
  • Trackbacks - 62

 

Analog Clock - C# version

DonXML AnalogClock
// Developed using C# v5.0
namespace ClockExample
{
    // This will be used as Style for the hours
    private UI TextHour : TextArea
    {
        FontWeight="Normal";
        FontSize="15";
        Foreground="#00F7D6";
    }
    // This is the actual clock layout
    private UI ClockCanvas : Canvas(210,210)
    {
        // The Clock Frame
        Ellipse(100,100,98,98){
            Stroke="Black";
            StrokeThickness="1";
            Fill.LinearGradientBrush.GradientStops.GradientStopCollection{
                GradientStop{ Color="silver" ; Offset="0.05"; }
                GradientStop{ Color="#333333"; Offset="0.95"; }
            }
        }
        Ellipse(100,100,85,85){
            Stroke="Black";
            StrokeThickness="1";
            Fill.LinearGradientBrush.GradientStops.GradientStopCollection{
                GradientStop{ Color="#333333"; Offset="0.05"; }
                GradientStop{ Color="silver" ; Offset="0.95"; }
            }
        }
        // The Shadow
        Ellipse(108,108,98,98){
            Stroke="#FFFFFF00";
            StrokeThickness="0";
            Fill.SolidColorBrush{ Color="Black"; Opacity="0.3"; }
        }
        
        // The Clock Face
        Ellipse(100,100,80,80){
            Stroke="#666666";
            StrokeThickness="1";
            Fill.SolidColorBrush.Color="Black";
        }
        
        // The Hours
        TextHour( 32,130){ Text="1"; }
        TextHour( 57,154){ Text="2"; }
        TextHour( 92,165){ Text="3"; }
        TextHour(130,155){ Text="4"; }
        TextHour(152,130){ Text="5"; }
        TextHour(161, 96){ Text="6"; }
        TextHour(152, 62){ Text="7"; }
        TextHour(130, 37){ Text="8"; }
        TextHour( 92, 27){ Text="9"; }
        TextHour( 57, 34){ Text="10";}
        TextHour( 32, 58){ Text="11";}
        TextHour( 23, 92){ Text="12";}
        
        // The Hands
        hours:TransformDecorator{
            AffectsLayout="False";
            Transform.TransformCollection{
                RotateTransform{ Center="0,0"; Angle="0"; }
                RotateTransform{ Center="0,0"; 
                    AngleAnimations.DoubleAnimation{ From="0"; To="360"; Duration="43200"; RepeatDuration="Indefinite"; }
                }
                TranslateTransform{ X="100"; Y="100"; }
           }
           Path{ Data="M -2.5,0 h 5 v -40 h -5 z"; Fill="#00F7D6"; Stroke="black"; StrokeThickness="0.1"; }
        }
        minutes:TransformDecorator{
            AffectsLayout="False";
            Transform.TransformCollection{
                RotateTransform{ Center="0,0"; Angle="0"; }
                RotateTransform{ Center="0,0";
                    AngleAnimations.DoubleAnimation{ From="0"; To="360"; Duration="3600"; RepeatDuration="Indefinite"; }
                }
                TranslateTransform{ X="100"; Y="100"; }
            }
            Path{ Data="M -2,0 h 4 v -65 h -4 z"; Fill="#00F7D6"; Stroke="black"; StrokeThickness="0.1"; }
        }
        seconds:TransformDecorator{
            AffectsLayout="False";
            Transform.TransformCollection{
                RotateTransform{ Center="0,0"; Angle="0"; }
                RotateTransform{ Center="0,0";
                    AngleAnimations.DoubleAnimation{ From="0"; To="360"; Duration="60"; RepeatDuration="Indefinite"; }
                }
                TranslateTransform{ X="100"; Y="100"; }
            {
            Path{ Data="M -1,0 h 2 v -70 h -2 z"; Fill="red"; Stroke="black"; StrokeThickness="0.1"; }
        } 
        
        // The Center Disk
        Ellipse(100,100,8,8){
            Stroke="#00F7D6";
            StrokeThickness="1";
            Fill.SolidColorBrush.Color="Black";
        }
    }
    // The class inherits from the UI
    public class AnalogClock : ClockCanvas
    {
        // The OnLoaded handler can be run automatically when the class is loaded.
        private void OnLoaded(object sender, EventArgs e) 
        {
             // Autocasting ON for better readability. See C# v5.0 manual
             CurrentDateTime = DateTime.Now; 
             CurrentSecond   = CurrentDateTime.Second; 
             CurrentMinute   = CurrentDateTime.Minute + (CurrentSecond / 60) ; 
             CurrentHour     = CurrentDateTime.Hour   + (CurrentMinute / 60) ; 
             SecondsTransforms = seconds.Transform; 
             SecondsIntitalRotation = SecondsTransforms[0]; 
             SecondsIntitalRotation.Angle = CurrentSecond * 6; 
              
             MinutesTransforms = minutes.Transform; 
             MinutesIntitalRotation = MinutesTransforms[0]; 
             MinutesIntitalRotation.Angle = CurrentMinute * 6;                 
             HoursTransforms = hours.Transform; 
             HoursIntitalRotation = HoursTransforms[0]; 
             HoursIntitalRotation.Angle = CurrentHour * 30;              
        }
    }
}
// End of code

Feedback

# re: Analog Clock - C# version

Gravatar A simple one, http://codelog.blogial.com/2008/12/12/analog-clock-in-c/ 12/18/2008 6:09 AM | Sudarsan Srinivasan

Post a comment





 

 

 

Copyright © RebelGeekz