WPF 画心2.0版之元旦快乐
Posted lonelyxmas
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF 画心2.0版之元旦快乐相关的知识,希望对你有一定的参考价值。
原文:WPF 画心2.0版之元旦快乐2017年元旦已经到了,想做一个祝福语的窗口,就把上一篇画心的程序改了改,变成了如下界面。
说下改动的地方,首先窗口没有标题栏了。
MainWindow.xaml
AllowsTransparency="True" MouseDoubleClick="Window_MouseDoubleClick" WindowStyle="None" Background="#00FFFFFF" Loaded="Window_Loaded" MouseMove="Window_MouseMove" Title="MainWindow" Height="700" Width="900"
中间圆形按钮的样式
<Style TargetType="Button" x:Key="ButtonStyle"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Grid> <Ellipse Width="200" Height="200"> <Ellipse.Fill> <LinearGradientBrush StartPoint="0,0" EndPoint="0,1"> <GradientStop Offset="0" Color="blue"/> <GradientStop Offset="1" Color="LightBlue"/> </LinearGradientBrush> </Ellipse.Fill> </Ellipse> <Ellipse Width="180" Height="180"> <Ellipse.Fill> <LinearGradientBrush StartPoint="0,0" EndPoint="0,1"> <GradientStop Offset="0" Color="White"/> <GradientStop Offset="1" Color="Transparent"/> </LinearGradientBrush> </Ellipse.Fill> </Ellipse> <ContentControl VerticalAlignment="Center" HorizontalAlignment="Center" Content="{TemplateBinding Content}" FontSize="20" FontWeight="Bold" Foreground="Red"/> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>
虽然没有了标题栏,为了能够让窗口自由移动
private void Window_MouseMove(object sender, MouseEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { this.DragMove(); } }
双击鼠标,关闭窗口
private void Window_MouseDoubleClick(object sender, MouseButtonEventArgs e) { this.Close(); }
不再使用线程睡眠的方式来动态显示心的生成,这次采用计时器。
private DispatcherTimer timer = new DispatcherTimer();//创建定时器对象 public MainWindow() { InitializeComponent(); WindowStartupLocation = WindowStartupLocation.CenterScreen; this.bendediction.Content = "2017年 元旦快乐"; timer.Tick += new EventHandler(timer_Tick); //添加事件,定时到事件 timer.Interval = TimeSpan.FromMilliseconds(100);//设置定时长 timer.Start(); } void timer_Tick(object sender, EventArgs e) { if (iTick < maxStep) { CreateHeartLine(iTick); } else { timer.Stop(); } iTick++; }
本次心的生成算法,基本不变,只是把心的半径和位置调整了下。
private void CreateHeartLine(int iTick) { centerPt = canvas_Shape.Width/ 4; radius = canvas_Shape.Width / 6; for (int i = 0; i < iTick; i++) { System.Windows.Controls.Image img = new System.Windows.Controls.Image(); img.Source = new BitmapImage(new Uri(@"image/" + 2 + ".png", UriKind.Relative));//给出照片路径 img.Height = 200; img.Width = 200; img.Stretch = Stretch.Fill; double angle = 2 * Math.PI / maxStep * i; //桃形心 double x = centerPt - 16 * (Math.Sin(angle) * Math.Sin(angle) * Math.Sin(angle)) * 20;// double y = centerPt - (13 * Math.Cos(angle) - 5 * Math.Cos(2 * angle) - 2 * Math.Cos(3 * angle) - Math.Cos(4 * angle)) * 20;// Canvas.SetLeft(img, x); Canvas.SetTop(img, y); canvas_Shape.Children.Add(img); } }
为了添加背景音乐,使用了MediaElement。
private void Window_Loaded(object sender, RoutedEventArgs e) { me.LoadedBehavior = MediaState.Manual; //设置为手动控制 me.UnloadedBehavior = MediaState.Manual; string strPath = string.Format(@"{0}datamusick.mp3", System.Windows.Forms.Application.StartupPath); Uri uri = new Uri(strPath, UriKind.Relative); me.Source = uri; me.IsHitTestVisible = true; me.MediaEnded += new RoutedEventHandler(me_MediaEnded); gd.Children.Add(me); me.Play(); }
为了能够循环播放
private void me_MediaEnded(object sender, RoutedEventArgs e) { me.Stop(); me.Play(); }
以上是关于WPF 画心2.0版之元旦快乐的主要内容,如果未能解决你的问题,请参考以下文章