C# WInform可自动缩放的窗体布局(含子窗体)

1、使用TableLayoutPane对主窗体布局
在这里插入图片描述
2、创建子窗体,同样采用TableLayoutPanel布局
(1)、子窗体一的布局
在这里插入图片描述

public partial class SonForm : Form
    {
        public SonForm()
        {
            InitializeComponent();
            InitializeChart();
        }

        private void SonForm_Load(object sender, EventArgs e)
        {

            chart1.GetToolTipText += new EventHandler<ToolTipEventArgs>(chart1_GetToolTipText);
            LoadData2Chart(chart1, CurveDataGenerate());
            //DataTable dt1 = new DataTable();
            //dt1 = CreateDataTable();
            chart1.DataSource = dt;
            chart1.Series[0].XValueMember = "nation";
            chart1.Series[0].YValueMembers = "2020";
            chart1.Series[1].YValueMembers = "2021";
            chart1.DataBind();

            //chart1.Series.Clear();
            //Series series = new Series("dt1");
            //series.Points.DataBind(dt1.AsEnumerable(), "X", "Y", "");
            series.ChartType = SeriesChartType.Bar;
            chart1.ChartAreas[0].AxisX.Title = "国家";
            chart1.ChartAreas[0].AxisY.Title = "人均收入";
            //chart1.Series.Add(series);

            //try
            //{

            //    foreach (var item in chart1.Series)
            //    {

            //        for (int i = 0; i < 10; i++)
            //        {
            //            Random rd = new Random();
            //            item.Points.AddXY(i + 1, rd.Next(1, 10));
            //            System.Threading.Thread.Sleep(50);
            //        }
            //        if (item.Name.Equals("Series1")) item.ChartType = SeriesChartType.Spline;
            //        if (item.Name.Equals("Series2")) item.ChartType = SeriesChartType.Line;
            //    }
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.ToString());
            //}

            DataTable dt2 = GetDemoDataTableXY();
            chart2.Series.Clear();
            //chart2.Titles.Add("函数曲线");
            Series dataTableSeries = new Series("dt2");
            dataTableSeries.Points.DataBind(dt2.AsEnumerable(), "X", "Y", "");
            //dataTableSeries.XValueMember = "X";
            dataTableSeries.ChartType = SeriesChartType.Line;
            //chart2.ChartAreas[0].AxisX.Title = "X轴";
            //chart1.ChartAreas[0].AxisX.LabelStyle.Format = "yyyy/MM/DD HH:mm:ss";
            //chart2.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm:ss";
            //chart2.ChartAreas[0].AxisY.Title = "Y轴";
            chart2.Series.Add(dataTableSeries);
        }

        private DataTable CreateDataTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("X", typeof(string));
            dt.Columns.Add("Y", typeof(double));
            dt.Columns.Add("Z", typeof(double));

            //dt.Rows.Add("卢森堡", 46123);
            dt.Rows.Add("挪威", 32435,100);
            dt.Rows.Add("阿联酋", 31137,50);
            dt.Rows.Add("美国", 24921,1000);
            dt.Rows.Add("日本", 21814,300);

            //DataRow dr = dt.NewRow();
            //dr["nation"] = "卢森堡";
            //dr["y"] = 45123;
            dr["2021"] = 46123;

            //dr = dt.NewRow();
            //dr["nation"] = "挪威";
            //dr["y"] = 32435;
            dr["2021"] = 37435;

            //dr = dt.NewRow();
            //dr["nation"] = "阿联酋";
            //dr["y"] = 30435;
            dr["2021"] = 31137;

            //dr = dt.NewRow();
            //dr["nation"] = "美国";
            //dr["y"] = 24921;
            dr["2021"] = 24921;

            //dr = dt.NewRow();
            //dr["nation"] = "日本";
            //dr["y"] = 21814;
            dr["2021"] = 21814;

            return dt;
        }

        public DataTable GetDemoDataTableXY()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("X", typeof(double));
            dt.Columns.Add("Y", typeof(double));
            for (int i = 0; i < 6; i++)
            {
                dt.Rows.Add(i, 10 * Math.Sqrt(i * 0.2));
            }
            return dt;
        }
        public void InitializeChart()
        {
            #region 设置图表的属性
            //图表的背景色
            chart1.BackColor = Color.FromArgb(211, 223, 240);
            //图表背景色的渐变方式
            chart1.BackGradientStyle = GradientStyle.TopBottom;
            //图表的边框颜色、
            chart1.BorderlineColor = Color.FromArgb(26, 59, 105);
            //图表的边框线条样式
            chart1.BorderlineDashStyle = ChartDashStyle.Solid;
            //图表边框线条的宽度
            chart1.BorderlineWidth = 2;
            //图表边框的皮肤
            chart1.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
            #endregion

            #region 设置图表的Title
            Title title = new Title();
            //标题内容
            title.Text = "多曲线图演示";
            //标题的字体
            title.Font = new System.Drawing.Font("Microsoft Sans Serif", 12, FontStyle.Bold);
            //标题字体颜色
            title.ForeColor = Color.FromArgb(26, 59, 105);
            //标题阴影颜色
            title.ShadowColor = Color.FromArgb(32, 0, 0, 0);
            //标题阴影偏移量
            title.ShadowOffset = 3;

            chart1.Titles.Add(title);
            #endregion

            #region 设置图表区属性
            //图表区的名字
            ChartArea chartArea = new ChartArea("Default");
            //背景色
            chartArea.BackColor = Color.FromArgb(64, 165, 191, 228);
            //背景渐变方式
            chartArea.BackGradientStyle = GradientStyle.TopBottom;
            //渐变和阴影的辅助背景色
            chartArea.BackSecondaryColor = Color.White;
            //边框颜色
            chartArea.BorderColor = Color.FromArgb(64, 64, 64, 64);
            //阴影颜色
            chartArea.ShadowColor = Color.Transparent;

            //设置X轴和Y轴线条的颜色和宽度
            chartArea.AxisX.LineColor = Color.FromArgb(64, 64, 64, 64);
            chartArea.AxisX.LineWidth = 1;
            chartArea.AxisY.LineColor = Color.FromArgb(64, 64, 64, 64);
            chartArea.AxisY.LineWidth = 1;

            //设置X轴和Y轴的标题
            chartArea.AxisX.Title = "横坐标标题";
            chartArea.AxisY.Title = "纵坐标标题";

            //设置图表区网格横纵线条的颜色和宽度
            chartArea.AxisX.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);
            chartArea.AxisX.MajorGrid.LineWidth = 1;
            chartArea.AxisY.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);
            chartArea.AxisY.MajorGrid.LineWidth = 1;

            chart1.ChartAreas.Add(chartArea);
            #endregion

            #region 图例及图例的位置
            Legend legend = new Legend();
            legend.Alignment = StringAlignment.Center;
            legend.Docking = Docking.Bottom;

            this.chart1.Legends.Add(legend);
            #endregion
        }

        private void chart1_GetToolTipText(object sender, ToolTipEventArgs e)
        {
            if (e.HitTestResult.ChartElementType == ChartElementType.DataPoint)
            {
                int i = e.HitTestResult.PointIndex;
                DataPoint dp = e.HitTestResult.Series.Points[i];
                e.Text = string.Format("时间:{0};数值:{1:F1} ", DateTime.FromOADate(dp.XValue), dp.YValues[0]);
            }
        }

        private double[][] CurveDataGenerate()
        {
            double[][] d = new double[3][];
            //X轴数据
            d[0] = new double[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            //Y轴数据1
            d[1] = new double[10] { 0, 0.5, 1, 0.5, 0, -0.5, -1, -0.5, 0, 0.5 };
            //Y轴数据2
            d[2] = new double[10] { 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5 };
            return d;
        }

        //设置Series样式
        private Series SetSeriesStyle(int i)
        {
            Series series = new Series(string.Format("第{0}条数据", i + 1));

            //Series的类型
            series.ChartType = SeriesChartType.Line;
            //Series的边框颜色
            series.BorderColor = Color.FromArgb(180, 26, 59, 105);
            //线条宽度
            series.BorderWidth = 3;
            //线条阴影颜色
            series.ShadowColor = Color.Black;
            //阴影宽度
            series.ShadowOffset = 2;
            //是否显示数据说明
            series.IsVisibleInLegend = true;
            //线条上数据点上是否有数据显示
            series.IsValueShownAsLabel = false;
            //线条上的数据点标志类型
            series.MarkerStyle = MarkerStyle.Circle;
            //线条数据点的大小
            series.MarkerSize = 8;
            //线条颜色
            switch (i)
            {
                case 0:
                    series.Color = Color.FromArgb(220, 65, 140, 240);
                    break;
                case 1:
                    series.Color = Color.FromArgb(220, 224, 64, 10);
                    break;
                case 2:
                    series.Color = Color.FromArgb(220, 120, 150, 20);
                    break;
            }
            return series;
        }

        private void LoadData2Chart(Chart chart, double[][] data)
        {
            for (int i = 1; i < data.Length; i++)
            {
                Series series = this.SetSeriesStyle(i);
                for (int j = 0; j < data[i].Length; j++)
                {
                    series.Points.AddXY(data[0][j], 10*data[i][j]);
                }
                chart.Series.Add(series);
            }
        }
    }

(2)、子窗体二的布局
在这里插入图片描述

3、在主窗体中添加动态菜单,并将子窗体嵌入Pannel1中

public partial class MainForm : Form
    {
        //定义子窗体
        SonForm sonForm = new SonForm();
        SonForm1 sonForm1 = new SonForm();
        public MainForm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //动态添加菜单
            ToolStripMenuItem subItem;
            subItem = AddContextMenu("文件", menuStrip1.Items, null);
            AddContextMenu("打开", subItem.DropDownItems, new EventHandler(Menu_Clicked));
            AddContextMenu("关闭", subItem.DropDownItems, new EventHandler(Menu_Clicked));
            subItem = AddContextMenu("帮助", menuStrip1.Items, null);
            AddContextMenu("说明书", subItem.DropDownItems, new EventHandler(Menu_Clicked));
            AddContextMenu("关于", subItem.DropDownItems, new EventHandler(Menu_Clicked));
            //嵌入子窗体
            sonForm.Dock = System.Windows.Forms.DockStyle.Fill;
            sonForm.TopLevel = false;
            //sonForm.WindowState = FormWindowState.Maximized;
            sonForm.FormBorderStyle = FormBorderStyle.None;
            this.panel1.Controls.Add(sonForm);
            sonForm.Show();
            sonForm1.Dock = System.Windows.Forms.DockStyle.Fill;
            sonForm1.TopLevel = false;
            //sonForm.WindowState = FormWindowState.Maximized;
            sonForm1.FormBorderStyle = FormBorderStyle.None;
        }
        //菜单点击处理函数
        private void Menu_Clicked(object sender, EventArgs e)
        {
            string text = (sender as ToolStripMenuItem).Tag.ToString();
            MessageBox.Show(text);
        }
        //添加菜单处理函数
        ToolStripMenuItem AddContextMenu(string text, ToolStripItemCollection cms, EventHandler callback)
        {
            if (text == "-")
            {
                ToolStripSeparator tss = new ToolStripSeparator();
                cms.Add(tss);
                return null;
            }
            else if (!string.IsNullOrEmpty(text))
            {
                ToolStripMenuItem tsmi = new ToolStripMenuItem(text);
                tsmi.Tag = text;
                if (callback != null)
                {
                    tsmi.Click += callback;
                }
                cms.Add(tsmi);
                return tsmi;

            }
            return null;
        }
    }
    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex == 1)
            {
                panel1.Controls.Clear();
                this.panel1.Controls.Add(sonForm1);
                sonForm1.Show();
            }
            else
            {
                panel1.Controls.Clear();
                this.panel1.Controls.Add(sonForm);
                sonForm.Show();
            }
        }

4、运行程序效果
在这里插入图片描述
在这里插入图片描述

5、放大效果
在这里插入图片描述
在这里插入图片描述

  • 2
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C#的Chart控件是微软自带的一个图形可视化组件,可以在Web程序和窗体程序中使用。在.NET4.0之后的版本中,Chart控件已经集成在Visual Studio中,不需要手动安装。你可以在工具箱中找到"数据"控件,里面就有一个Chart控件,你可以直接拖动到窗体中使用。默认情况下,会创建一个名为chart1的实例对象。\[1\] 如果你想了解更多关于Winform控件的介绍和示例,你可以参考一些网址,比如: - https://docs.devexpress.com/WindowsForms/17530/controls-and-libraries/spreadsheet/examples/charts/how-to-show-or-hide-the-chart-legend - https://blog.csdn.net/zhouyingge1104/article/details/105504889 - https://blog.csdn.net/quanlver/article/details/83035998 - https://blog.csdn.net/qq_27825451/article/details/81305387 - https://www.cnblogs.com/arxive/p/5861960 - https://blog.csdn.net/xianfajushi/article/details/7493697 在使用Chart控件时,你可以使用一些方法来绑定数据,比如: - DataBind方法可以将数据源绑定到Chart控件的Series上,可以使用不同的参数组合来绑定不同的数据。 - DataBindXY方法可以将一组X和Y的值绑定到一个Series上。 - DataBindY方法可以将一组Y的值绑定到一个Series上。 例如,你可以使用以下代码将一组X和Y的值绑定到名为"Series1"的Series上: chart1.Series\["Series1"\].Points.DataBindXY(x, y)。\[3\] 希望这些信息对你有帮助! #### 引用[.reference_title] - *1* *3* [c# chart图表控件总结](https://blog.csdn.net/a843538946/article/details/103048471)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [C# chart控件使用方法](https://blog.csdn.net/u012719076/article/details/108192562)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大浪淘沙胡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值