C#多窗口切换的实现

本文关键字:

多窗口切换 label splitContainer 窗口背景颜色设置 字体设置 窗口布局 按钮事件 按钮

新建项目:

开发MainForm:

MainForm先添加1个splitContainer,然后splitContainer.Panel1添加3个按钮,分别是button1,button2,button3
小技巧
这里设置splitContainer的左侧panel1固定大小,
splitContainer1.IsSplitterFixed = false;
// IsSpliterFixed属性设为False
splitContainer1.FixedPanel = FixedPanel.Panel1;
//FixedPannel属性设为Pannel1(要固定的面板的名称)

再右键项目名称添加一个Form1:



Form1设计界面添加2个label控件,分别命名为lbTitle,lbContent;
小技巧
控件设置为文本居中,大小固定;


在Form1的构造函数修改为传入3个参数,分别为Title,Conten和背景色color
Form1的完整代码:

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 多窗口切换的实现
{
    public partial class Form1 : Form
    {
        public Form1(string title,string content ,Color color) {
            InitializeComponent( );
            lbTitle.Text = title;
            lbContent.Text = content;
            this.BackColor = color;

            lbTitle.Font=new Font("仿宋", 20, FontStyle.Regular); //第一个是字体,第二个大小,第三个是样式
            lbContent.Font = new Font("仿宋", 20, FontStyle.Regular); //第一个是字体,第二个大小,第三个是样式
        }
    }
}

MainForm的完整代码:

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 多窗口切换的实现
{
    public partial class MainForm : Form
    {
        //在主窗体初始化3个Form1窗体
        public Form f1;
        public Form f2;
        public Form f3;
        public MainForm( ) {
            InitializeComponent( );
            splitContainer1.IsSplitterFixed = false;
            //            IsSpliterFixed属性设为False
            splitContainer1.FixedPanel = FixedPanel.Panel1;
//FixedPannel属性设为Pannel1(要固定的面板的名称)
        }

        private void MainForm_Load( object sender, EventArgs e ) {
            string title = "窗口一";
            string content = "北国风光,千里冰封。";
            Color color = Color.FromArgb(255, 192, 255); //此方法设置的颜色,其透明度属性alpha=255,完全不透明。 
            f1 = new Form1(title, content,color);
            f1.Dock = System.Windows.Forms.DockStyle.Fill;
            f1.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            f1.TopLevel = false;
            title = "窗口二";
            content = "成吉思汗,只识弯弓射大雕。";
            color = Color.FromArgb(205, 255, 205); //此方法设置的颜色,其透明度属性alpha=255,完全不透明。 
            f2 = new Form1(title, content,color);
            f2.Dock = System.Windows.Forms.DockStyle.Fill;
            f2.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            f2.TopLevel = false;
            title = "窗口三";
            content = "俱往矣,数风流人物还看今朝。";
            color = Color.FromArgb(155, 152, 255); //此方法设置的颜色,其透明度属性alpha=255,完全不透明。 
            f3 = new Form1(title, content,color);
            f3.Dock = System.Windows.Forms.DockStyle.Fill;
            f3.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            f3.TopLevel = false;
//————————————————

//                            版权声明:本文为博主原创文章,遵循 CC 4.0 BY - SA 版权协议,转载请附上原文出处链接和本声明。
                        
//原文链接:https://blog.csdn.net/qq_27474555/article/details/127688352
        }

        private void button1_Click( object sender, EventArgs e ) {
            f1.Show( );
            splitContainer1.Panel2.Controls.Clear( );
            splitContainer1.Panel2.Controls.Add(f1);
        }

        private void button2_Click( object sender, EventArgs e ) {
            f2.Show( );
            splitContainer1.Panel2.Controls.Clear( );
            splitContainer1.Panel2.Controls.Add(f2);
        }

        private void button3_Click( object sender, EventArgs e ) {
            f3.Show( );
            splitContainer1.Panel2.Controls.Clear( );
            splitContainer1.Panel2.Controls.Add(f3);
        }
    }
}

小技巧

  • form1在MainForm初始化时变实例化3个窗口,为后续使用;

整个解决方案的结构:

最终实现的效果:

参考文档:
https://blog.csdn.net/qq_27474555/article/details/127688352
https://blog.csdn.net/xieyunc/article/details/89193393
https://blog.csdn.net/wojiuguowei/article/details/110927004

posted @ 2024-01-30 10:56  Stephen_Young  阅读(501)  评论(0编辑  收藏  举报