您的位置:首页 - 教程 - WinForm - 正文
练习:WinForm (Button:Do you love me?)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Do_you_love_me
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 当鼠标移动到当前控件上时,随机移动位置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void unlove_MouseEnter(object sender, EventArgs e)
        {
            //给按钮一个新的坐标
            //这个按钮活动的最大宽度就是窗体的宽度减去按钮本身的宽度
            int x = this.ClientSize.Width - unlove.Width;
            int y = this.ClientSize.Height - unlove.Height;
            //给按钮一个随机坐标
            Random r = new Random();
            unlove.Location = new Point(r.Next(0,x+1),r.Next(0,y+1));
        }

        private void love_Click(object sender, EventArgs e)
        {
            MessageBox.Show("我也爱你~");
            this.Close();
        }

        private void unlove_Click(object sender, EventArgs e)
        {
            MessageBox.Show("被你点到了~");
            this.Close();
        }
    }
}


评论: