客户端WinForm实现Form表单格式自定义

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.Design.WebControls;
using System.Windows.Forms;
using YUTU.UIL.Common.Controls;

namespace WindowsFormsApp1
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            initDesign();
            loadVisibleControl();
        }

        /// <summary>
        /// 定义
        /// </summary>
        DesignSurface surface;
        IDesignerHost host;
        Control root;
        //List<Control> selectedControls = new List<Control>();


        /// <summary>
        /// 设计窗体
        /// </summary>
        private void initDesign()
        {
            surface = new DesignSurface();
            surface.BeginLoad(typeof(UserControl));

            Control designView = surface.View as Control;
            panel1.Controls.Add(designView);
            designView.Dock = DockStyle.Fill;
            designView.Visible = true;


            host = (IDesignerHost)surface.GetService(typeof(IDesignerHost));
            root = (Control)host.RootComponent;
            root.Dock = DockStyle.Fill;


            //控制移动位置
            ISelectionService selectionService = host.GetService(typeof(ISelectionService)) as ISelectionService;
            selectionService.SelectionChanged += delegate
            {
                try
                {
                    ICollection collection = selectionService.GetSelectedComponents();
                    if (collection.Count > 0)
                    {
                        foreach (Control item in collection)
                        {
                            label1.Text = "X:" + item.Location.X.ToString();
                            label2.Text = "Y:" + item.Location.Y.ToString();
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            };
        }

        /// <summary>
        /// 加载控件
        /// </summary>
        public void loadVisibleControl()
        {
            //可以加载不同类型的控件

            TextBoxEx tbe = (TextBoxEx)host.CreateComponent(typeof(TextBoxEx), "name");
            tbe.LabelText = "姓名";
            root.Controls.Add(tbe);

            TextBoxEx tbe1 = (TextBoxEx)host.CreateComponent(typeof(TextBoxEx), "class");
            tbe1.LabelText = "班级";
            root.Controls.Add(tbe1);

            TextBoxEx tbe2 = (TextBoxEx)host.CreateComponent(typeof(TextBoxEx), "age");
            tbe2.LabelText = "年龄";
            root.Controls.Add(tbe2);


            //ComboBoxEx cbe = (ComboBoxEx)host.CreateComponent(typeof(ComboBoxEx), "bbb");
            //root.Controls.Add(cbe);

            //TextBox tb = (TextBox)host.CreateComponent(typeof(TextBox), "ccc");
            //root.Controls.Add(tb);
        }

    }
}

  

 

posted @ 2024-11-29 14:12  上位机李工  阅读(34)  评论(0)    收藏  举报