2017年元旦已经到了,想做一个祝福语的窗口,就把上一篇画心的程序改了改,变成了如下界面。

WPF 画心2.0版之元旦快乐-LMLPHP

说下改动的地方,首先窗口没有标题栏了。

MainWindow.xaml

  AllowsTransparency="True"
MouseDoubleClick="Window_MouseDoubleClick"
WindowStyle="None" Background="#00FFFFFF" Loaded="Window_Loaded" MouseMove="Window_MouseMove"
Title="MainWindow" Height="" Width=""

中间圆形按钮的样式

<Style TargetType="Button" x:Key="ButtonStyle">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<Ellipse Width="" Height="">
<Ellipse.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Offset="" Color="blue"/>
<GradientStop Offset="" Color="LightBlue"/>
</LinearGradientBrush>
</Ellipse.Fill>
</Ellipse>
<Ellipse Width="" Height="">
<Ellipse.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Offset="" Color="White"/>
<GradientStop Offset="" Color="Transparent"/>
</LinearGradientBrush>
</Ellipse.Fill>
</Ellipse>
<ContentControl VerticalAlignment="Center" HorizontalAlignment="Center" Content="{TemplateBinding Content}"
FontSize="" 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年\r\n元旦快乐";
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/ ;
radius = canvas_Shape.Width / ;
for (int i = ; i < iTick; i++)
{
System.Windows.Controls.Image img = new System.Windows.Controls.Image(); img.Source = new BitmapImage(new Uri(@"image/" + + ".png", UriKind.Relative));//给出照片路径
img.Height = ;
img.Width = ;
img.Stretch = Stretch.Fill; double angle = * Math.PI / maxStep * i; //桃形心
double x = centerPt - * (Math.Sin(angle) * Math.Sin(angle) * Math.Sin(angle)) * ;//
double y = centerPt - ( * Math.Cos(angle) - * Math.Cos( * angle) - * Math.Cos( * angle) - Math.Cos( * angle)) * ;//
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}\data\music\bk.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();
}
05-11 11:19