椭圆收缩仪;在C#WPF中淡入而不超过画布边界
当用户点击画布边缘时,如何使动画以更大的尺寸停留在画布中?当前,如果大小过大,并且用户在画布边缘附近单击,则椭圆将在画布外部增长以覆盖按钮。我需要动画留在画布内,使其看起来像一块比萨饼 应该是这样的: 目前看起来是这样的: Xaml:椭圆收缩仪;在C#WPF中淡入而不超过画布边界,c#,wpf,animation,fade,shrink,C#,Wpf,Animation,Fade,Shrink,当用户点击画布边缘时,如何使动画以更大的尺寸停留在画布中?当前,如果大小过大,并且用户在画布边缘附近单击,则椭圆将在画布外部增长以覆盖按钮。我需要动画留在画布内,使其看起来像一块比萨饼 应该是这样的: 目前看起来是这样的: Xaml: C#: 公共部分类主窗口:窗口 { 私有整数大小; 私人SolidColorBrush fillColor; 私人SolidColorBrush strokeColor; 私有列表颜色; 私有int-fillIndex; 斯特罗肯德克斯私人酒店; 私有in
C#:
公共部分类主窗口:窗口
{
私有整数大小;
私人SolidColorBrush fillColor;
私人SolidColorBrush strokeColor;
私有列表颜色;
私有int-fillIndex;
斯特罗肯德克斯私人酒店;
私有int strokeThickness=1;
私有int-fillColorDefault;
私有int strokeColorDefault;
专用点?_start=null;
公共主窗口()
{
初始化组件();
添加颜色();
textBox.Text=strokehickness.ToString();
parse();
}
private void MenuItem_Click_退出(对象发送方,RoutedEventTargets e){Environment.Exit(1);}
私有无效窗口\u KeyUp\u ESC(对象发送方,KeyEventArgs e)
{
if(Key.Escape==e.Key)
菜单项单击退出(发件人,e);
}
私有void addColors()
{
colors=typeof(笔刷).GetProperties()。选择(p=>p.GetValue(null,null)作为SolidColorBrush.ToList();
整数计数=0;
foreach(颜色中的SolidColorBrush颜色)
{
组合框_FillColor.Items.Add(新矩形(){Height=12,Width=17.5,Fill=color});
ComboBox_StrokeColor.Items.Add(新矩形(){Height=12,Width=17.5,Fill=color});
if(color.color==Colors.Red)
{
fillIndex=计数;
fillColor=颜色[fillIndex];
组合框_FillColor.SelectedIndex=计数;
fillColorDefault=计数;
}
if(color.color==Colors.Black)
{
strokeIndex=计数;
strokeColor=颜色[strokeIndex];
ComboBox_StrokeColor.SelectedIndex=计数;
strokeColorDefault=计数;
}
计数++;
}
}
私有无效组合框_FillColor_Selected(对象发送方,routedEventTargets e){fillIndex=ComboBox _FillColor.SelectedIndex;FillColor=colors[fillIndex];}
private void ComboBox_StrokeColor_Selected(对象发送方,RoutedEventTargets e){strokeIndex=ComboBox_StrokeColor.SelectedIndex;StrokeColor=colors[strokeIndex];}
私有void Canvas_MouseDown(对象发送器,MouseButtonEventArgs e)
{
笔划=笔划颜色;
path.StrokeThickness=StrokeThickness;
path.Fill=fillColor;
path.HorizontalAlignment=HorizontalAlignment.Stretch;
path.VerticalAlignment=VerticalAlignment.Stretch;
path.Stretch=Stretch.None;
path.SetValue(Grid.ColumnProperty,1);
_start=Mouse.GetPosition((UIElement)sender);
myCircle.Center=(点)\u开始;
var sb=FindResource(“动画”)作为故事板;
var x=sb.Children.First()作为双动画;
x、 To=2*尺寸;
x、 持续时间=新的持续时间(时间跨度从秒(0.5));
变量y=sb.Children.ElementAt(1)作为双动画;
y、 To=2*尺寸;
y、 持续时间=新的持续时间(时间跨度从秒(0.5));
var z=sb.Children.Last()作为双动画;
z、 From=0.0;
z、 To=1.0;
z、 持续时间=新的持续时间(时间跨度从秒(0.5));
开始(道路);
}
私有void textBox\u TextChanged(对象发送者,textchangedventargs e)
{
//正则表达式,其中除数字外的任何字符字符串
正则表达式模式=新正则表达式(@“^([^0-9]*)$”,RegexOptions.Compiled);
匹配结果=pattern.Match(textBox.Text);
if(textBox.Text.ToString()==string.Empty)
返回;
else if(result.Success)
{
MessageBox.Show(“输入的字符无效。仅限整数。笔划厚度将重置为默认值1”);
冲程厚度=1;
textBox.Text=strokehickness.ToString();
textBox.SelectAll();
}
其他的
{
int x;
if(int.TryParse(textBox.Text,out x))
strokeThickness=int.Parse(textBox.Text);
}
}
专用空白滑块\u大小\u预览鼠标向上(对象发送器,鼠标按钮Ventargs e)
{
parse();
}
私有void parse()
{
int x;
if(int.TryParse(tempSize.Content.ToString(),out x))
尺寸=x;
}
}
}所以你不需要椭圆留在
画布中,但是你想把剩下的部分剪掉,对吗?只需将cliptobunds
(画布的)设置为true
(可以在Xaml中完成)。这样您就不需要椭圆停留在画布中,但您希望剪掉离开它的部分,对吗?只需将cliptobunds
(画布)设置为true
(可以在Xaml中完成)
<Window.Resources>
<Storyboard x:Key="anim">
<DoubleAnimation
Storyboard.TargetName="myCircle"
Storyboard.TargetProperty="RadiusX"
AutoReverse="True"/>
<DoubleAnimation
Storyboard.TargetName="myCircle"
Storyboard.TargetProperty="RadiusY"
AutoReverse="True"/>
<DoubleAnimation
Storyboard.TargetName="path"
Storyboard.TargetProperty="Opacity"
AutoReverse="True"/>
</Storyboard>
</Window.Resources>
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="23"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<DockPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="0" Margin="0,0,0,1">
<Menu DockPanel.Dock="Top" Height="23">
<MenuItem Header="Main" RenderTransformOrigin="-1.896,0.643" HorizontalAlignment="Left" Width="39" Height="23">
<MenuItem Header="Exit, Esc" Click="MenuItem_Click_Exit"/>
</MenuItem>
</Menu>
</DockPanel>
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="1" Name="pane">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="0" Name="pane2">
<Grid.RowDefinitions>
<RowDefinition Height="35"/>
<RowDefinition Height="35"/>
<RowDefinition Height="35"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="50"/>
</Grid.ColumnDefinitions>
<Label Content="Size" Grid.Row="0" Grid.Column="0" Height="25" VerticalAlignment="Stretch"/>
<Label Content="Fill Color" Grid.Row="1" Grid.Column="0" Height="25" VerticalAlignment="Stretch"/>
<Label Content="Stroke Thickness" Grid.Row="2" Grid.Column="0" Height="25" VerticalAlignment="Stretch"/>
<Label Content="Stroke Color" Grid.Row="3" Grid.Column="0" VerticalAlignment="Top" Height="25"/>
<Slider x:Name="Slider_Size" Grid.Row="0" Grid.Column="1" Height="20" Width="45"
Minimum="5" Maximum="50"
AutoToolTipPlacement="BottomRight"
TickFrequency="1"
IsSnapToTickEnabled="True"
PreviewMouseUp="Slider_Size_PreviewMouseUp"/>
<Label Name="tempSize" Content="{Binding Path=Value, ElementName=Slider_Size}" Margin="0,25,0,131" Grid.Row="3" Visibility="Hidden"/>
<ComboBox Name="ComboBox_FillColor" Grid.Row="1" Grid.Column="1" Height="20" Width="45" SelectionChanged="ComboBox_FillColor_Selected"/>
<TextBox Name="textBox" Grid.Row="2" Grid.Column="1" Height="20" Width="45" TextChanged="textBox_TextChanged"/>
<ComboBox Name="ComboBox_StrokeColor" Grid.Row="3" Grid.Column="1" VerticalAlignment="Top" Height="20" Width="45" SelectionChanged="ComboBox_StrokeColor_Selected"/>
</Grid>
<Border Name ="border" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderBrush="Black" Grid.Column="1" BorderThickness="2">
<Canvas Name="canvas" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" MouseDown="Canvas_MouseDown">
<Path x:Name="path">
<Path.Data>
<EllipseGeometry x:Name="myCircle"/>
</Path.Data>
</Path>
<Canvas.Background>
<SolidColorBrush Color="White" Opacity="0"/>
</Canvas.Background>
</Canvas>
</Border>
</Grid>
</Grid>
public partial class MainWindow : Window
{
private int size;
private SolidColorBrush fillColor;
private SolidColorBrush strokeColor;
private List<SolidColorBrush> colors;
private int fillIndex;
private int strokeIndex;
private int strokeThickness = 1;
private int fillColorDefault;
private int strokeColorDefault;
private Point? _start = null;
public MainWindow()
{
InitializeComponent();
addColors();
textBox.Text = strokeThickness.ToString();
parse();
}
private void MenuItem_Click_Exit(object sender, RoutedEventArgs e) { Environment.Exit(1); }
private void Window_KeyUp_ESC(object sender, KeyEventArgs e)
{
if (Key.Escape == e.Key)
MenuItem_Click_Exit(sender, e);
}
private void addColors()
{
colors = typeof(Brushes).GetProperties().Select(p => p.GetValue(null, null) as SolidColorBrush).ToList();
int count = 0;
foreach (SolidColorBrush color in colors)
{
ComboBox_FillColor.Items.Add(new Rectangle() { Height = 12, Width = 17.5, Fill = color });
ComboBox_StrokeColor.Items.Add(new Rectangle() { Height = 12, Width = 17.5, Fill = color });
if (color.Color == Colors.Red)
{
fillIndex = count;
fillColor = colors[fillIndex];
ComboBox_FillColor.SelectedIndex = count;
fillColorDefault = count;
}
if (color.Color == Colors.Black)
{
strokeIndex = count;
strokeColor = colors[strokeIndex];
ComboBox_StrokeColor.SelectedIndex = count;
strokeColorDefault = count;
}
count++;
}
}
private void ComboBox_FillColor_Selected(object sender, RoutedEventArgs e) { fillIndex = ComboBox_FillColor.SelectedIndex; fillColor = colors[fillIndex]; }
private void ComboBox_StrokeColor_Selected(object sender, RoutedEventArgs e) { strokeIndex = ComboBox_StrokeColor.SelectedIndex; strokeColor = colors[strokeIndex]; }
private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
{
path.Stroke = strokeColor;
path.StrokeThickness = strokeThickness;
path.Fill = fillColor;
path.HorizontalAlignment = HorizontalAlignment.Stretch;
path.VerticalAlignment = VerticalAlignment.Stretch;
path.Stretch = Stretch.None;
path.SetValue(Grid.ColumnProperty, 1);
_start = Mouse.GetPosition((UIElement)sender);
myCircle.Center = (Point)_start;
var sb = FindResource("anim") as Storyboard;
var x = sb.Children.First() as DoubleAnimation;
x.To = 2 * size;
x.Duration = new Duration(TimeSpan.FromSeconds(0.5));
var y = sb.Children.ElementAt(1) as DoubleAnimation;
y.To = 2 * size;
y.Duration = new Duration(TimeSpan.FromSeconds(0.5));
var z = sb.Children.Last() as DoubleAnimation;
z.From = 0.0;
z.To = 1.0;
z.Duration = new Duration(TimeSpan.FromSeconds(0.5));
sb.Begin(path);
}
private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
//regex where any string of chars besides numbers
Regex pattern = new Regex(@"^([^0-9]*)$", RegexOptions.Compiled);
Match result = pattern.Match(textBox.Text);
if (textBox.Text.ToString() == string.Empty)
return;
else if (result.Success)
{
MessageBox.Show("Invalid character entered. Integer numbers only. Stroke Thickness will be reseted to a default of 1.");
strokeThickness = 1;
textBox.Text = strokeThickness.ToString();
textBox.SelectAll();
}
else
{
int x;
if (int.TryParse(textBox.Text, out x))
strokeThickness = int.Parse(textBox.Text);
}
}
private void Slider_Size_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
parse();
}
private void parse()
{
int x;
if (int.TryParse(tempSize.Content.ToString(), out x))
size = x;
}
}