當(dāng)前位置:
首頁 >
C#模拟MSN窗体抖动[原创]
發(fā)布時(shí)間:2023/12/20
40
豆豆
生活随笔
收集整理的這篇文章主要介紹了
C#模拟MSN窗体抖动[原创]
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
C#模擬MSN窗體抖動
窗體抖動是件很有意思的事情,就讓我們看看一起來看看它的原理吧。?
其實(shí)是生成隨機(jī)數(shù),然后改變Form的左上角的坐標(biāo)。
我用的是循環(huán)來弄得,其實(shí)可以用timer來控制.
?我把抖動分成了兩種抖動:
1。 生成隨機(jī)數(shù),改變窗體左上角坐標(biāo),然后立即把窗體的坐上角坐標(biāo)還原,繼續(xù)循環(huán)。
2。 生成隨機(jī)數(shù),改變窗體左上角坐標(biāo),循環(huán)完畢之后,然后立即把窗體的坐上角坐標(biāo)還原。
核心代碼如下:
????????private?void?button1_Click(object?sender,?EventArgs?e)
????????{
????????????int?recordx?=?this.Left;????????????//保存原來窗體的左上角的x坐標(biāo)
????????????int?recordy?=?this.Top;?????????????//保存原來窗體的左上角的y坐標(biāo)
????????????Random?random?=?new?Random();???????
????????????for?(int?i?=?0;?i?<?100;?i++)
????????????{
????????????????int?x?=?random.Next(rand);
????????????????int?y?=?random.Next(rand);
????????????????if?(x?%?2?==?0)
????????????????{
????????????????????this.Left?=?this.Left?+?x;
????????????????}
????????????????else
????????????????{
????????????????????this.Left?=?this.Left?-?x;
????????????????}
????????????????if?(y?%?2?==?0)
????????????????{
????????????????????this.Top?=?this.Top?+?y;
????????????????}
????????????????else
????????????????{
????????????????????this.Top?=?this.Top?-?y;
????????????????}
????????????????this.Left?=?recordx;????????????//還原原始窗體的左上角的x坐標(biāo)
????????????????this.Top?=?recordy;?????????????//還原原始窗體的左上角的y坐標(biāo)
????????????}
???????????
????????}
?
????????//第二種抖動
????????private?void?button2_Click(object?sender,?EventArgs?e)
????????{
????????????int?recordx?=?this.Left;
????????????int?recordy?=?this.Top;
????????????Random?random?=?new?Random();
????????????for?(int?i?=?0;?i?<?50;?i++)
????????????{
????????????????int?x?=?random.Next(rand);
????????????????int?y?=?random.Next(rand);
????????????????if?(x?%?2?==?0)
????????????????{
????????????????????this.Left?=?this.Left?+?x;
????????????????}
????????????????else
????????????????{
????????????????????this.Left?=?this.Left?-?x;
????????????????}
????????????????if?(y?%?2?==?0)
????????????????{
????????????????????this.Top?=?this.Top?+?y;
????????????????}
????????????????else
????????????????{
????????????????????this.Top?=?this.Top?-?y;
????????????????}
????????????????System.Threading.Thread.Sleep(1);
????????????}
????????????this.Left?=?recordx;
????????????this.Top?=?recordy;
????????}
Demo下載
總結(jié)
以上是生活随笔為你收集整理的C#模拟MSN窗体抖动[原创]的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 保护SQL Server数据库的十大绝招
- 下一篇: c#学习体会:使用 ref 和 out