Click here to Skip to main content
15,889,992 members
Articles / Multimedia / GDI+
Article

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

Rate me:
Please Sign up or sign in to vote.
4.91/5 (234 votes)
8 Sep 2008CPOL1 min read 362.8K   14.7K   324   60
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:

C#
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)


Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralCool! Made some additions... [modified] Pin
Don Kackman21-Sep-08 10:26
Don Kackman21-Sep-08 10:26 
Generalhi Pin
srk-61921-Sep-08 5:58
srk-61921-Sep-08 5:58 
GeneralNice Article Pin
Reese17-Sep-08 6:45
Reese17-Sep-08 6:45 
GeneralNice Work! Pin
Shakeel Iqbal15-Sep-08 18:29
Shakeel Iqbal15-Sep-08 18:29 
Generalthanks ! Pin
BillWoodruff15-Sep-08 17:05
professionalBillWoodruff15-Sep-08 17:05 
GeneralWonderful Pin
wangwenyu15-Sep-08 14:52
wangwenyu15-Sep-08 14:52 
QuestionExcellent but...! Pin
VainsofJenna14-Sep-08 0:17
VainsofJenna14-Sep-08 0:17 
AnswerRe: Excellent but...! Pin
Davidwu15-Sep-08 3:49
Davidwu15-Sep-08 3:49 
This tech do not support any winform controls, but your can use two forms(one is layout transparent form and another is normal winform with no border) to achieve this goal.
GeneralRe: Excellent but...! Pin
luangeng21-Sep-10 17:13
luangeng21-Sep-10 17:13 
GeneralSize does mater ! Pin
Nicolas MEURET11-Sep-08 3:19
Nicolas MEURET11-Sep-08 3:19 
GeneralRe: Size does matter ! Pin
richardw4819-Sep-08 4:47
richardw4819-Sep-08 4:47 
QuestionWhere is the executable or demo? Pin
Ilíon9-Sep-08 5:22
Ilíon9-Sep-08 5:22 
AnswerRe: Where is the executable or demo? Pin
Davidwu9-Sep-08 5:43
Davidwu9-Sep-08 5:43 
GeneralRe: Where is the executable or demo? Pin
Ilíon9-Sep-08 6:54
Ilíon9-Sep-08 6:54 
Generalnice work! Pin
godviser9-Sep-08 3:58
godviser9-Sep-08 3:58 
NewsProblem with website referenced in code Pin
tlhIn`toq9-Sep-08 3:47
tlhIn`toq9-Sep-08 3:47 
GeneralRe: Problem with website referenced in code Pin
Davidwu9-Sep-08 5:09
Davidwu9-Sep-08 5:09 
Generalwonderful. 5 star Pin
Tim Aderson9-Sep-08 2:33
Tim Aderson9-Sep-08 2:33 
GeneralWow Pin
JobsFunny9-Sep-08 1:17
JobsFunny9-Sep-08 1:17 
GeneralVery nice job! Pin
domz20089-Sep-08 1:10
domz20089-Sep-08 1:10 
GeneralNice looking fish, but... Pin
Dave Kreskowiak9-Sep-08 0:20
mveDave Kreskowiak9-Sep-08 0:20 
GeneralRe: Nice looking fish, but... Pin
JobsFunny9-Sep-08 1:31
JobsFunny9-Sep-08 1:31 
GeneralRe: Nice looking fish, but... Pin
Zagnar9-Sep-08 3:26
Zagnar9-Sep-08 3:26 
GeneralRe: Nice looking fish, but... Pin
Schmoo2k9-Sep-08 3:40
Schmoo2k9-Sep-08 3:40 
GeneralRe: Nice looking fish, but... Pin
cocoskili9-Sep-08 3:41
cocoskili9-Sep-08 3:41 

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

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