简易的温度计控件实现

来源:互联网 发布:复权价格 知乎 编辑:程序博客网 时间:2024/04/30 15:52
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 Weather_Glasses_Control{    /// <summary>    /// UserControl1.xaml 的交互逻辑    /// </summary>    public partial class Weather_Glasses : UserControl    {        private double _maxT;                   //最大温度        private double _minT;                   //最小温度        private double _currentT;               //当前温度        private RectangleGeometry rt;           //显示的区域        private PathGeometry maskGeometry;        public Weather_Glasses()            : this(60.0, -40.0, 0.0)        {        }        public Weather_Glasses(double maxT, double minT, double currentT)        {            InitializeComponent();            _maxT = maxT;            _minT = minT;            _currentT = currentT;            maskGeometry = new PathGeometry();            rt = new RectangleGeometry();            rt.Rect = GetRectangle();            maskGeometry = Geometry.Combine(maskGeometry, rt, GeometryCombineMode.Union, null);            mask.Clip = maskGeometry;        }        private Rect GetRectangle()        {            return new Rect(0, 300 - 89 + 30 - (_currentT + 40) / (_maxT - _minT) * (300 - 97 - 30 + 5) + 30, 100,  (_currentT + 40) / (_maxT - _minT) * (300 - 97 - 30 + 5));        }        public double CurrentT        {            get { return _currentT; }            set            {                double new_currentT = value / 100.0 * (_maxT - _minT) + _minT;                if (_currentT == new_currentT)                {                    return;                }                else if (_currentT > new_currentT)     //下降                {                    rt.Rect = GetRectangle();                    maskGeometry = Geometry.Combine(maskGeometry, rt, GeometryCombineMode.Intersect, null);                    mask.Clip = maskGeometry;                }                else if (_currentT < new_currentT)     //上升                {                    rt.Rect = GetRectangle();                    maskGeometry = Geometry.Combine(maskGeometry, rt, GeometryCombineMode.Union, null);                    mask.Clip = maskGeometry;                }                _currentT = new_currentT;            }        }        public double MinT        {            get { return _minT; }            set { _minT = value; }        }        public double MaxT        {            get { return _maxT; }            set { _maxT = value; }        }    }}


<UserControl x:Name="Glasses"             x:Class="Weather_Glasses_Control.Weather_Glasses"             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"              mc:Ignorable="d"              d:Height="300" Width="100">    <Viewbox  StretchDirection="Both">        <Canvas Width="100" Height="300">            <Ellipse Fill="Gray" Width="100" Height="100" Canvas.Left="0" Canvas.Bottom="0"/>            <Rectangle Fill="Gray" Width="40" Height="150" Canvas.Left="30" Canvas.Top="97"/>            <Border Background="Gray" CornerRadius="200,200,0,0" Width="40" Height="20" Canvas.Left="30" Canvas.Top="78"/>            <Ellipse Fill="White" Width="70" Height="70" Canvas.Left="15" Canvas.Bottom="15"/>            <Rectangle Fill="White" Width="20" Height="150" Canvas.Left="40" Canvas.Top="97"/>            <Border Background="White" CornerRadius="200,200,0,0" Width="20" Height="10" Canvas.Left="40" Canvas.Top="88"/>            <Canvas Name="mask" Width="100" Height="300">                <Ellipse Fill="Green" Width="40" Height="40" Canvas.Left="30" Canvas.Bottom="30"/>                <Rectangle Fill="Green" Width="10" Height="150" Canvas.Left="45" Canvas.Top="97"/>                <Border Background="Green" CornerRadius="200,200,0,0" Width="10" Height="5" Canvas.Left="45" Canvas.Top="93"/>            </Canvas>                          </Canvas>    </Viewbox></UserControl>