《C#程序设计》第二次作业:WinForm可视化设计

来源:互联网 发布:淘宝详情页模板下载 编辑:程序博客网 时间:2024/06/07 23:11

“随机图片显示器”开发

  1. 题目链接:题目
  2. 主要的实现方法:把图片资源保存在Resources.resx文件中,利用ResourceManager类变量获取图片资源,再设置PictureBox属性Image显示图片。具体实现在代码中注释体现:
  3. 具体实现代码:

PictureShow.cs
using System;
using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.IO;

using System.Linq;

using System.Reflection;

using System.Resources;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace pictureShow

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        int[] picture=new int[6];

        String strNum;

        //System.Resources.ResourceManager rm = new System.Resources.ResourceManager(typeof(Form1));

        ResourceManager rm = new ResourceManager("pictureShow.Properties.Resources", Assembly.GetExecutingAssembly());

        private void btnRandow_Click(object sender, EventArgs e)

        {

            show();

           

        }

        //实现了关闭窗体后把最新的计数写入文件并保存

        public void fileSava()

        {

            StreamWriter sr;

            if (File.Exists("D:\\numInfo.txt")) //如果文件存在,则创建File.AppendText对象

            {

                sr = File.AppendText("D:\\numInfo.txt");

            }

            else   //如果文件不存在,则创建File.CreateText对象

            {

                sr = File.CreateText("D:\\numInfo.txt");

            }

            sr.Close();

            //创建一个文件流,用以写入或者创建一个StreamWriter

            FileStream fs = new FileStream("D:\\numInfo.txt", FileMode.OpenOrCreate, FileAccess.Write);

            StreamWriter m_streamWriter = new StreamWriter(fs, Encoding.Default);

            m_streamWriter.Flush();

            // 使用StreamWriter来往文件中写入内容

            m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);

            for (int i = 0; i < picture.Length; i++)

            {

                m_streamWriter.WriteLine("picture{0}的点击次数为:{1}\n",i,picture[i]);

            }

            //关闭此文件

            m_streamWriter.Flush();

            m_streamWriter.Close();

        }

        //打开文件并读取文件里面各照片的显示次数

        public void fileOpen()

        {

            String fileContent = "";

            try{

                StreamReader read = new StreamReader("D:\\numInfo.txt", Encoding.Default);

                fileContent = read.ReadToEnd();

                read.Close();

               

            }

            catch (FileNotFoundException){

                for (int j = 0; j < picture.Length; j++)

                {

                   

                        picture[j] = 0;

                    

                }

            }

            //读取显示次数及把次数的String类型转化为int类型

            String[] fileArray = fileContent.Split('\n');

            String[] fileArrayTwo;

            

            for (int i = 0,n=0; i < picture.Length; i++,n+=2)

            {

                fileArrayTwo = fileArray[n].Split(':');

                String numString = fileArrayTwo[fileArrayTwo.Length - 1];

                try

                {

                    picture[i] = int.Parse(numString);

                   // nameLbl.Text = "";

                }

                catch (Exception e)

                {

                }

                    

            }

            

        }

        //清理文件以前的内容,以便记入新内容更新照片显示次数

        public void fileClear()

        {

            FileStream fs = new FileStream("D:\\numInfo.txt", FileMode.Open, FileAccess.Write);

            fs.SetLength(0);

            fs.Close();

        }

        private void show()

        {

            String pictureName = "picture";

            Random rd = new Random();

            int num = rd.Next(0, 6);

            picture[num] = picture[num] + 1;

            pictureName = pictureName + Convert.ToString(num);

            //Image imageResource = (Image)rm.GetObject("pictureName");//根据资源名称获取资源,并转型

            //picShow.Image = imageResource;

            var img = rm.GetObject(pictureName);//获取资源名为juhua的图片

            picShow.Image = img as Bitmap;//将图片显示在图片控件中

            strNum = Convert.ToString(picture[num]);

            lblNum.Text = strNum;

        }

        private void Form1_Load(object sender, EventArgs e)

        {

            fileOpen();

            picture[0] += 1;

            lblNum.Text = Convert.ToString(picture[0]);

            fileClear();

            

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)

        {

            fileSava();

        }

       

    }

}


       4. 参考链接:文件操作    获取资源方法

       5. 程序效果图

       







      6. 感恩互评作业的同学和老师!



0 0
原创粉丝点击