schangxiang@126.com
2025-11-04 f5ed29dc26c7cd952d56ec5721a2efc43cd25992
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using XCommon.Log;
using XImagingXhandler.XDAL;
 
namespace XHandler.View.Device
{
    /// <summary>
    /// 设备状态共用页面
    /// </summary>
    public partial class DeviceStatusCom : UserControl
    {
        #region 设备参数信息
        public class DeviceStatusInfo
        {
            /// <summary>
            /// 设备ID
            /// </summary>
            public string DeviceId { get; set; }
 
            /// <summary>
            /// 设备连接状态
            /// </summary>
            public bool Status { get; set; }
 
            /// <summary>
            /// 设备连接状态文言
            /// </summary>
            public string StatusTitle { get; set; }
 
            /// <summary>
            /// 鼠标提示文言
            /// </summary>
            public string ToolTip { get; set; }
 
            /// <summary>
            /// DeviceLeft(X)
            /// </summary>
            public double DeviceLeft { get; set; }
 
            /// <summary>
            /// DeviceTop(Y)
            /// </summary>
            public double DeviceTop { get; set; }
        }
        #endregion
 
        #region 变量
        private Element m_current = new Element();
 
        /// <summary>
        /// 设备参数信息
        /// </summary>
        private DeviceStatusInfo m_statusInfo = null;
 
 
        public event EventHandler MouseUp;
        #endregion
 
        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="status"></param>
        /// <param name="statusTitle"></param>
        /// <param name="toolTip"></param>
        public DeviceStatusCom(DeviceStatusInfo statusInfo)
        {
            InitializeComponent();
            m_statusInfo = statusInfo;
        }
        #endregion
 
        #region 界面初期表示事件
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                // 提示文言
                canvas.ToolTip = m_statusInfo.ToolTip;
                // 状态图片
                btnStatus.NormalImage = (ImageSource)new ImageSourceConverter().ConvertFrom(new Uri(m_statusInfo.Status ?
                    "pack://application:,,,./Assets/RunStatus/GreenState.jpg" : "pack://application:,,,./Assets/RunStatus/GreyState.jpg"));
                // 状态文言
                tBlockStatusTitle.Text = m_statusInfo.StatusTitle;
 
                #region 设置位置
                var transformGroup = ((UIElement)border).RenderTransform as TransformGroup;
                if (transformGroup != null)
                {
                    var translateTransforms = from transform in transformGroup.Children
                                              where transform.GetType().Name == "TranslateTransform"
                                              select transform;
 
                    foreach (TranslateTransform tt in translateTransforms)
                    {
                        tt.X = m_statusInfo.DeviceLeft;
                        tt.Y = m_statusInfo.DeviceTop;
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                LoggerHelper.ErrorLog("ERROR:", ex);
            }
        }
        #endregion
 
        #region 拖拽相关
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            //LoggerHelper.DebugLog("=============== OnMouseLeftButtonDown");
            base.OnMouseLeftButtonDown(e);
            this.m_current.X = Mouse.GetPosition(this.canvas).X;
            this.m_current.Y = Mouse.GetPosition(this.canvas).Y;
 
            if (this.m_current.InputElement != null)
            {
                this.m_current.InputElement.CaptureMouse();
            }
 
            if (!this.m_current.IsStretching)
            {
                this.m_current.IsDragging = true;
            }
        }
 
        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            //LoggerHelper.DebugLog("=============== OnMouseLeftButtonUp");
            base.OnMouseLeftButtonUp(e);
 
            if (this.m_current.InputElement != null)
            {
                this.m_current.InputElement.ReleaseMouseCapture();
            }
 
            var newX = Mouse.GetPosition((IInputElement)canvas).X;
            var newY = Mouse.GetPosition((IInputElement)canvas).Y;
            LoggerHelper.DebugLog(string.Format("=============== [OnMouseLeftButtonUp]:newX = {0}, newY = {1}", newX, newY));
 
            #region 获取拖动坐标
            var transformGroup = ((UIElement)border).RenderTransform as TransformGroup;
            if (transformGroup != null)
            {
                var translateTransforms = from transform in transformGroup.Children
                                          where transform.GetType().Name == "TranslateTransform"
                                          select transform;
 
                var ttFirst = translateTransforms.FirstOrDefault();
                if (ttFirst != null)
                {
                    TranslateTransform tt = (TranslateTransform)ttFirst;
                    m_statusInfo.DeviceLeft = tt.X;
                    m_statusInfo.DeviceTop = tt.Y;
 
                    MouseUp?.Invoke(m_statusInfo, EventArgs.Empty);
                }
            }
            #endregion
 
 
            this.Cursor = Cursors.Arrow;
        }
 
        private void Canvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed &&
                 m_current.InputElement != null)
            {
                //LoggerHelper.DebugLog("=============== Canvas_MouseMove     1");
 
                //递增z-Order并将其传递给当前元素,以使其停留在所有其他元素的顶部
                ((Border)this.m_current.InputElement).SetValue(Canvas.ZIndexProperty, this.m_current.ZIndex++);
 
                //判断操作类型为拖拽移动
                if (this.m_current.IsDragging)
                {
                    Drag(sender);
                }
            }
        }
 
        private void border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //LoggerHelper.DebugLog("=============== border_MouseLeftButtonDown");
            //capture the last highest z index before pointing to new current element
            //在指向新的当前元素之前捕获最后的最高z索引
            int newZIndex = (int)((Border)sender).GetValue(Canvas.ZIndexProperty);
            this.m_current.ZIndex = newZIndex > this.m_current.ZIndex ? newZIndex : this.m_current.ZIndex;
 
            //capture the new current element
            //捕获新的当前元素
            this.m_current.InputElement = (IInputElement)sender;
        }
 
        private void border_MouseLeave(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                return;
            }
 
            //LoggerHelper.DebugLog("=============== border_MouseLeave");
            // 获得坐标、计算Border
            Border border = (Border)sender;
            var rightLimit = border.ActualWidth - border.Padding.Right;
            var bottomLimit = border.ActualHeight - border.Padding.Bottom;
            //获得鼠标点击(与特定元素的相对位置)
            var x = Mouse.GetPosition((IInputElement)sender).X;
            var y = Mouse.GetPosition((IInputElement)sender).Y;
 
            // figure out stretching directions - only to Right, Bottom 
            //找出缩放的方向-仅向右方、底部
            bool stretchRight = (x >= rightLimit && x < border.ActualWidth) ? true : false;
            bool stretchBottom = (y >= bottomLimit && y < border.ActualHeight) ? true : false;
 
            // update current element
            //更新当前元素
            this.m_current.InputElement = (IInputElement)sender;
            this.m_current.X = x;
            this.m_current.Y = y;
            //默认为“缩放”操作
            this.m_current.IsStretching = true;
 
            //set cursor to show stretch direction 
            //设置光标显示
            //右&底部,光标显示右下箭头
            if (stretchRight && stretchBottom)
            {
                this.Cursor = Cursors.SizeNWSE;
                return;
            }
            //右
            else if (stretchRight && !stretchBottom)
            {
                this.Cursor = Cursors.SizeWE;
                return;
            }
            //底部
            else if (stretchBottom && !stretchRight)
            {
                this.Cursor = Cursors.SizeNS;
                return;
            }
            //非缩放操作,显示箭头光标,设置IsStretching=false
            else //no stretch
            {
                this.Cursor = Cursors.Arrow;
                this.m_current.IsStretching = false;
            }
 
        }
 
        private void border_MouseEnter(object sender, MouseEventArgs e)
        {
            //LoggerHelper.DebugLog("=============== border_MouseEnter        2");
            Border border = (Border)sender;
 
            var rightLimit = border.ActualWidth - border.Padding.Right;
            var bottomLimit = border.ActualHeight - border.Padding.Bottom;
 
            var x = Mouse.GetPosition((IInputElement)sender).X;
            var y = Mouse.GetPosition((IInputElement)sender).Y;
 
            if (x < rightLimit && y < bottomLimit)
                this.Cursor = Cursors.Arrow;
        }
 
        /// <summary>
        /// 拖拽移动
        /// </summary>
        /// <param name="sender"></param>
        private void Drag(object sender)
        {
            //LoggerHelper.DebugLog("=============== border_MouseEnter");
 
            this.Cursor = Cursors.Hand;
 
            // Retrieve the current position of the mouse.
            var newX = Mouse.GetPosition((IInputElement)sender).X;
            var newY = Mouse.GetPosition((IInputElement)sender).Y;
 
            // Reset the location of the object (add to sender's renderTransform newPosition minus currentElement's position
            var transformGroup = ((UIElement)this.m_current.InputElement).RenderTransform as TransformGroup;
            if (transformGroup == null)
            {
                return;
            }
 
            var translateTransforms = from transform in transformGroup.Children
                                      where transform.GetType().Name == "TranslateTransform"
                                      select transform;
 
            foreach (TranslateTransform tt in translateTransforms)
            {
                tt.X += newX - m_current.X;
                tt.Y += newY - m_current.Y;
 
                LoggerHelper.DebugLog(string.Format("===============!!!!! [Drag]:tt.X = {0}, tt.Y = {1}", tt.X, tt.Y));
            }
 
            // Update the beginning position of the mouse
            m_current.X = newX;
            m_current.Y = newY;
 
            LoggerHelper.DebugLog(string.Format("=============== [Drag]:newX = {0}, newY = {1}", newX, newY));
        }
        #endregion
    }
}