Making a fluttering form effect with C #

Source: Internet
Author: User

Recently looked at the previous learning C # contact code, inadvertently found a very interesting project. Is the effect of a fluttering form, after running the program, the current screen will be like snowflakes fluttering a lot of custom icons, and they are like snowflakes light from the top of the screen falling to the bottom of the screen, until disappeared. A certain number of snowflakes will be maintained on the screen during the running of the program. In the system tray area will have an icon, click on this icon, you can exit the program. This contact code contacts how to use irregular forms and system tray controls.

Core source code in program:

View Plaincopy to Clipboardprint?

using System;


using System.Drawing;


using System.Drawing.Drawing2D;


using System.Windows.Forms;


namespace Fallinggold


{


  ///


///Description: Picture Contour class, through this class provides the method to obtain the picture's periphery contour


///Author: Zhou Gong


///Original address: http://blog.csdn.net/zhoufoxcn/archive/2008/06/06/2515753.aspx


  ///


public class Bitmapregion


  {


public bitmapregion ()


    { }


public static void Createcontrolregion (Control control, Bitmap Bitmap)


     {


//If the control or image is empty


if (Control = null | | bitmap = NULL)


      {


return;


      }


//Set the control to the same size as the image


control. Width = Bitmap. Width;


control. Height = Bitmap. Height;


//If you are working with a form object


if (control is System.Windows.Forms.Form)


      {


form form = control as form;//cast to Form instance


//Set the size of the form slightly larger than the picture, so you don't have to display the form border


form. Width = 15;


form. Height = 35;


//Set form no border


form. FormBorderStyle = Formborderstyle.none;


//Set form background


form. BackgroundImage = bitmap;


//According to the picture calculation path


GraphicsPath GraphicsPath = Calculatecontrolgraphicspath (bitmap);


//application area


Form. Region = new Region (GraphicsPath);


      }


//If you are dealing with a button object


else if (control is System.Windows.Forms.Button)


      {


button button = control as button;//cast to Button instance


//Do not display text


button. Text = "";


//Change the cursor state when the mouse is over


button. Cursor = Cursors.hand;


//Set background picture


button. BackgroundImage = bitmap;


//According to the picture calculation path


GraphicsPath GraphicsPath = Calculatecontrolgraphicspath (bitmap);


//application area


button. Region = new Region (GraphicsPath);


      }





    }


    ///


///scans the contour of a picture by approximation


    ///


///to scan the picture


    ///


private static GraphicsPath Calculatecontrolgraphicspath (Bitmap Bitmap)


    {


GraphicsPath GraphicsPath = new GraphicsPath ();


//Defines the color of the picture (0,0) as a transparent color


Color transparentcolor = bitmap. GetPixel (0, 0);


//Stores the value of the first opaque pixel (that is, the x-coordinate), which defines the edges of the opaque area that we scan


int opaquepixelx = 0;


//Start from Portrait


for (int y = 0; y < bitmap.) Height; y++)


      {


opaquepixelx = 0;


for (int x = 0; x < bitmap. Width; x + +)


         {


if (bitmap. GetPixel (x, y)!= TransparentColor)


          {


//mark the position of an opaque pixel


opaquepixelx = x;


//record current position


int nextx = x;


for (nextx = Opaquepixelx; nextx < bitmap. Width; nextx++)


            {


if (bitmap. GetPixel (nextx, y) = = TransparentColor)


              {


break;


              }


             }


Graphicspath.addrectangle (New Rectangle (Opaquepixelx, Y, Nextx-opaquepixelx, 1));


x = NEXTX;


          }


        }


      }


return GraphicsPath;


    }


  }


}

View Plaincopy to Clipboardprint?

view Plaincopy to Clipboardprint?


using System;


using System.Collections.Generic;


using System.ComponentModel;


using System.Data;


using System.Drawing;


using System.Text;


Using System.Windows.Forms;


namespace Fallinggold


{


  ///


///Description: Fluttering form


///Author: Zhou Gong


///Original address: http://blog.csdn.net/zhoufoxcn/archive/2008/06/06/2515753.aspx


  ///


public partial class Goldform:form


  {


private int currentx;//picture's current horizontal axis


private int currenty;//picture's current ordinate


private int screenheight;//screen height


private int screenwidth;//screen width


private int counter;//picture number


private int increment;//move increment


private int interval;//move time interval


private Bitmap bmpflake = Properties.Resources.snow;


    ///


///Construction Function


    ///


///Move Interval

The horizontal axis
of the
///flapping form

public goldform (int interval, int currentx)


{


This.interval = interval + 10;


this.currentx = CurrentX;


InitializeComponent ();


bitmapregion.createcontrolregion (this, bmpflake);


    }


private void Goldform_load (object sender, EventArgs e)


    {


//Get the working area of the screen, excluding the status bar


Rectangle rectangleworkarea = Screen.PrimaryScreen.WorkingArea;


screenheight = rectangleworkarea.height;


screenwidth = rectangleworkarea.width;


timermove.interval = interval;//Set timer interval


this. left = currentx;//Sets the starting horizontal axis of the form


Timermove.start ()//Run timer


     }


//timer Event


private void Timermove_tick (object sender, EventArgs e)


    {


Timermove.stop ();


CurrentY + + 5;


counter++;


Random Random = new Random ();


if (counter = 15)


      {


if (random. Next (5) > 0)


        {


increment = 1;


         }


Else


        {


increment =-1;


        }


counter = 0;


      }


CurrentX + = increment;


if (CurrentY > ScreenHeight)


       {


currenty = 0;


CurrentX = random. Next (ScreenWidth);


interval = random. Next (50,100);


      }


//sets the form position, which is equivalent to moving the form


this. Location = new Point (CurrentX, currenty);


timermove.interval = Interval;


Timermove.start ();


}


  }


}

View Plaincopy to Clipboardprint?

The source code of the entire program please go to http://download.csdn.net/source/484535 download.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.