Click here to Skip to main content
Licence CPOL
First Posted 8 Sep 2008
Views 115,430
Downloads 5,590
Bookmarked 298 times

A lovely goldfish desktop pet (using alpha-PNG and GDI+)

By | 8 Sep 2008 | Article
Using alpha-PNG and GDI+ technology to implement a desktop pet fish!

GoldFish

Introduction

This article demonstrates a very lovely goldfish desktop pet, using alpha-PNG and GD+. This cute goldfish can swim from one side to another of your desktop, and tries to escape when you try to catch it using your mouse.

Background

After I posted these articles: A Cool Vista Sidebar Gadget Style CPUInfo Animate Control! (Fixed)A cool Vista-Sidebar-style clock control (4 style)(fixed)! many people asked me how to make a real alpha-transparent form using these controls. Now, this has become a reality! This is a real alpha-transparent form with animation, even in some none-Vista OSs such as XP!

Have fun! And, do not forget to vote! :)

Using the code

To make a form transparent, use this code to set an alpha-transparent PNG picture as its background:

public void SetBits(Bitmap bitmap)
{
    if (!haveHandle) return;
    if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || 
        !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat))
        throw new ApplicationException("The picture must be " + 
                  "32bit picture with alpha channel");
    IntPtr oldBits = IntPtr.Zero;
    IntPtr screenDC = Win32.GetDC(IntPtr.Zero);
    IntPtr hBitmap = IntPtr.Zero;
    IntPtr memDc = Win32.CreateCompatibleDC(screenDC);
    try
    {
        Win32.Point topLoc = new Win32.Point(Left, Top);
        Win32.Size bitMapSize = new Win32.Size(bitmap.Width, bitmap.Height);
        Win32.BLENDFUNCTION blendFunc = new Win32.BLENDFUNCTION();
        Win32.Point srcLoc = new Win32.Point(0, 0);
        hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
        oldBits = Win32.SelectObject(memDc, hBitmap);
        blendFunc.BlendOp = Win32.AC_SRC_OVER;
        blendFunc.SourceConstantAlpha = 255;
        blendFunc.AlphaFormat = Win32.AC_SRC_ALPHA;
        blendFunc.BlendFlags = 0;
        Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, 
                         memDc, ref srcLoc, 0, ref blendFunc, Win32.ULW_ALPHA);
    }
    finally
    {
        if (hBitmap != IntPtr.Zero)
        {
            Win32.SelectObject(memDc, oldBits);
            Win32.DeleteObject(hBitmap);
        }
        Win32.ReleaseDC(IntPtr.Zero, screenDC);
        Win32.DeleteDC(memDc);
    }
}

OK, so simple! Now, you can use a timer control to implement the animation function.

Sorry for my poor English! Please see the source code for more details.

Points of Interest

  • Developing with GDI+ and C# is a very interesting thing!
  • PNG format is very good for drawing alpha pics!
  • For more code samples, please visit my personal web site.

History

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Davidwu



China China

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalnice one Pinmembergeorge pradeep23:17 11 Mar '12  
QuestionC++ version Pinmembermrx_jims0:40 6 Mar '12  
GeneralMy vote of 5 Pinmembermanoj kumar choubey23:58 16 Feb '12  
GeneralMy vote of 5 PinmemberHarith Randika17:14 2 Feb '12  
GeneralMy vote of 5 PinmemberTushar_Patil0:09 28 Dec '11  
QuestionMy Vote of 4 PinmemberClark Kent1237:54 30 Nov '11  
GeneralMy vote of 5 PinmemberMMChandrasekar15:56 12 Nov '11  
GeneralMy vote of 5 Pingroupred_code200819:59 5 Sep '11  
GeneralMy vote of 5 Pinmemberjerviz_7617:05 1 Feb '11  
Questionwitch part makes the fish stay on desktop? please tell me.thank you very much. [modified] Pinmemberluangeng17:10 21 Sep '10  
GeneralC++ version to David Wu contribution. [modified] Pinmemberhirohata3:22 13 Jul '10  
GeneralReally cute PinmemberBigdeak4:55 10 Jun '10  
GeneralVery beautiful Pinmemberkuyexing22:12 3 Jun '10  
GeneralGood job!,but it is CPU-bounded application. Pinmembernblijun16:22 3 Apr '10  
GeneralMy vote of 5 PinmemberSmithers-Jones0:10 8 Feb '10  
JokeRe: My vote of 5 PinmemberSmithers-Jones2:07 10 Feb '10  
GeneralTV Logo Pinmemberdivyesh143219:45 4 Feb '10  
Generalc++ code for the same PinmemberMember 356448320:14 9 Jan '10  
Generalgood job Pinmemberfei_cong18:43 9 Dec '09  
GeneralWOW. Pinmemberfrancis320:40 21 Sep '09  
GeneralLovely Pinmemberequipmentloannswguru0:08 5 Sep '09  
GeneralAmazing!!!! Pinmemberoe30e30e303e3:51 12 Aug '09  
QuestionWhere does the goldfish image come from? Pinmembergapman19:26 22 Jul '09  
Generalnice job Pinmemberpkfoo2:29 11 May '09  
General[Message Removed] PinmemberKatekortez9:08 25 Oct '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 9 Sep 2008
Article Copyright 2008 by Davidwu
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid