C# Winform學習---MDI窗體的設計,PictureBox控件(圖片上一頁下一頁),Timer控件,MenuStrip控件

1、MDI窗體的設計git

1.MDI簡介github

MDI(Multiple Document Interface)就是所謂的多文檔界面,與此對應就有單文檔界面 (SDI), 它是微軟公司從Windows 2.0下的Microsoft Excel電子表格程序開始引入的,Excel電子表格用戶有時須要同時操做多份表格,MDI正好爲這種操做多表格提供了很大的方便,因而就產生了MDI程序後端

2.效果圖:dom

以下圖所示,多窗體嵌套,其中一個是父窗體,其條是子窗體。佈局

 

橫向排列下面的窗體:學習

 

縱向排列下面的窗體:this

 

關閉所有子窗體:spa

 

3.實現過程設計

1)、新建一個window窗體程序3d

2)新建4個form

 

3)、後端實現代碼

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 mdi設計
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void 顯示子窗體ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form f2 = new Form2(); f2.MdiParent = this;//指定其mdi父窗體
 f2.Show();
            
            
            Form f3 = new Form3();
            f3.MdiParent = this;
            f3.Show();

            Form f4 = new Form4();
            f4.Show();
            f4.MdiParent = this;


        }

        private void 橫向排列ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void 縱向排列ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void 關閉所有窗體ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form f in this.MdiChildren)
            {
                f.Close();
            }

        }
    }
}

 

所有源碼實現:https://github.com/amosli/CSharp/tree/mdi

 

其中菜單選項用的是MenuStrip控件,其使用也是很是簡單方便的,以下所示:

 

2、MenuStrip控件

新建一個MenuStrip控件,而後寫入名稱便可,雙擊能夠定義其事件,如上面的橫向排列和縱向排列。

 

 

 

3、PictureBox控件和Timer控件

1.先看效果圖:

 

 

 

2.實現過程

新建PictureBox和timer的控件

 

爲了實現PictureBox控件的相對大小,這裏要注意更改Anchor佈局爲所有勾選!

 

設置圖片顯示的格式:

 

 

 

【timer控件】間隔時間設置:

間隔執行的事件:

 

3.後臺實現代碼

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PictureBox控件學習
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// timer更改時間
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            lbTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }

        public static String ImgDirPath = @"C:\work\img";
        String[] imgPaths = Directory.GetFiles(ImgDirPath, "*.jpg");
        public static int ImgNum = 0;
        /// <summary>
        /// 實現上一頁按鈕
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPrevious_Click(object sender, EventArgs e)
        {
            ImgNum--;
            if (ImgNum < 0)
            {
                ImgNum = imgPaths.Length-1;
            }
            pboGirls.ImageLocation = imgPaths[ImgNum];

        }

        /// <summary>
        /// 實現下一頁按鈕
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNext_Click(object sender, EventArgs e)
        {
            ImgNum++;
            if (ImgNum >= imgPaths.Length)
            {
                ImgNum = 0;
            }
            pboGirls.ImageLocation = imgPaths[ImgNum];
        }

        /// <summary>
        /// 隨機瀏覽
        /// </summary>
        Random rnd = new Random();
        private void button1_Click(object sender, EventArgs e)
        {
            pboGirls.ImageLocation = imgPaths[rnd.Next(imgPaths.Length)];
        }
    }
}

 

所有源碼:https://github.com/amosli/CSharp/tree/picturebox

相關文章
相關標籤/搜索