C#客户端WinForm实现Form表单格式自定义
|
admin
2024年12月27日 21:37
本文热度 1838
|


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(); }
DesignSurface surface; IDesignerHost host; Control root;
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); } }; }
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);
}
}}
主要用到的技术知识点:
DesignSurface 类
参考
定义
命名空间:
System.ComponentModel.Design
程序集:
System.Windows.Forms.Design.dll
为设计组件提供一个用户界面。
C#
public class DesignSurface : IDisposable, IServiceProvider
继承
Object
DesignSurface
实现
IDisposable IServiceProvider
注解
类 DesignSurface 实现用户视为设计器的内容。DesignSurface 是用户操作以更改设计时功能的用户界面。DesignSurface 提供完全独立的设计图面。
类 DesignSurface 可以用作独立设计器,也可以与 DesignSurfaceManager 类结合使用,为托管多个 DesignSurface 对象的应用程序提供通用实现。
类 DesignSurface 可以单独使用,或者用户可以从中派生一个新类并增强行为。
类 DesignSurface 自动提供多个设计时服务。类 DesignSurface 在其构造函数中添加其所有服务。这些服务中的大多数都可以通过在受保护的 ServiceContainer 属性中替换它们来重写。若要替换服务,请重写构造函数、调用基,并通过受保护的 ServiceContainer 属性进行任何更改。在释放设计图面时,将释放添加到服务容器和实现 IDisposable 的所有服务。下表显示了类提供的默认可替换服务 DesignSurface 集。
阅读原文:https://mp.weixin.qq.com/s/NhlJtbw9i3gy-RqZnOEmuA
该文章在 2024/12/28 12:07:44 编辑过