分享

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

 9loong 2012-02-17

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

  • 2008/9/8: First posted on .

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


(###)

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多