C#窗体之整人小程序
生活随笔
收集整理的這篇文章主要介紹了
C#窗体之整人小程序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
今天在人人上看到一個好玩的gif圖,是這樣的
看著挺好玩。這典型的C#中監聽鼠標各種事件的例子。當然不是說只能用C#做。語言無界限嘛。
首先拖一個按鈕上去,改文字,然后給這個按鈕加上監聽讓它彈出來一個對話框,平??偸怯胢essagebox。要是java語言的話這會就得繼承對話框自定義一個了吧。C#應該也差不多。這次不用java主要是因為雖然java號稱到處運行,你也得裝jvm啊。一個整人的小程序不能先讓人裝個jvm再看吧。。
之前弄過一會C#不過都忘的干凈了。
這次再試一下。本來說是用一個對話框,然后想去添加個類,然后在工程上點右鍵看見可以添加窗體,那么直接添加一個窗體不就好了嘛哈哈,然后自然肯定就有show方法了。結果還是很簡單的。就是這樣就做到了。那么下一部分其實就是給這兩個按鈕加上各種監聽的移動還有變換文字了。個人認為比較 難的點其實就是如何讓顯示按鈕的移動軌跡。在程序一般移動都是閃一下就過去的。那么這次可能要用一個新的線程?計時移動了吧、
VS跟eclipse真是無法比覺得,eclipse在用一個沒用過的按鈕的時候直接alt+/就能看看開發文檔,知道參數是什么 。C#中封裝的不徹底。也許是我java用習慣了吧
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;}
方法一,并沒有用到計時器。這個實現了大概功能就是當鼠標快要接觸到按鈕的時候按鈕會移動,為了當到一定位置時可以轉向。用了一個flag標記。到底的時候再轉回來。但是沒有動畫效果
方法二,從網上查了一下例子,知道了timer這個東西。我記著以前用過,是VB吧。。不管是什么反正最終實現了效果。
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();}}這個實現后面就簡單了,再加一個計數的,動兩回之后變字。
全部代碼就這樣啦~
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("我就知道你會選是的");}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("關了窗口也改變不了你是煞筆的事實");}private void button2_Click(object sender, EventArgs e){MessageBox.Show("我就知道你會選是的");}} }
為了配合一下樣式再改點
總結
以上是生活随笔為你收集整理的C#窗体之整人小程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何判断绩效管理系统实施是否有效
- 下一篇: C#Socket编程TCP实例(四)