tangxu
2024-10-22 4d9fe5ed98ceb6b8fe9dc52ebfb80860ad1aee99
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
using Microsoft.Windows.Forms;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Windows.Forms.Design.Behavior;
 
namespace DPumpHydr.WinFrmUI.WenSkin.Design.Designer
{
    public class UiControlsDesigner : ControlDesigner
    {
        public UiControlsDesigner() : base()
        {
 
        }
 
        //获取当前选择的子控件
        #region 私有属性
        public IUIControl uIControl => this.Component as IUIControl;
        private IUIControl selectUiControl;
 
        #region 鼠标
 
        private bool isMouseDown = false;
        private Point oldPoint;
        #endregion
 
        #endregion
 
        public static int LoWord(int dwValue) => dwValue & 0xFFFF;
        public static int HiWord(int dwValue) => (dwValue >> 16) & 0xFFFF;
 
 
 
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            switch (m.Msg)
            {
                case 0x203:
                    Point pi = new Point(LoWord((int)m.LParam), HiWord((int)m.LParam));
                    var c = selectUiControl = FindSelectedControl(pi);
                    if (c == null)
                        return;
                    CreateEventCode();
                    break;
                    //case 0x200:
                    //    pi = new Point(LoWord((int)m.LParam), HiWord((int)m.LParam));
                    //    if (isMouseDown && selectUiControl != null)
                    //    {
                    //        selectUiControl.Location = new Point(selectUiControl.Location.X + pi.X - oldPoint.X, selectUiControl.Location.Y + pi.Y - oldPoint.Y);
                    //        oldPoint = pi;
                    //        HookChildControls(this.Control);
                    //    }
                    //    break;
            }
        }
 
        private UIControl FindSelectedControl(Point pi)
        {
            if (uIControl != null)
                foreach (var item in uIControl.UIControls)
                {
                    RectangleF rec = new RectangleF(item.Location, item.Size);
                    if (rec.Contains(pi))
                    {
                        return item;
                    }
                }
            return null;
        }
        private DesignerActionListCollection actionLists;
        public override DesignerActionListCollection ActionLists
        {
            get
            {
                if (actionLists == null)
                {
                    actionLists = new DesignerActionListCollection();
                    this.ActionLists.Add(new WenDesignerActionList(this.Component));
                }
                return actionLists;
            }
        }
        public class WenDesignerActionList : DesignerActionList
        {
            public WenDesignerActionList(IComponent component) : base(component)
            {
            }
 
            public IUIControl uIControl => this.Component as IUIControl;
 
 
            [Editor(typeof(DPumpHydr.WinFrmUI.WenSkin.Design.Editor.DuiWenCollectionEditor), typeof(System.Drawing.Design.UITypeEditor))]
            public UIControl.UIControlCollection UIControls => uIControl.UIControls;
 
 
 
            public override DesignerActionItemCollection GetSortedActionItems()
            {
                DesignerActionPropertyItem uIControls = new DesignerActionPropertyItem("UIControls", "子控件集合");
                return new DesignerActionItemCollection() { uIControls };
            }
        }
 
        //当响应在组件上按住鼠标左键不放这一操作时接收调用。
        protected override void OnMouseDragBegin(int x, int y)
        {
            var pi = this.Control.PointToClient(new Point(x, y));
            var c = selectUiControl = FindSelectedControl(pi);
            if (c != null)
            {
                isMouseDown = true;
                oldPoint = pi;
            }
            else
                base.OnMouseDragBegin(x, y);
        }
        //在拖放操作结束时接收调用,以完成或取消此次操作。
        protected override void OnMouseDragEnd(bool cancel)
        {
            isMouseDown = false;
            if (selectUiControl != null)
            {
                this.Control.Invalidate();
                selectUiControl = null;
            }
            base.OnMouseDragEnd(cancel);
        }
        //为拖放操作期间的每一次鼠标移动接收调用。
        protected override void OnMouseDragMove(int x, int y)
        {
            var pi = this.Control.PointToClient(new Point(x, y));
            if (isMouseDown && selectUiControl != null)
            {
                selectUiControl.Location = new Point(selectUiControl.Location.X + pi.X - oldPoint.X, selectUiControl.Location.Y + pi.Y - oldPoint.Y);
                oldPoint = pi;
                UpdateCode(selectUiControl as IComponent);
            }
            else
                base.OnMouseDragMove(x, y);
        }
 
 
        //重绘 选中的时候外框虚线框
        protected override void OnPaintAdornments(PaintEventArgs pe)
        {
            base.OnPaintAdornments(pe);
            if (selectUiControl != null)
            {
                using var pen = new Pen(Color.FromArgb(106, 106, 106)) { DashStyle = System.Drawing.Drawing2D.DashStyle.Dash };
                pe.Graphics.DrawRectangle(pen, selectUiControl.Location.X - 2, selectUiControl.Location.Y - 2, selectUiControl.Width + 4, selectUiControl.Height + 4);
            }
        }
 
        private void UpdateCode(IComponent com)
        {
            if (TryGetService(out IComponentChangeService service) && com != null)
            {
                service.OnComponentChanged(com, null, null, null);
            }
        }
 
        internal bool TryGetService<T>(out T service) where T : class
        {
            service = GetService<T>();
            return service != null;
        }
        internal T GetService<T>() where T : class => GetService(typeof(T)) as T;
 
        public override void DoDefaultAction()
        {
        }
 
 
        private void CreateEventCode()
        {
            if (!TryGetService(out IEventBindingService ebs))
            {
                return;
            }
 
            var ss = TypeDescriptor.GetEvents(selectUiControl);
 
            var eve = ss.Find("Click", true);
            if (eve == null) return;
 
            CreateEventCode(ebs, selectUiControl as IComponent, eve);
            ebs.ShowCode(selectUiControl as IComponent, eve);
        }
 
        private void CreateEventCode(IEventBindingService ebs, IComponent comp, EventDescriptor eventDescriptor)
        {
            if (ebs == null || comp == null || eventDescriptor == null)
                return;
 
 
            //如果找不到此事件的属性,或者该属性的属性为只读,则中止。
            if (!(ebs.GetEventProperty(eventDescriptor) is PropertyDescriptor propEvent) || propEvent.IsReadOnly)
                return;
 
 
            //如果解析的init方法中没有显式事件连接,则处理程序将为null
            object result = propEvent.GetValue(comp);
            string handler = result as string;
 
            bool eventChanged;
            if (handler is null)
            {
                //跳过无效属性。
                if (result != null)
                {
                    return;
                }
                eventChanged = true;
                handler = ebs.CreateUniqueMethodName(comp, eventDescriptor);
            }
            else
            {
                //确保处理程序仍在那里
                eventChanged = true;
                ICollection methods = ebs.GetCompatibleMethods(eventDescriptor);
                if (methods != null)
                {
                    foreach (string compatibleMethod in methods.OfType<string>())
                    {
                        if (handler == compatibleMethod)
                        {
                            eventChanged = false;
                            break;
                        }
                    }
                }
            }
            //生成持久事件代码
            //保存新值。。。在导航到它之前!
            if (eventChanged)
            {
                propEvent.SetValue(comp, handler);
            }
 
        }
 
 
        protected override void PreFilterEvents(IDictionary events)
        {
            base.PreFilterEvents(events);
        }
 
 
        protected override void PreFilterProperties(System.Collections.IDictionary properties)
        {
            base.PreFilterProperties(properties);
        }
 
    }
}