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;
}

————————————————
版權聲明:本文為CSDN博主「Herry-白愛民」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/aimin_com/article/details/80645269


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM