Fishy Fishy Fish






4.90/5 (40 votes)
Just for fun; a bunch of fish swimming around the screen
- HockeyApp AppX page (If you want to side load as a UWP app)
- Download fishtank.zip - 2.8 MB
- Download source - 2.5 MB
Introduction
This article is a bit of a homage to Davidwu's excellent article, A lovely goldfish desktop pet. In that article, David demonstrates using alpha-blending and GDI+ to make a little fish swim across the screen.
I've taken that code and extended it to support multiple fish along with a SysTray icon to control the number of fish in your tank.
I also added some different colored fish images for a little variety.
Using the Code
The code is largely the same as the original, though I've moved things around a bit in order to support multiple fish. Unlike the original code, frames are extracted from the source PNG at startup rather than at each timer tick.
class Frameset : List<Bitmap>, IDisposable
{
public Frameset(Bitmap b, int framecount)
{
if (!Bitmap.IsCanonicalPixelFormat(b.PixelFormat) ||
!Bitmap.IsAlphaPixelFormat(b.PixelFormat))
throw new ApplicationException("The picture must be 32bit
picture with alpha channel.");
FrameWidth = b.Width / framecount;
FrameHeight = b.Height;
for (int i = 0; i < framecount; i++)
{
Bitmap bitmap = new Bitmap(FrameWidth, FrameHeight);
using (Graphics g = Graphics.FromImage(bitmap))
g.DrawImage(b, new Rectangle(0, 0, FrameWidth, FrameHeight),
new Rectangle(FrameWidth * i, 0, FrameWidth, FrameHeight),
GraphicsUnit.Pixel);
Add(bitmap);
}
}
public int FrameWidth { get; private set; }
public int FrameHeight { get; private set; }
public void Dispose()
{
foreach (Bitmap f in this)
f.Dispose();
Clear();
}
}
This greatly reduces the CPU usage for the animation which is important with a whole tankful swimming around. There is also a Form derived from the main FishForm
to host the NotifyIcon
in the system tray. An instance of this form will always be the first one created. The NotifyIcon
context menu allows the user to add and remove fish, show and hide all the fish and of course exit the application.
.NET 4
The project files are all in VS.NET 2010 format, but the only .NET 4 type used is the Tuple
. Sacha posted a version of a Tuple
in the comments below. So if you want to play with this in 2008 and .NET 3.5, you'll need to incorporate that snippet or otherwise replace the Tuple
usage, which shouldn't be too hard.
Conversion to UWP
With the Windows 10 Anniversary release and the introduciton of the Desktop App Converter, I decided to see if this would run as a UWP app. The whole conversion process was remarkably simple.
1) The first step was to update the project from vs.net 2010 to 2015 (specifically the 15 Release 3 Preview - more on that later) and then target .NET 4.6.1. No issues there.
2) Next a UWP AppXManifext layout file needed to be created. I decided to go the manual route since this thing is xcopy deployable. The manifest is about as simple as can be. Really the hardest part was refreshing myself on app signing. In the end I added a couple build event scripts that rebuild the appx package with each build. Drop that and some logo png's into the solution folder.
AppXManifest.xml
<?xml version="1.0" encoding="utf-8"?> <Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"> <Identity Name="[this is the guid of your package]" ProcessorArchitecture="x86" Publisher="CN=kackman.net" Version="1.1.11.0" /> <Properties> <DisplayName>Fishy Fishy Fish</DisplayName> <PublisherDisplayName>Reserved</PublisherDisplayName> <Description>Some fish. Swimming around on your screen.</Description> <Logo>StoreLogo.png</Logo> </Properties> <Resources> <Resource Language="en-us" /> </Resources> <Dependencies> <TargetDeviceFamily Name="Windows.Desktop" MinVersion="10.0.14316.0" MaxVersionTested="10.0.14393.0" /> </Dependencies> <Capabilities> <rescap:Capability Name="runFullTrust"/> </Capabilities> <Applications> <Application Id="FishyFishyFish" Executable="FishyFish.exe" EntryPoint="Windows.FullTrustApplication"> <uap:VisualElements BackgroundColor="transparent" DisplayName="Fishy Fishy Fish" Square150x150Logo="Square150x150Logo.png" Square44x44Logo="Square44x44Logo.png" Description="Some fish. Swimming around on your screen." /> </Application> </Applications> </Package>
Prebuild
:: clean any previous AppX outputs rmdir AppX /s /q del $(TargetName).appx /q /f
Postbuild
:: copy all of the files that go into the AppX into a working folder mkdir AppX xcopy "$(TargetPath)" AppX\ /R /Y xcopy "$(TargetPath).config" AppX\ /R /Y xcopy "$(SolutionDir)appxmanifest.xml" AppX\ /R /Y xcopy "$(SolutionDir)StoreLogo.png" AppX\ /R /Y xcopy "$(SolutionDir)Square44x44Logo.png" AppX\ /R /Y xcopy "$(SolutionDir)Square150x150Logo.png" AppX\ /R /Y :: build a new AppX package "$(Win10SDKDir)MakeAppX.exe" pack /d AppX /p $(TargetName).appx "$(Win10SDKDir)SignTool.exe" sign -f d:\temp\tempca.pfx -fd SHA256 -v .\$(TargetName).appx
$(Win10SDKDir)
is a variable added to the csproj
file that points to the windows 10 sdk folder.)<PropertyGroup> <Win10SDKDir Condition=" '$(Win10SDKDir)' == '' ">C:\Program Files (x86)\Windows Kits\10\bin\x64\</Win10SDKDir> </PropertyGroup>
Conclusion
Oh, and I kind of cheated on the fish colorizing. I color shifted the source PNG in Paint.Net and saved each one as a new set of images. That's why the project size is large. Perhaps someday I'll correct that short cut but for now my apologies for the extra large download. :)
That's about all there is to it. It's been a fun little project to play around with (and the family and friends I've given it to have enjoyed it as well). This will probably be my last WinForms article. I've caught the WPF bug and am finally cresting the learning curve.
History
- 3/31/2010 - Initial post
- 8/3/2016 - UWP section