마우스 커서를 피하는 버튼

Ad


나 잡아봐라 버튼~
Catch me if you can

이벤트를 익히기 위해 윈도우 응용프로그램을 연습중이다.

아래의 프로그램은 마우스를 가까이 가져가면 도망가는 버튼이다.
일명 “나잡아봐라~ 버튼” …
잠깐 잡고 있다보면 열받기 시작한다.

그게 중요한건 아니고,

랜덤으로 변수를 두개만들고 프로그램을 실행하니
간혹 마우스 커서 밑으로 튀어버리는 경우가 있어
약간 변칙으로 튀었는데 마우스 커서가 있으면 다시 튀어버리게 했다.
조금씩 약올리면서 도망가는건 어떻게 해야할지 감이 안잡혀서 일단
여기까지 하기로 했다.

실행화면 :


catch me if you can
실행화면
Flickr에서 보기

2006년 버젼

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication6
{
  public partial class Form1 : Form
  {
       Rectangle rec;
       Random rn;

       public Form1()
       {
           InitializeComponent();
       }

       private void Form1_MouseMove_1(object sender, MouseEventArgs e)
       {
           rn = new Random();
           int a = rn.Next(this.Width-80);
           int b = rn.Next(this.Height-80);
           Point mPos = e.Location;
           Point btPos = button1.Location;
           //toolStripStatusLabel1.Text = e.X + "," + e.Y;
           rec = new Rectangle(button1.Location, button1.Size);
           rec.Inflate(30, 30);
           //rec.Offset(-10, -10);
           if (rec.Contains(e.Location))
           {
               if (!rec.Contains(a,b) ||rec.Left>a || rec.Right<a || rec.Top>b || rec.Bottom<b)
               {
                   button1.Location = new Point(a, b);
                   //toolStripStatusLabel1.Text = "들어왔다.";
               }
           }
           else
           {
               return;
           }
        }

       private void button1_Click(object sender, EventArgs e)
       {
           MessageBox.Show("헉!! 이럴수가");
           
       }

       private void button1_MouseMove(object sender, MouseEventArgs e)
       {
           int a = rn.Next(this.Width-50);
           int b = rn.Next(this.Height-80);
           button1.Location = new Point(a, b);
       }

       private void Form1_Load(object sender, EventArgs e)
       {

       }
  }
}

2014년 버젼

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CatchMeIfYouCan
{
    public partial class Form1 : Form
    {
        private Point currentMouseLocation;

        public Form1()
        {
            InitializeComponent();

            this.MouseMove += Form1_MouseMove_1;
            this.button1.Click += button1_Click;
            this.button1.MouseMove += button1_MouseMove;
            this.SizeChanged += Form1_SizeChanged;
            this.button1.TabStop = false;

            this.label1.Text = "";
        }

        private void MoveRandomLocation(bool forceMove)
        {
            try
            {
                Point mPos = this.currentMouseLocation;

                Random rn = new Random();
                int a = 0;
                int b = 0;

                Point btPos = button1.Location;
                Rectangle rec = this.ButtonArea();
                Rectangle thisForm = new Rectangle(0, 0, this.Width - this.button1.Size.Width, this.Height - this.button1.Size.Height);
                if (this.ContainsButtonArea(mPos) || forceMove)
                {
                    do
                    {
                        rn = new Random();
                        a = rn.Next(this.DisplayRectangle.Width - this.button1.Size.Width);
                        b = rn.Next(this.DisplayRectangle.Height - this.button1.Size.Height);
                        //if (!rec.Contains(a, b) || rec.Left > a || rec.Right < a || rec.Top > b || rec.Bottom < b)
                        //{
                        //    break;
                        //}
                        if (!rec.Contains(a, b) && thisForm.Contains(a, b)) { break; }
                    }
                    while (rec.Contains(a, b));

                    button1.Location = new Point(a, b);
                }
            }
            finally
            {
                this.ShowLabelData();
            }
        }

        private void MoveRandomLocation()
        {
            this.MoveRandomLocation(false);
        }

        private Rectangle ButtonArea()
        {
            Rectangle rec = new Rectangle(button1.Location.X - 5, button1.Location.Y - 5, button1.Size.Width + 10, button1.Size.Height + 10);
            rec.Inflate(30, 30);
            return rec;
        }

        private bool ContainsButtonArea(Point p)
        {
            Rectangle rec = this.ButtonArea();
            return rec.Contains(p);
        }

        private void ShowLabelData()
        {
            if (!this.currentMouseLocation.IsEmpty)
            {
                this.label1.Text = string.Format("Mouse Position : ({0},{1})", this.currentMouseLocation.X, this.currentMouseLocation.Y);
            }
        }

        private void Form1_MouseMove_1(object sender, MouseEventArgs e)
        {
            this.currentMouseLocation = e.Location;
            this.MoveRandomLocation();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("You Won!!");
        }

        private void button1_MouseMove(object sender, MouseEventArgs e)
        {
            this.MoveRandomLocation(true);
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            Size thisSize = this.Size;
            if (this.Size.Width < (this.button1.Location.X + this.button1.Size.Width) || this.Size.Height < (this.button1.Location.Y + this.button1.Size.Height))
            {
                // Form Size < button Location
                this.MoveRandomLocation(true);
            }
        }
    }
}


Ad


댓글 남기기