C#窗体之整人小程序
今天在人人上看到一個(gè)好玩的gif圖,是這樣的
看著挺好玩。這典型的C#中監(jiān)聽(tīng)鼠標(biāo)各種事件的例子。當(dāng)然不是說(shuō)只能用C#做。語(yǔ)言無(wú)界限嘛。
首先拖一個(gè)按鈕上去,改文字,然后給這個(gè)按鈕加上監(jiān)聽(tīng)讓它彈出來(lái)一個(gè)對(duì)話框,平常總是用messagebox。要是java語(yǔ)言的話這會(huì)就得繼承對(duì)話框自定義一個(gè)了吧。C#應(yīng)該也差不多。這次不用java主要是因?yàn)殡m然java號(hào)稱到處運(yùn)行,你也得裝jvm啊。一個(gè)整人的小程序不能先讓人裝個(gè)jvm再看吧。。
之前弄過(guò)一會(huì)C#不過(guò)都忘的干凈了。
這次再試一下。本來(lái)說(shuō)是用一個(gè)對(duì)話框,然后想去添加個(gè)類,然后在工程上點(diǎn)右鍵看見(jiàn)可以添加窗體,那么直接添加一個(gè)窗體不就好了嘛哈哈,然后自然肯定就有show方法了。結(jié)果還是很簡(jiǎn)單的。就是這樣就做到了。那么下一部分其實(shí)就是給這兩個(gè)按鈕加上各種監(jiān)聽(tīng)的移動(dòng)還有變換文字了。個(gè)人認(rèn)為比較 難的點(diǎn)其實(shí)就是如何讓顯示按鈕的移動(dòng)軌跡。在程序一般移動(dòng)都是閃一下就過(guò)去的。那么這次可能要用一個(gè)新的線程?計(jì)時(shí)移動(dòng)了吧、
VS跟eclipse真是無(wú)法比覺(jué)得,eclipse在用一個(gè)沒(méi)用過(guò)的按鈕的時(shí)候直接alt+/就能看看開(kāi)發(fā)文檔,知道參數(shù)是什么 。C#中封裝的不徹底。也許是我java用習(xí)慣了吧
private void button2_MouseMove(object sender, MouseEventArgs e){if (button2.Location.Y > 50 && flag){button2.Location = new System.Drawing.Point(button2.Location.X, button2.Location.Y - 1);}else{flag = false;button2.Location = new System.Drawing.Point(button2.Location.X, button2.Location.Y + 1);}if (button2.Location.Y > 128)flag = true;}
方法一,并沒(méi)有用到計(jì)時(shí)器。這個(gè)實(shí)現(xiàn)了大概功能就是當(dāng)鼠標(biāo)快要接觸到按鈕的時(shí)候按鈕會(huì)移動(dòng),為了當(dāng)?shù)揭欢ㄎ恢脮r(shí)可以轉(zhuǎn)向。用了一個(gè)flag標(biāo)記。到底的時(shí)候再轉(zhuǎn)回來(lái)。但是沒(méi)有動(dòng)畫效果
方法二,從網(wǎng)上查了一下例子,知道了timer這個(gè)東西。我記著以前用過(guò),是VB吧。。不管是什么反正最終實(shí)現(xiàn)了效果。
private void button2_MouseMove(object sender, MouseEventArgs e){run();}private void run(){timer1.Interval = 10;timer1.Start();}private void timer1_Tick(object sender, EventArgs e){if (button2.Location.Y >= 50 && flag){button2.Location = new System.Drawing.Point(button2.Location.X, button2.Location.Y - 1);}else{flag = false;button2.Location = new System.Drawing.Point(button2.Location.X, button2.Location.Y + 1);}if (button2.Location.Y >=button1.Location.Y|| button2.Location.Y < 50){flag = true;timer1.Stop();}}這個(gè)實(shí)現(xiàn)后面就簡(jiǎn)單了,再加一個(gè)計(jì)數(shù)的,動(dòng)兩回之后變字。
全部代碼就這樣啦~
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 WindowsFormsApplication1 {public partial class Form2 : Form{Boolean flag = true;int time = 0;public Form2(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){MessageBox.Show("我就知道你會(huì)選是的");}private void Form2_Load(object sender, EventArgs e){}private void button2_KeyUp(object sender, KeyEventArgs e){button2.Location = new System.Drawing.Point(2, 34);}private void button2_MouseUp(object sender, MouseEventArgs e){button2.Location = new System.Drawing.Point(2, 34);}private void button2_MouseMove(object sender, MouseEventArgs e){if (time <= 1)run();else {button1.Text = "不是";button2.Text = "是";}}private void run(){timer1.Interval = 7;timer1.Start();}private void timer1_Tick(object sender, EventArgs e){if (button2.Location.Y >= 50 && flag){button2.Location = new System.Drawing.Point(button2.Location.X, button2.Location.Y - 1);}else{flag = false;button2.Location = new System.Drawing.Point(button2.Location.X, button2.Location.Y + 1);}if (button2.Location.Y >=button1.Location.Y|| button2.Location.Y < 50){time++;flag = true;timer1.Stop();}}private void button1_MouseMove(object sender, MouseEventArgs e){button2.Text = "不是";button1.Text = "是";}private void Form2_FormClosed(object sender, FormClosedEventArgs e){MessageBox.Show("關(guān)了窗口也改變不了你是煞筆的事實(shí)");}private void button2_Click(object sender, EventArgs e){MessageBox.Show("我就知道你會(huì)選是的");}} }
為了配合一下樣式再改點(diǎn)
總結(jié)
以上是生活随笔為你收集整理的C#窗体之整人小程序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 如何判断绩效管理系统实施是否有效
- 下一篇: php递归函数及简单实例讲解