#freeze
[[.NET]]

#ref(takoruka.png)
(たこルカは付属しません。画像ロードを試した結果です)

みればわかると思うけど、逆回転時計よ。

起動すると三つのボタンが出るの。他はともかく、そこについては説明しておくわ。

- 右下の赤いボタンは、終了。
- 右上のボタンは、逆回転時計とノーマルの切り替え。
- 左上のボタンは、何か背景画像をロードする時に使ってね。

Canvasを使ったり、うちでは馴染みのないデータの塊だから、全部載せてみるわね。プロジェクト名は clock1 よ。

今度、部分部分の詳しい解説も書いてみるわね。

* MainWindow.xaml [#h879ccfa]

 <Window x:Name="w1" x:Class="clock1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" VerticalAlignment="Center" VerticalContentAlignment="Center" Height="231" Width="221" WindowStyle="None">
    <Canvas x:Name="canvas1" Width="200" Height="200" MouseLeftButtonDown="canvasleftdown">
        <Canvas.Background>
            <ImageBrush x:Name="ibb" Opacity="0.8" />
        </Canvas.Background>
        <Line x:Name="HourLine"  Stroke="Black" Fill="Black" 
              X1="100" Y1="100" X2="100" Y2="35"              
               StrokeThickness="3" >
            <Line.RenderTransform>
                <RotateTransform  x:Name="AngleHour" Angle="0" 
                                  CenterX="100" CenterY="100"/>
            </Line.RenderTransform>
        </Line>
        <Line x:Name="MinuteLine"  Stroke="Black" Fill="Black" 
              X1="100" Y1="100" X2="100" Y2="15"              
               StrokeThickness="2" >
            <Line.RenderTransform>
                <RotateTransform x:Name="AngleMinute"  Angle="0" 
                                 CenterX="100" CenterY="100"/>
            </Line.RenderTransform>
        </Line>
        <Line x:Name="SecondLine"  Stroke="Red" Fill="LightGray"
              X1="100" Y1="120" X2="100" Y2="10"  >
            <Line.RenderTransform>
                <RotateTransform x:Name="AngleSecond" Angle="0"  
                                 CenterX="100" CenterY="100"/>
            </Line.RenderTransform>
        </Line>
        <Ellipse Fill="Black" Width="12" Height="12" HorizontalAlignment="Center" VerticalAlignment="Center" 
                 Canvas.Top="94" Canvas.Left="94"/>
        <Button x:Name="picButton" Content="Button" Width="12" Height="10" RenderTransformOrigin="1.167,0" VerticalAlignment="Top" HorizontalAlignment="Left" Click="picButton_Click" Canvas.Top="-6"/>
        <Button x:Name="revButton" Content="Button" Width="12" Height="10" RenderTransformOrigin="1.167,0" VerticalAlignment="Top" HorizontalAlignment="Left" Canvas.Left="188" Click="revButton_Click" Canvas.Top="-6" />
        <Button x:Name="endButton" Content="Button" Width="12" Height="10" RenderTransformOrigin="1.167,0" HorizontalContentAlignment="Right" VerticalContentAlignment="Bottom" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0" Canvas.Left="188" Canvas.Top="197" Click="endButtonClick" Background="Red" />
        <TextBox x:Name="tvtext" Height="23" TextWrapping="Wrap" Text="TextBox" Canvas.Top="184" Width="120" IsReadOnly="True" IsTabStop="False"/>
    </Canvas>
 </Window>

* MainWindow.xaml.cs [#x8c650c9]

 using System;
 using System.Collections.Generic;
 using System.Drawing;
 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;
 using System.Windows.Threading;
 using Microsoft.Win32;
  
 namespace clock1
 {
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        private int second = -1;
        private bool reverse = true;
        private BitmapImage b_ima = null;
        private Point mp = new Point(); // 場所記憶用
 
        public MainWindow()
        {
            InitializeComponent();
 
            //
            DispatcherTimer dt = new DispatcherTimer();
            dt.Interval = new TimeSpan(0, 0, 0,0,50);
            dt.IsEnabled = true;
            dt.Tick += new EventHandler(dt_Tick);
 
            DateTime tt = DateTime.Now;
            updateClock(tt);
 
        }
 
        private void updateClock(DateTime dt)
        {
            //w1.Title = dt.ToString("HH:mm:ss");
            tvtext.Text = dt.ToString("HH:mm:ss");
            if (reverse == true)
            {
                this.AngleSecond.Angle = 360 - (dt.Second * 360.0 / 60.0);
                this.AngleMinute.Angle = 360 - ((dt.Minute + dt.Second / 60.0) * 360.0 / 60.0);
                this.AngleHour.Angle = 360 - ((dt.Hour + dt.Minute / 60.0) * 360.0 / 12);
            }
            else
            {
                this.AngleSecond.Angle = dt.Second * 360.0 / 60.0;
                this.AngleMinute.Angle = (dt.Minute + dt.Second / 60.0) * 360.0 / 60.0;
                this.AngleHour.Angle = (dt.Hour + dt.Minute / 60.0) * 360.0 / 12;
            }
        }
 
        private void dt_Tick(object sender, EventArgs s)
        {
            DateTime dt = DateTime.Now;
            if (second != dt.Second)
            {
                updateClock(dt);
            }
        }
 
        private void revButton_Click(object sender, RoutedEventArgs e)
        {
            if (reverse == false)
            {
                reverse = true;
            }
            else
            {
                reverse = false;
            }
            DateTime tt = DateTime.Now;
            updateClock(tt);
        }
 
        private void picButton_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.FilterIndex = 0;
            openFileDialog.Filter = "Picture Files(*.jpg;*.png;*.bmp)|*.jpg;*.png;*.bmp|All Files (*.*)|*.*";
            bool? result = openFileDialog.ShowDialog();
            if (result == true)
            {
                // 既にあったら解放。
                b_ima = null;
                //
                b_ima = new BitmapImage();
                b_ima.BeginInit();
                b_ima.UriSource = new Uri(openFileDialog.FileName, UriKind.Relative);
                b_ima.EndInit();
                ibb.ImageSource = b_ima;
                ibb.Stretch = Stretch.Uniform;
                //canvas1 = b_ima;
            }
        }
 
        private void canvasleftdown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }
 
        private void endButtonClick(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
 
    }
 }

トップ   編集 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS