要求:
目标3的功能简介:有若干张图片(自己确定,不少于5张,命名方法自定),窗体有3个控件(PictureBox,按钮,label);按下”随机显示“按钮,则随机显示一张图片,并给出该图片显示的次数在右边的label中;同时在”关闭“窗体时,把每张图片目前已经显示的次数写入到指定文件中,方便下次程序运行时使用。
大家可以再增加功能,比如图片名称为”学号姓名“,显示姓名在图片上方(增加一个label);……;越来越接近玩”猜猜看“(显示一张相片,给定3个候选名字,猜测;或 显示3张相片,给定1个候选名字,猜测是那一张相片等)。
第一步:做了可视化设计及部分属性的初始化:
如下图所示:
第二步:在form1.cs中对一些事件和函数封装:
代码如下:
form1.cs:
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; using System.IO; namespace WindowsFormsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void show() { Random x = new Random(); int i = x.Next(1, 10); string r = @"C:\Users\Administrator.LBDZ-20121019BZ\Documents\Visual Studio 2013\Projects\WindowsFormsApplication2\WindowsFormsApplication2\imgdata\"+i+".jpg"; Bitmap img1 = new Bitmap(r); pictureBox1.Image=img1; lbl1.Text = "随机次数:" + op; lbl2.Text = "图片:"+i + ".jpg"; } private void infoout() { using (FileStream aFile = new FileStream(@"C:\Users\Administrator.LBDZ-20121019BZ\Documents\Visual Studio 2013\Projects\WindowsFormsApplication2\WindowsFormsApplication2\imgdata\info.txt", FileMode.OpenOrCreate)) { using (StreamWriter sw = new StreamWriter(aFile, Encoding.UTF8)) { sw.WriteLine(lbl1.Text); sw.WriteLine("当前显示图片为"+lbl2.Text); } } } private void button1_Click(object sender, EventArgs e) { op++; show(); } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { infoout(); } } }
具体的函数和属性代码如下
Form1.Designer.cs:
namespace WindowsFormsApplication2 { partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.btn1 = new System.Windows.Forms.Button(); this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.lbl1 = new System.Windows.Forms.Label(); this.lbl2 = new System.Windows.Forms.Label(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); this.SuspendLayout(); // // btn1 // this.btn1.BackColor = System.Drawing.SystemColors.ActiveCaption; this.btn1.Location = new System.Drawing.Point(87, 337); this.btn1.Name = "btn1"; this.btn1.Size = new System.Drawing.Size(75, 23); this.btn1.TabIndex = 0; this.btn1.Text = "随机显示"; this.btn1.UseVisualStyleBackColor = false; this.btn1.Click += new System.EventHandler(this.button1_Click); // // pictureBox1 // this.pictureBox1.ImageLocation = "C:\\Users\\Administrator.LBDZ-20121019BZ\\Documents\\Visual Studio 2013\\Projects\\Wind" + "owsFormsApplication2\\WindowsFormsApplication2\\imgdata\\1.jpg"; this.pictureBox1.Location = new System.Drawing.Point(87, 24); this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Size = new System.Drawing.Size(363, 280); this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; this.pictureBox1.TabIndex = 1; this.pictureBox1.TabStop = false; // // lbl1 // this.lbl1.AutoSize = true; this.lbl1.BackColor = System.Drawing.SystemColors.ControlLight; this.lbl1.Location = new System.Drawing.Point(385, 342); this.lbl1.Name = "lbl1"; this.lbl1.Size = new System.Drawing.Size(65, 12); this.lbl1.TabIndex = 2; this.lbl1.Text = "随机次数:0"; // // lbl2 // this.lbl2.AutoSize = true; this.lbl2.BackColor = System.Drawing.SystemColors.ControlLight; this.lbl2.Location = new System.Drawing.Point(268, 342); this.lbl2.Name = "lbl2"; this.lbl2.Size = new System.Drawing.Size(65, 12); this.lbl2.TabIndex = 3; this.lbl2.Text = "图片:1.jpg"; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.BackColor = System.Drawing.SystemColors.GradientActiveCaption; this.ClientSize = new System.Drawing.Size(582, 377); this.Controls.Add(this.lbl2); this.Controls.Add(this.lbl1); this.Controls.Add(this.pictureBox1); this.Controls.Add(this.btn1); this.Name = "Form1"; this.Text = "随机图片显示器"; this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); } #endregion int op=0; private System.Windows.Forms.Button btn1; private System.Windows.Forms.PictureBox pictureBox1; private System.Windows.Forms.Label lbl1; private System.Windows.Forms.Label lbl2; } }
program.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication2 { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }
第三步:运行结果及文件截图:
原文地址:http://lsywuya77.blog.51cto.com/9516729/1636647