WPF Draw Heart 2.0 edition of Happy New Year

Source: Internet
Author: User
Tags sin

2017 New Year's day has arrived, want to do a blessing language window, put on a painting heart program changed, into the following interface.

The next place to change, the first window has no title bar.

MainWindow.xaml

allowstransparency="True"MouseDoubleClick="Window_mousedoubleclick"WindowStyle="None"Background="#00FFFFFF"Loaded="window_loaded"Mousemove="Window_mousemove"Title="MainWindow"height=" the"Width=" the"

The style of the middle circle button

<style targettype="Button"x:key="ButtonStyle"> <setter property="Template"> <Setter.Value> <controltemplate targettype="Button"> <Grid> <ellipse width=" $"height=" $"> <Ellipse.Fill> <lineargradientbrus H startpoint="0,0"Endpoint="0,1"> <gradientstop offset="0"Color="Blue"/> <gradientstop offset="1"Color="LightBlue"/> </LinearGradientBrush> </ellipse. fill> </Ellipse> <ellipse width=" the"height=" the"> <Ellipse.Fill> <lineargradientbrus H startpoint="0,0"Endpoint="0,1"> <gradientstop offset="0"Color=" White"/> <gradientstop offset="1"Color="Transparent"/> </LinearGradientBrush> </ellipse. fill> </Ellipse> <contentcontrol Verticalalignme Nt="Center"Horizontalalignment="Center"Content="{TemplateBinding Content}"FontSize=" -"fontweight="Bold"Foreground="Red"/> </Grid> </ControlTemplate> </set ter. Value> </Setter> </Style>

Although there is no title bar, in order to allow the window to move freely

      Private void Window_mousemove (object  sender, MouseEventArgs e)        {            if (E.leftbutton = =  mousebuttonstate.pressed)            {                this. DragMove ();            }        }

Double-click the mouse to close the window

        privatevoid Window_mousedoubleclick (object  sender, MouseButtonEventArgs e)        {           this. Close ();        }

No longer use thread sleep to animate the creation of the heart, this time using a timer.

      Private DispatcherTimer timer = new DispatcherTimer ();//Create Timer Object public          MainWindow ()        {            InitializeComponent ();            Windowstartuplocation = Windowstartuplocation.centerscreen;            this.bendediction.Content = "2017 \ r \ n Happy New Year";            Timer. Tick + = new EventHandler (Timer_tick); Adds an event, timed to the event              timer. Interval = timespan.frommilliseconds (100);//Set Timer long              timer. Start ();        }        void Timer_tick (object sender, EventArgs e)        {            if (Itick < Maxstep)            {                createheartline (itick);            }            Else            {                timer. Stop ();            }            itick++;        }

The generation algorithm of the heart, basically unchanged, just the radius and position of the heart to adjust the next.

Private voidCreateheartline (intItick) {Centerpt= canvas_shape.width/4; Radius= Canvas_shape.width/6;  for(inti =0; i < Itick; i++) {System.Windows.Controls.Image img=NewSystem.Windows.Controls.Image (); Img. Source=NewBitmapImage (NewUri (@"image/"+2+". PNG", urikind.relative));//give the photo pathImg. Height = $; Img. Width= $; Img. Stretch=Stretch.fill; DoubleAngle =2* Math.pi/maxstep *i; //Peach-shaped Heart                Doublex = centerpt- -* (Math.sin (angle) * Math.sin (angle) * Math.sin (angle)) * -;//                Doubley = centerpt-( -* Math.Cos (angle)-5* Math.Cos (2* Angle)-2* Math.Cos (3* Angle)-Math.Cos (4* angle)) * -;//canvas.setleft (IMG, x);                Canvas.settop (img, y);            Canvas_Shape.Children.Add (IMG); }        }     

In order to add background music, MediaElement was used.

       private void Window_Loaded (object sender, RoutedEventArgs e)        {            me. Loadedbehavior = mediastate.manual;  Set to manually control              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 ();        }

To be able to loop playback

      Private void me_mediaended (object  sender, RoutedEventArgs e)        {            me. Stop ();            Me. Play ();        }

WPF Draw Heart 2.0 edition of Happy New Year

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.