C#窗体--鼠标事件

常见的鼠标事件:
mouseclick,mousedown,mouseup,mousuenter,mouseleave.mousemove


mouseDown按下鼠标事件:

//鼠标按下后显示
 private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            MessageBox.Show("哈哈我有帅了");
            button1.Text = "惦记我";
        }

MouseUp鼠标抬起事件:

//鼠标抬起后显示:
 private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            MessageBox.Show("确实如此啊");
        }

MouseEnter提示事件:

//只要鼠标在空白处则提出显示:
private void Form1_MouseEnter(object sender, EventArgs e)
        {
            MessageBox.Show("哈哈,我又来了");
        }

MouseLeave弹窗事件:

//只要没有在窗体空白处则弹出窗体:

 private void Form1_MouseLeave(object sender, EventArgs e)
        {
            MessageBox.Show("弹出窗体");
        }

MouseDoubleClick双击事件:

//鼠标双击之后显示:
 private void Form1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            MessageBox.Show("大白又帅了");
        }

MouseMove移动事件:

//在标题中显示鼠标移动时候的坐标:

 private void Form1_MouseMove(object sender, MouseEventArgs e)
        {           
            this.Text=string.Format("x:{0},y:{1}",e.X,e.Y );
        }

MouoseDouble双击事件

鼠标双击之后显示:
 private void Form1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            MessageBox.Show("大白又帅了");
        }

小游戏:鼠标碰不到我

//每次碰到后则发生位置变化,第七次后则弹出信息。
 int i = 0;
        private void picZXH_MouseEnter(object sender, EventArgs e)
        {
            int xWidth = this.ClientSize.Width-picZXH.Width ;//获取窗体的宽度
            int yHeight = this.ClientSize.Height-picZXH.Height ;//获取窗体的高度

            Random r = new Random();  //定义随意数

            int xZxh = r.Next(xWidth + 1);
            int yZxh=r.Next (yHeight+1);
            picZXH.Location = new Point(xZxh,yZxh );//获取随意坐标

            this.BackColor = Color.Gray; //移动后改变颜色
            i++;
            if (i%7==0)   //移动七次后弹出窗体和网页,并将背景变黑
            {
                MessageBox.Show("haha ,我是最邪恶的,看看我是谁");
                System.Diagnostics.Process.Start("http://www.itcast.cn");
                this.BackColor = Color.Black;
            }

你可能感兴趣的:(#,【C#-window窗口】)