现在的位置: 首页 > 综合 > 正文

让你的桌面下玫瑰

2012年09月11日 ⁄ 综合 ⁄ 共 1799字 ⁄ 字号 评论关闭

效果

  .Net 2.0 版  .Net 3.5版  .Net 4.0版

  截图:

  Falling rose... 

原理

  这个实现很简单,没有什么技术含量,但是拿来哄下女朋友啥的还是可以的:)

  上学时朋友给我发过一个下雪的圣诞礼物,于是一直惦记着要实现它,突然发现简单来处理的话也还是不复杂的

  • 单朵玫瑰(一个影藏掉边框的roseForm,加一张玫瑰花图片)
  • 单朵下落(一个timer,每秒把重新调整roseForm的高度和宽度以达到下落左右飘逸,同时到达底部后回到顶端
    ps: 也可加上自己的小功能,譬如玫瑰的拖拽或者Hove悬停再或者点击引发更漂亮的效果等。
  • mainForm设置roseForm的个数一级开始位置

实现

  • mainForm设置roseForm的个数一级开始位置

    private void OnLoad(object sender, EventArgs e)

    {

    int timer = 5;

     

    int posX = 0;

    Random rnd = new Random();

    for (int i = 0; i < 10; i++)

    {

    timer = rnd.Next(50);

    posX = rnd.Next(Screen.PrimaryScreen.WorkingArea.Width);

    Flakes fk = new Flakes(timer, posX);

    fk.Show();

    }

    }

  • 单朵玫瑰

    public Flakes(int timer, int currentWidth)

    {

    this.currentWidth = currentWidth;

    InitializeComponent();

    // initialize the rose form

    BitmapRegion.CreateControlRegion(this, bmpFlake);

    }
    其中BitmapRegion有2个方法

    void

    CreateControlRegion(Control control, Bitmap bitmap)    //对传入的control进行初始化(去掉border一级设置传入的bitmap为背景即所使用的rose)

    GraphicsPath

    CalculateControlGraphicsPath(Bitmap bitmap)    //计算图形路径,用于CreateControlRegion()中所提供的bitmap

  • 单朵下落
    • 下落
      roseForm的Tick触发改变Height

      // Move down

      currentHeight += 5;

    • 左右飘逸
      依旧是roseForm的Tick
      Random r = new Random();

      if (counter == 20)

      {

      if (r.Next(10) > 5) increment = 1;

      else increment = -1;

      counter = 0;

      }

      currentWidth += increment;

    • 到达底部后回到顶端
      还是roseForm的Tick

      // If reach the bottom

      if (currentHeight > screenHeight)

      {

      currentHeight = 0;

      currentWidth = r.Next(screenWidth);

      timer = r.Next(50) + 10;

      }


      那么完整的Tick事件要做的应该是:

      private void OnTimer(object sender, EventArgs e)
              {
              this.timer1.Stop();
              // Move down
              currentHeight += 5;
              counter++;
              // Figure out the horizontal coordinate
              Random r = new Random();
              if (counter == 20)
              {
              if (r.Next(10) > 5) increment = 1;
              else increment = -1;
              counter = 0;
              }
              currentWidth += increment;
              // If reach the bottom
              if (currentHeight > screenHeight)
              {
              currentHeight = 0;
              currentWidth = r.Next(screenWidth);
              timer = r.Next(50) + 10;
              }
              this.Left = currentWidth;
              this.Top = currentHeight;
              this.timer1.Interval = timer;
              this.timer1.Start();
              }
              

不足,待改进

未实现遇到桌面图标悬停在上面

使用多个form引发按Alt+Tab时窗口过多~

 

 

抱歉!评论已关闭.