-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
238 lines (207 loc) · 8.14 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
using System.ComponentModel;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Windows.Forms.Design;
using System.Windows.Forms;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Drawing.Design;
using NotImplementedException = System.NotImplementedException;
using Wew.Control;
using Wew.Media;
using Wew;
using System.Collections;
using System.Runtime.Serialization;
using System.Reflection;
namespace Demo;
public partial class Form1 : Form
{ readonly cDesignerHost sur;
readonly PropertyGrid pg;
readonly IToolboxService tbox; readonly toolitem toolit;
public Form1()
{
InitializeComponent();
sur = new cDesignerHost();
Splitter spl = new Splitter() { Dock = DockStyle.Right};
Controls.Add(spl);
pg = new PropertyGrid() { Dock = DockStyle.Right, PropertySort = PropertySort.Alphabetical, Width = 600
, Site = new sit(sur)
};
Controls.Add(pg);
ContainerControl cnt = new () { Dock = DockStyle.Top, AutoSize = true};
Button btn = new Button() { Text = "grab", Height = 30};
btn.Click += Grab_Click;
cnt.Controls.Add(btn);
btn = new Button() { Text = "tool", Height = 30, Left = 120};
btn.Click += tool_Click;
cnt.Controls.Add(btn);
Controls.Add(cnt);
tbox = new cToolBox();
sur.ToolBox = tbox;
toolit = new toolitem();
tbox.AddToolboxItem(toolit, null);
sur.SelectionService.SelectionChanged += SelSrv_SelectionChanged;
sur.ComponentChangeService.ComponentChanged += Form1_ComponentChanged;
sur.ComponentChangeService.ComponentRename += Form1_ComponentRename;
sur.ComponentChangeService.ComponentRemoved += Form1_ComponentRemoved;
WindowState = FormWindowState.Maximized;
sur.BeginLoad(new ldr());
Control vw = (Control)sur.View;
vw.Dock = DockStyle.Fill;
//vw.BackColor = Color.LightGray;
Controls.Add(vw); Controls.SetChildIndex(vw, 0);
ToolStrip ctl=new ToolStrip();
((Control)sur.DesignerHost.RootComponent).Controls.Add(ctl);
sur.DesignerHost.Container.Add(ctl);
ListView lv=new (){ View = View.Details, Location = new System.Drawing.Point(10, 30)};
_ = lv.Columns.Add("ww");
((Control)sur.DesignerHost.RootComponent).Controls.Add(lv);
sur.DesignerHost.Container.Add(lv);
}
private void Form1_ComponentRemoved(object? sender, ComponentEventArgs e)
{
}
private void Form1_ComponentRename(object? sender, ComponentRenameEventArgs e)
{
}
private void Form1_ComponentChanged(object? sender, ComponentChangedEventArgs e)
{
//pg.Refresh();
//System.Diagnostics.Debug.Print("chg");
}
private void Grab_Click(object? sender, System.EventArgs e)
{ DesignerSerializationManager ss = new DesignerSerializationManager(); _ = ss.CreateSession();
TypeCodeDomSerializer dd = new TypeCodeDomSerializer();
CodeTypeDeclaration cod = dd.Serialize(ss, sur.ComponentContainer.Components[0], null);
CodeNamespace cnNam = new ("Names"); _ = cnNam.Types.Add(cod);
CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
CodeGeneratorOptions options = new () { BracingStyle = "C", IndentString = "\t", BlankLinesBetweenMembers = false};
CodeCompileUnit targetUnit = new CodeCompileUnit(); _ = targetUnit.Namespaces.Add(cnNam);
using System.IO.StreamWriter sourceWriter = new ("e:\\dd.cs");
provider.GenerateCodeFromCompileUnit(targetUnit, sourceWriter, options);
}
private void tool_Click(object? sender, System.EventArgs e) { tbox.SetSelectedToolboxItem(toolit);}
private void SelSrv_SelectionChanged(object? sender, System.EventArgs e)
{ pg.SelectedObjects = ((ISelectionService)sender!).GetSelectedComponents().ToObjectArray();
}
class ldr : DesignerLoader
{ public override void BeginLoad(IDesignerLoaderHost host)
{ Control ctl, cnt;
Control raiz = (Control)host.CreateComponent(typeof(Form), "raíz");
//ctl = (Control)host.CreateComponent(typeof(ToolStrip), "ctl");
//InheritanceAttribute ia = (InheritanceAttribute)TypeDescriptor.GetAttributes(ctl)[typeof(InheritanceAttribute)];
//if (ia is null || ia.InheritanceLevel == InheritanceLevel.NotInherited)
//{
//}
ctl = (Control)host.CreateComponent(typeof(Button), "ctl2");
ctl.Location = new System.Drawing.Point(50, 50);
raiz.Controls.Add(ctl);
ctl = new Label() { Text="lbl", Location = new System.Drawing.Point(150, 50)};
raiz.Controls.Add(ctl);
cnt = (Control)host.CreateComponent(typeof(cnt), "ii");
cnt.Location = new System.Drawing.Point(50, 80);
raiz.Controls.Add(cnt);
ctl = new Label() { Text="lbl2"};
host.Container.Add(ctl);
ctl.Location = new System.Drawing.Point(50, 50);
cnt.Controls.Add(ctl);
cnt = (Control)host.CreateComponent(typeof(cnt), "cc");
cnt.Location = new System.Drawing.Point(10, 180);
raiz.Controls.Add(cnt);
_ = host.CreateComponent(typeof(cGrid), "grd");
// ** Fin
host.EndLoad(null!, true, null);
}
public override void Dispose() {}
}
class dsr : ParentControlDesigner
{
protected override void OnGiveFeedback(GiveFeedbackEventArgs e)
{
base.OnGiveFeedback(e);
}
protected override void OnDragEnter(DragEventArgs de)
{
base.OnDragEnter(de);
de.Effect = DragDropEffects.Copy;
}
protected override void OnDragOver(DragEventArgs de)
{
base.OnDragOver(de);
}
protected override void OnDragLeave(System.EventArgs e)
{
base.OnDragLeave(e);
}
protected override void OnMouseDragBegin(int x, int y)
{
base.OnMouseDragBegin(x, y);
}
protected override void OnMouseDragMove(int x, int y)
{
base.OnMouseDragMove(x, y);
}
}
class toolitem : ToolboxItem
{ public toolitem() : base(typeof(ToolStrip)) {}
protected override IComponent[] CreateComponentsCore(IDesignerHost host, IDictionary defaultValues)
{
return base.CreateComponentsCore(host, defaultValues);
}
protected override void Deserialize(SerializationInfo info, StreamingContext context)
{
base.Deserialize(info, context);
}
protected override object FilterPropertyValue(string propertyName, object value)
{
return base.FilterPropertyValue(propertyName, value);
}
protected override System.Type GetType(IDesignerHost host, AssemblyName assemblyName, string typeName, bool reference)
{
return base.GetType(host, assemblyName, typeName, reference);
}
public override void Initialize(System.Type type)
{
base.Initialize(type);
}
protected override void OnComponentsCreated(ToolboxComponentsCreatedEventArgs args)
{
base.OnComponentsCreated(args);
}
protected override void Serialize(SerializationInfo info, StreamingContext context)
{
base.Serialize(info, context);
}
protected override object ValidatePropertyValue(string propertyName, object value)
{
return base.ValidatePropertyValue(propertyName, value);
}
public override string Version => base.Version;
public override void Lock()
{
base.Lock();
}
}
[Designer(typeof(dsr))]
class cnt : GroupBox{}
private class sit : System.ComponentModel.ISite
{
readonly DesignSurface m_sur;
public sit(DesignSurface sur) { this.m_sur = sur;}
IComponent ISite.Component => throw new System.NotImplementedException();
IContainer? ISite.Container => null;
bool ISite.DesignMode => false;
string? ISite.Name { get => throw new System.NotImplementedException(); set => throw new System.NotImplementedException(); }
object? System.IServiceProvider.GetService(System.Type serviceType)
{
if (serviceType == typeof(IDesignerHost)) return m_sur.GetService(serviceType);
if (serviceType == typeof(System.Windows.Forms.AmbientProperties)) return null;
if (serviceType == typeof(IDesignerEventService)) return null;
if (serviceType == typeof(IHelpService)) return null;
if (serviceType == typeof(System.Windows.Forms.Design.IUIService)) return null;
if (serviceType == typeof(System.Drawing.Design.IPropertyValueUIService)) return null;
if (serviceType == typeof(IEventBindingService)) return null;
throw new System.NotImplementedException();
}
}
}