

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 = "班級(jí)"; root.Controls.Add(tbe1);
TextBoxEx tbe2 = (TextBoxEx)host.CreateComponent(typeof(TextBoxEx), "age"); tbe2.LabelText = "年齡"; root.Controls.Add(tbe2);
}
}}
主要用到的技術(shù)知識(shí)點(diǎn):
DesignSurface 類(lèi)
參考
定義
命名空間:
System.ComponentModel.Design
程序集:
System.Windows.Forms.Design.dll
為設(shè)計(jì)組件提供一個(gè)用戶(hù)界面。
C#
public class DesignSurface : IDisposable, IServiceProvider
繼承
Object
DesignSurface
實(shí)現(xiàn)
IDisposable IServiceProvider
注解
類(lèi) DesignSurface 實(shí)現(xiàn)用戶(hù)視為設(shè)計(jì)器的內(nèi)容。DesignSurface 是用戶(hù)操作以更改設(shè)計(jì)時(shí)功能的用戶(hù)界面。DesignSurface 提供完全獨(dú)立的設(shè)計(jì)圖面。
類(lèi) DesignSurface 可以用作獨(dú)立設(shè)計(jì)器,也可以與 DesignSurfaceManager 類(lèi)結(jié)合使用,為托管多個(gè) DesignSurface 對(duì)象的應(yīng)用程序提供通用實(shí)現(xiàn)。
類(lèi) DesignSurface 可以單獨(dú)使用,或者用戶(hù)可以從中派生一個(gè)新類(lèi)并增強(qiáng)行為。
類(lèi) DesignSurface 自動(dòng)提供多個(gè)設(shè)計(jì)時(shí)服務(wù)。類(lèi) DesignSurface 在其構(gòu)造函數(shù)中添加其所有服務(wù)。這些服務(wù)中的大多數(shù)都可以通過(guò)在受保護(hù)的 ServiceContainer 屬性中替換它們來(lái)重寫(xiě)。若要替換服務(wù),請(qǐng)重寫(xiě)構(gòu)造函數(shù)、調(diào)用基,并通過(guò)受保護(hù)的 ServiceContainer 屬性進(jìn)行任何更改。在釋放設(shè)計(jì)圖面時(shí),將釋放添加到服務(wù)容器和實(shí)現(xiàn) IDisposable 的所有服務(wù)。下表顯示了類(lèi)提供的默認(rèn)可替換服務(wù) DesignSurface 集。
閱讀原文:https://mp.weixin.qq.com/s/NhlJtbw9i3gy-RqZnOEmuA
該文章在 2024/12/28 12:07:44 編輯過(guò)