1. 程式人生 > 其它 >WPF例項(一)

WPF例項(一)

技術標籤:WPFwpf

時鐘程式

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace CLOCK { using System.Threading; using System.Windows.Threading; /// <summary> /// MainWindow.xaml 的互動邏輯 /// </summary> public partial class MainWindow : Window {
//計時器 System.Timers.Timer timer = new System.Timers.Timer(1000); public MainWindow() { InitializeComponent(); #region 初始化時間 secondPointer.Angle = DateTime.Now.Second * 6; minutePointer.Angle = DateTime.Now.Minute * 6; hourPointer.
Angle = (DateTime.Now.Hour * 30) + (DateTime.Now.Minute * 0.5); this.labTime.Content = DateTime.Now.ToString("HH:mm:ss"); #endregion timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed); timer.Enabled = true; } private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { //進行拖放移動 this.DragMove(); } private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { //UI非同步更新 this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() => { //秒針轉動,秒針繞一圈360度,共60秒,所以1秒轉動6度 secondPointer.Angle = DateTime.Now.Second * 6; //分針轉動,分針繞一圈360度,共60分,所以1分轉動6度 minutePointer.Angle = DateTime.Now.Minute * 6; //時針轉動,時針繞一圈360度,共12時,所以1時轉動30度。 //另外同一個小時內,隨著分鐘數的變化(繞一圈60分鐘),時針也在緩慢變化(轉動30度,30/60=0.5) hourPointer.Angle = (DateTime.Now.Hour * 30) + (DateTime.Now.Minute * 0.5); //更新時間值 this.labTime.Content = DateTime.Now.ToString("HH:mm:ss"); })); } } }
<Window x:Class="CLOCK.MainWindow"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         Title="MainWindow" Margin="2" Height="327" Width="311" AllowsTransparency="True" 
         WindowStyle="None" Background="Transparent" WindowStartupLocation="CenterScreen"  
         ResizeMode="NoResize" Topmost="False" Opacity="1">

    <Grid Width="300" Height="300" MouseLeftButtonDown="Grid_MouseLeftButtonDown">
        <Grid.Background>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FF5E8120" Offset="0"/>
                <GradientStop Color="#FFF0B311" Offset="1"/>
            </LinearGradientBrush>
        </Grid.Background>
        <Ellipse HorizontalAlignment="Left" Height="262" Margin="18,20,0,0" VerticalAlignment="Top" Width="265">
            <Ellipse.Fill>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#FFCFB724" Offset="0"/>
                    <GradientStop Color="#FFA3E445" Offset="1"/>
                </LinearGradientBrush>
            </Ellipse.Fill>
            <Ellipse.Stroke>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#FFE8CA11" Offset="0"/>
                    <GradientStop Color="#FFA3E445" Offset="1"/>
                </LinearGradientBrush>
            </Ellipse.Stroke>
        </Ellipse>

        <Label x:Name="lab商標" Foreground="White" Margin="0,0,0,211" HorizontalAlignment="Center" VerticalAlignment="Bottom" Height="Auto" Width="Auto" FontSize="13" Content="SunMoon" />
        <Label x:Name="lab建立時間" Foreground="White" Margin="0,91,0,0" HorizontalAlignment="Center" VerticalAlignment="Top" Height="Auto" Width="Auto" Content="2020"/>
        <!-- 秒針定義  -->
        <Rectangle Margin="150,0,149,150" x:Name="rectangleSecond" Stroke="#FFF1A115" Height="120" VerticalAlignment="Bottom" Width="1">
            <Rectangle.RenderTransform>
                <RotateTransform x:Name="secondPointer" CenterX="0" CenterY="120" Angle="0"/>
            </Rectangle.RenderTransform>
        </Rectangle>

        <Rectangle Margin="150,50,149,150" x:Name="rectangleMinute" Stroke="#FF12A612" Width="1">

            <Rectangle.RenderTransform>
                <RotateTransform x:Name="minutePointer" CenterX="0" CenterY="100" Angle="45"/>
            </Rectangle.RenderTransform>

        </Rectangle>

        <!-- 時針定義  -->

        <Rectangle Margin="150,80,149,150" x:Name="rectangleHour" Stroke="#FFF3F30B" Width="1">

            <Rectangle.RenderTransform>

                <RotateTransform x:Name="hourPointer" CenterX="0" CenterY="70" Angle="90" />

            </Rectangle.RenderTransform>

        </Rectangle>

        <Label Content="00:00:00" FontSize="16" Foreground="White" Height="Auto" HorizontalAlignment="Center" Margin="114,0,113,86" x:Name="labTime" VerticalAlignment="Bottom" Width="Auto" />

    </Grid>

</Window>