Desktop pet in C # WPF

Desktop pet - albeit useless, but very funny desktop decoration. This article shows, probably, the simplest implementation of it.





GIF under the cut!





A little about getting started with WPF



To create the application, we used Visual Studio 2017. So, create a project, Visual C # -> WPF Application .





, . -> , Images.





, .







, , . MainWindow.xaml. Window. :



Title="MainWindow" Height="450" Width="800">


:



WindowStyle="None" ResizeMode="NoResize"


:



Background="Transparent" AllowsTransparency="True"


, . , . , Top , Left โ€“ :



WindowStartupLocation="Manual" Top="485" Left="1000"


Window :



<Window x:Class="WpfApp1.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:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="340" Width="353.2" WindowStyle="None" Background="Transparent" AllowsTransparency="True" WindowStartupLocation="Manual" Top="485" Left="1000">


WpfAnimatedGif



, - WPF , WpfAnimatedGif. NuGetโ€ฆ





โ€œWpfAnimatedGifโ€, .





Window :



xmlns:gif="http://wpfanimatedgif.codeplex.com"


, .





Grid. Grid:



<Grid  Opacity="1">


Image, AnimatedSource WpfAnimatedGif:



gif:ImageBehavior.AnimatedSource="Images/a.gif"


, gif:ImageBehavior.RepeatBehavior="Forever", , gif:ImageBehavior.RepeatBehavior="1xโ€.



<Image Name="img" gif:ImageBehavior.AnimatedSource="Images/a.gif" gif:ImageBehavior.RepeatBehavior="1x"  Margin="-68,-63,-68,-38" RenderTransformOrigin="0.5,0.5" gif:ImageBehavior.AutoStart="True" gif:ImageBehavior.AnimationCompleted="Complete">
        </Image>


gif:ImageBehavior.AnimationCompleted="Complete" โ€“ , .



:



<Button Name="btn1" Content="Button" HorizontalAlignment="Left" Height="314" Margin="93,26,0,0" VerticalAlignment="Top" Width="163" Opacity="0" Click="Change"/>


Click=โ€Changeโ€, Change โ€“ , .



Grid :



<Grid  Opacity="1">
        <Image Name="img" gif:ImageBehavior.AnimatedSource="Images/a.gif" gif:ImageBehavior.RepeatBehavior="1x"  Margin="-68,-63,-68,-38" RenderTransformOrigin="0.5,0.5" gif:ImageBehavior.AutoStart="True" gif:ImageBehavior.AnimationCompleted="Complete">
        </Image>
        <Button Name="btn1" Content="Button" HorizontalAlignment="Left" Height="314" Margin="93,26,0,0" VerticalAlignment="Top" Width="163" Opacity="0" Click="Change"/>
    </Grid>




(MainWindow.xaml.cs). , .

MainWindow:Window :



int k = 0;// 
string num = "";//  
int n=11;//  +1(  )


: Complete Change.



private void Complete(object sender, RoutedEventArgs e)
        {
            num = "Images/" + k.ToString() + ".gif";
            BitmapImage image = new BitmapImage();
            image.BeginInit();
            image.UriSource = new Uri(num, UriKind.Relative); 
            image.EndInit();
            ImageBehavior.SetAnimatedSource(img, image); // , img โ€“ name  Image, image โ€“  
        }

private void Change(object sender, RoutedEventArgs e)
        {
            k = (k + 1) % n;
            BitmapImage image = new BitmapImage();
            image.BeginInit();
            if (k%2==0) image.UriSource = new Uri("Images/a.gif", UriKind.Relative);
            else image.UriSource = new Uri("Images/b.gif", UriKind.Relative);
            image.EndInit();
            ImageBehavior.SetAnimatedSource(img, image);
        }


object sender in this case a button. a.gif and b.gif are sneezing animations (right and left), and 0.gif - 10.gif are emotions. Each time you press k , it increases until it becomes greater than 10. If k is even , it goes one way, if it's odd, the other. After the sneeze animation ends, the Complete function is executed to change the character's emotion.





Result





Sources



WPF

WpfAnimatedGif

Sprites




All Articles