開始學習C#了,做的第一款垃圾軟件:
http://download.csdn.net/detail/zhoujiachengdhy/7226423
源碼:
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 PictureViewer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void showButton_Click(object sender, EventArgs e)
{
/* Show the Open File dialog. If the user clicks OK, load the
picture that the user chose.
*/
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
pictureBox1.Load(openFileDialog1.FileName);
}
}
private void clearButton_Click(object sender, EventArgs e)
{
// Clear the picture.
pictureBox1.Image = null;
}
private void backgroundButton_Click(object sender, EventArgs e)
{
// Show the color dialog box. If the user clicks OK, change the
// PictureBox control's background to the color the user chose.
if (colorDialog1.ShowDialog() == DialogResult.OK)
pictureBox1.BackColor = colorDialog1.Color;
}
private void closeButton_Click(object sender, EventArgs e)
{
// Close the form.
this.Close();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
// If the user selects the Stretch check box,
// change the PictureBox's
// SizeMode property to "Stretch". If the user clears
// the check box, change it to "Normal".
if (checkBox1.Checked)
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
else
pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
}
}
}
第二款垃圾軟件
http://download.csdn.net/detail/zhoujiachengdhy/7228407
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Math_Quiz
{
public partial class Form1 : Form
{
Random randomizer = new Random();//隨機數生成器
int addend1, addend2;
int minu1, minu2;
int mul1, mul2;
int div1, div2;
int timeleft;
public void StartTheQuiz()
{
//+
addend1 = randomizer .Next(51);
addend2 = randomizer.Next(51);
plusLeftLabel.Text = addend1.ToString();//將數字轉換爲文本
plusRightLabel.Text = addend2.ToString();
sum.Value = 0;
//-
minu1 = randomizer.Next(1, 101);
minu2 = randomizer.Next(1, minu1);
minusLeftLabel.Text = minu1.ToString();
minusRightLabel.Text = minu2.ToString();
difference.Value = 0;
//*
mul1 = randomizer .Next(51);
mul2 = randomizer.Next(51);
mulLeftLabel.Text = mul1.ToString();
mulRightLabel.Text = mul2.ToString();
product.Value = 0;
// /
div1 = randomizer.Next(2, 51);
int temp = randomizer.Next(2, 51);
div2 = div1 * temp;
dividedRightLabel.Text = div1.ToString();
dividedLeftLabel.Text = div2.ToString();
quotient.Value = 0;
//timer
timeleft = 10;
timeLabel.Text = "10 seconds";
timer1.Start();
}
/*
在此示例中,您調用了 randomizer.Next(51)。 您使用了 51 而不是 50,
以便兩個隨機數相加所得的答案介於 0 到 100 之間。 如果將 50 傳遞給 Next() 方法,
則此方法會選擇一個介於 0 到 49 之間的數字,因此可能得到的最大答案是 98,而不是
100。 在執行此方法中的前兩個語句後,這兩個整型變量(addend1 和 addend2)都將保
留一個 0 到 50 之間的隨機數。
*/
private bool Checkans()
{
if( (addend1 + addend2 == sum.Value)&&(minu1 - minu2 == difference .Value)&&(mul1*mul2==product .Value)&&(div2==div1*quotient.Value))
return true;
else return false;
}
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label1_Click_1(object sender, EventArgs e)
{
}
private void sum_ValueChanged(object sender, EventArgs e)
{
}
private void startButton_Click(object sender, EventArgs e)
{
StartTheQuiz();
startButton.Enabled = false; //使測驗對象在測驗期間不能選擇此按鈕。
}
private void timer1_Tick(object sender, EventArgs e)
{
if (Checkans())
{
timer1.Stop();
MessageBox.Show("You got all the answer right !", "Congratulations !");
startButton.Enabled = true;
}
else if (timeleft > 0)
{
timeleft--;
timeLabel.Text = timeleft + " seconds";
}
else
{
timer1.Stop();
timeLabel.Text = "Time is up !";
MessageBox.Show("You didn't finish in time.", "Sorry");
sum.Value = addend1 + addend2;
difference.Value = minu1 - minu2;
product.Value = mul1 * mul2;
quotient.Value = div2 / div1;
startButton.Enabled = true;
}
}
private void answer_Enter(object sender, EventArgs e)
{
NumericUpDown answerBox = sender as NumericUpDown;
if (answerBox != null)
{
int lengthOfAnswer = answerBox.Value.ToString().Length;
answerBox.Select(0, lengthOfAnswer);
}
}
}
}
第三款垃圾軟件
http://download.csdn.net/detail/zhoujiachengdhy/7233765
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;
/*
1. 當玩家單擊其中一個帶有隱藏圖標的方塊時,程序通過將圖標顏色更改爲黑色來向玩家顯示該圖標。
2. 然後玩家單擊另一個隱藏的圖標。
3. 如果圖標互相匹配,則它們保持可見。 如果不匹配,則兩個圖標都會再次隱藏。
*/
namespace Matching
{
public partial class Form1 : Form
{
int timespent;
Random random = new Random();
List<string> icons = new List<string>()
{
"!","N",",","k","b","v","w","z",
"!","N",",","k","b","v","w","z"
};
/*
列表中的項目稱爲“元素”,每個列表只包含一種元素。 所以數字列表只包含數字 - 您不能向其中添加文本。 同樣,您也不能向 true/false 值列表添加數字。
*/
private void iTOsquares()//
{
foreach (Control control in tableLayoutPanel1.Controls)//使用 foreach 循環重複執行相同操作
{
Label iconLabel = control as Label;
if (iconLabel != null)
{
int ranNum = random.Next(icons.Count);
iconLabel.Text = icons[ranNum];
iconLabel.ForeColor = tableLayoutPanel1.BackColor;
iconLabel.BackColor = Color.Transparent;
icons.RemoveAt(ranNum);
}
}
}
//確定前兩個被點擊的標籤,並一起消失;
Label fisChick = null,secChick = null;
public Form1()
{
InitializeComponent();
iTOsquares();
MessageBox.Show("將所有圖標兩兩匹配後即可...", "遊戲規則");
}
private void lable_chick(object sender, EventArgs e)
{
if (timer1.Enabled ==true) return;
Label chick =sender as Label;
if (chick != null)
{
if (chick.ForeColor == Color.Black) return;
//如果已經爲黑色,則該圖標已被單擊,因此該方法完成。
if (fisChick == null)
{
fisChick = chick;
fisChick.ForeColor = Color.Black;
return;
}
//如果該圖標尚未被單擊,它會將其文本顏色更改爲黑色。
secChick = chick;
secChick.ForeColor = Color.Black;
CheckForWiner();
if (fisChick.Text == secChick.Text)
{
fisChick = null;
secChick = null;
return;
}
timer1.Start();
}
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
fisChick.ForeColor = tableLayoutPanel1.BackColor;
secChick.ForeColor = tableLayoutPanel1.BackColor;
//兩個圖標都藏起來
fisChick = null;
secChick = null;
}
private void CheckForWiner()
{
foreach (Control control in tableLayoutPanel1.Controls)
{
Label iconLabel = control as Label;
if (iconLabel != null)
{
if (iconLabel.ForeColor == tableLayoutPanel1.BackColor) return;
}
}
MessageBox.Show("You matched all the icons !", "Congratulations !");
Close();
}
private void tableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
{
// 單元格重繪
Pen pp = new Pen(Color.Black);
e.Graphics.DrawRectangle(pp, e.CellBounds.X, e.CellBounds.Y, e.CellBounds.X + e.CellBounds.Width - 1, e.CellBounds.Y + e.CellBounds.Height - 1);
}
private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
{
}
private void timeLabel_Click(object sender, EventArgs e)
{
}
}
}