//xaml
<Window x:Class="WpfApp184.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp184"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Ellipse
x:Name="elp"
Width="{Binding ElpWidth,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
Height="{Binding ElpWidth,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
VerticalAlignment="Center"
HorizontalAlignment="Center"
MouseDown="elp_MouseDown"
MouseMove="elp_MouseMove"
MouseUp="elp_MouseUp"
RenderTransformOrigin="0.5,0.5">
<Ellipse.Fill>
<!--<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="Orange" Offset="0"/>
<GradientStop Color="Orange" Offset="0.2"/>
<GradientStop Color="Yellow" Offset="0.2"/>
<GradientStop Color="Yellow" Offset="0.4"/>
<GradientStop Color="Green" Offset="0.4"/>
<GradientStop Color="Green" Offset="0.498"/>
<GradientStop Color="Red" Offset="0.498"/>
<GradientStop Color="Red" Offset="0.502"/>
<GradientStop Color="Green" Offset="0.502"/>
<GradientStop Color="Green" Offset="0.6"/>
<GradientStop Color="Blue" Offset="0.6"/>
<GradientStop Color="Blue" Offset="0.8"/>
<GradientStop Color="Cyan" Offset="0.8"/>
<GradientStop Color="Cyan" Offset="1.0"/>
</LinearGradientBrush>-->
<ImageBrush ImageSource="{Binding BgImgSource,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
Stretch="Fill"/>
</Ellipse.Fill>
<Ellipse.RenderTransform>
<RotateTransform x:Name="elpRotater"/>
</Ellipse.RenderTransform>
</Ellipse>
</Grid>
</Window>
//xaml.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
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.IO;
using static System.Net.Mime.MediaTypeNames;
namespace WpfApp184
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
this.Loaded += MainWindow_Loaded;
this.SizeChanged += MainWindow_SizeChanged;
}
private void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
{
InitVariables();
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
InitVariables();
}
private void InitVariables()
{
var fe = this.Content as FrameworkElement;
if (fe != null)
{
ElpWidth = Math.Min(fe.ActualWidth, fe.ActualHeight) - 100;
originPt = new Point(fe.ActualWidth / 2, fe.ActualHeight / 2);
string bgImgUrl = @"../../Images/2.jpg";
BgImgSource=GetImgSourceViaUrl(bgImgUrl);
}
}
private ImageSource GetImgSourceViaUrl(string imgUrl)
{
if(!File.Exists(imgUrl))
{
return null;
}
BitmapImage bmi = new BitmapImage();
bmi.BeginInit();
bmi.UriSource=new Uri(imgUrl,UriKind.RelativeOrAbsolute);
bmi.EndInit();
if(bmi.CanFreeze)
{
bmi.Freeze();
}
return bmi;
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propName = "")
{
var handler = PropertyChanged;
if (handler != null)
{
handler?.Invoke(this, new PropertyChangedEventArgs(propName));
}
}
private double elpWidth;
public double ElpWidth
{
get
{
return elpWidth;
}
set
{
if (value != elpWidth)
{
elpWidth = value;
OnPropertyChanged(nameof(ElpWidth));
}
}
}
private ImageSource bgImgSource;
public ImageSource BgImgSource
{
get
{
return bgImgSource;
}
set
{
if (value != bgImgSource)
{
bgImgSource = value;
OnPropertyChanged(nameof(BgImgSource));
}
}
}
private Point prevPt { get; set; }
private Point originPt { get; set; }
private void elp_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
prevPt = e.GetPosition(this);
elp.CaptureMouse();
}
}
private void elp_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
Vector prevVec = Point.Subtract(prevPt, originPt);
Point newPt = e.GetPosition(this);
Vector newVec = Point.Subtract(newPt, originPt);
double rotatedAngle = Vector.AngleBetween(prevVec, newVec);
elpRotater.Angle += rotatedAngle;
this.Title = elpRotater.Angle.ToString();
prevPt = newPt;
}
}
private void elp_MouseUp(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Released)
{
elp.ReleaseMouseCapture();
}
}
}
}
![]()
![]()
![]()
![]()