C#窗體--滑鼠事件

Herry-白愛民發表於2018-06-10

常見的滑鼠事件:
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;
            }

相關文章