1
yhj
2024-07-24 5e5d945e91568b973faa27d8ab0bcef99fc4a6c5
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
#region
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using CSFrameworkV5.Business;
using CSFrameworkV5.Common;
using CSFrameworkV5.Core;
using CSFrameworkV5.Interfaces;
using CSFrameworkV5.Library;
using CSFrameworkV5.Library.CommonClass;
using CSFrameworkV5.Models;
using CSFrameworkV5.UserCustom.Properties;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
 
#endregion
 
namespace CSFrameworkV5.UserCustom
{
    public partial class frmUserCustomMain : frmBaseModule
    {
        //按钮名称前缀
        private const string BTN_PREFIX = "_SHORTCUT_";
 
        private const int BTN_WIDTH = 138; //按钮宽度
        private const int BTN_HEIGHT = 69; //按钮高度
        private const int BTN_MARGIN = 10; //按钮之间的间隔
        private bllUserCustomAction _bllUserCustomAction;
 
        private GZTable _ButtonTable;
        private DataTable _dtCustomAction;
        private GZLockMouse.Rect _MouseRect;
 
        private Dictionary<string, SimpleButton> _ShortCutButtons =
            new Dictionary<string, SimpleButton>();
 
        private Point _StartPoint;
        private int AddWidth = 20;
        private BorderStyles BorderStyleBak;
        private SimpleButton ButtonBak;
        private GZCell CellFrom;
        private GZCell CellTo;
        private List<GZCell> lstEmptyCell = new List<GZCell>();
 
        private bool Manage;
 
        public frmUserCustomMain()
        {
            InitializeComponent();
 
            _bllUserCustomAction = new bllUserCustomAction();
            _ModuleID = (int)Interfaces.ModuleID.UserCustom; //设置模块编号
            _ModuleName = ModuleNames.UserCustom; //设置模块名称
 
            menuUserCustomMain.Text =
                ModuleNames.UserCustom; //与AssemblyModuleEntry.ModuleName定义相同
            menuUserCustomMain.Tag = new MenuItemTag(MenuType.ItemOwner, 0, 0);
 
            MainMenuStrip = menuStrip1;
 
            //创建表格实例并初始化5行
            _ButtonTable = GZTableClass.GetGZTable(BTN_WIDTH, BTN_HEIGHT,
                bllUserCustomAction.BTN_COLS);
            for (var i = 0; i < bllUserCustomAction.BTN_ROWS; i++)
                _ButtonTable.AddRow();
 
            //动态加载菜单数据
            LoadMenuItems(MainMenuStrip.Items[0] as ToolStripMenuItem);
        }
 
        //取消操作
        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            btn_Cancel.Visible = false;
            Manage = false;
            _dtCustomAction.RejectChanges();
            RecInitialButton();
            pic_img.Visible = false;
 
            btn_Manage.Text = "管理";
            btn_Manage.ImageOptions.Image = Resources.bringtofront_32x321;
 
            管理调整位置ToolStripMenuItem.Text = "管理-调整位置";
            管理调整位置ToolStripMenuItem.Image = Resources.bringtofront_32x321;
        }
 
        private void btn_Manage_Click(object sender, EventArgs e)
        {
            //CSFrameworkV5.UserCustom.Properties.Resources.savepagesetup_32x32
            //CSFrameworkV5.UserCustom.Properties.Resources.bringtofront_32x321
 
            if (!Manage)
            {
                Manage = true;
                btn_Cancel.Visible = true;
                lstEmptyCell.Clear();
                for (var i = 0; i < _ButtonTable.RowCount; i++)
                for (var j = 0; j < _ButtonTable.CellCount; j++)
                {
                    var tmpcell = _ButtonTable[i][j];
                    if (!tmpcell.IsUsed)
                    {
                        var btn = CreateEmptyButton();
                        tmpcell.ContainControl = btn;
                        pan_Container.Controls.Add(btn);
                        lstEmptyCell.Add(tmpcell);
                    }
                }
 
                btn_Manage.Text = "提交";
                btn_Manage.ImageOptions.Image = Resources.savepagesetup_32x32;
 
                管理调整位置ToolStripMenuItem.Text = "提交";
                管理调整位置ToolStripMenuItem.Image = Resources.savepagesetup_32x32;
            }
            else
            {
                Manage = false;
                btn_Cancel.Visible = false;
                foreach (var cl in lstEmptyCell)
                {
                    pan_Container.Controls.Remove(cl.ContainControl);
                    cl.ContainControl = null;
                }
 
                var tmp = _dtCustomAction.GetChanges();
                if (tmp != null)
                {
                    _bllUserCustomAction.Update(tmp);
                    _dtCustomAction.AcceptChanges();
                }
 
                btn_Manage.Text = "管理";
                btn_Manage.ImageOptions.Image = Resources.bringtofront_32x321;
 
                管理调整位置ToolStripMenuItem.Text = "管理-调整位置";
                管理调整位置ToolStripMenuItem.Image = Resources.bringtofront_32x321;
            }
        }
 
        private void btn_MouseDown(object sender, MouseEventArgs e)
        {
            if (Manage && e.Button == MouseButtons.Left)
            {
                _StartPoint = e.Location;
                //Point P = new Point(Control.MousePosition.X - (this.Location.X + this.GZ_Table.Location.X), Control.MousePosition.Y - (this.Location.Y + this.GZ_Table.Location.Y));
                var LP = (sender as SimpleButton).Location;
                var P = new Point(e.X + LP.X, e.Y + LP.Y);
                CellFrom = _ButtonTable.GetCellByPoint(P);
                GetMouseRect();
 
                if (!pic_img.Visible)
                {
                    var btn = sender as SimpleButton;
                    var handle = btn.Handle;
                    var img = ScreenCapture.CaptureWindow(handle);
                    BorderStyleBak = btn.BorderStyle;
                    btn.BorderStyle = BorderStyles.NoBorder;
                    pic_img.Image = img;
                    pic_img.Visible = true;
 
                    pic_img.Size = btn.Size;
                }
 
                GZLockMouse.Lock(_MouseRect);
            }
        }
 
        private void btn_MouseMove(object sender, MouseEventArgs e)
        {
            if (Manage && e.Button == MouseButtons.Left)
            {
                var btn = CellFrom.ContainControl as SimpleButton;
 
                var p = new Point(e.X + btn.Location.X, e.Y + btn.Location.Y);
                CellTo = _ButtonTable.GetCellByPoint(p);
                if (CellFrom != CellTo)
                {
                    if (ButtonBak != null && (CellTo == null ||
                                              ButtonBak !=
                                              CellTo.ContainControl as
                                                  SimpleButton))
                        ButtonStatusChange(ButtonBak, false);
 
                    if (CellTo != null)
                        if (ButtonBak == null || ButtonBak !=
                            CellTo.ContainControl as SimpleButton)
                        {
                            ButtonBak = CellTo.ContainControl as SimpleButton;
                            ButtonStatusChange(ButtonBak, true);
                        }
                }
 
                var NewP = new Point(
                    e.X + btn.Location.X + pan_Container.Location.X -
                    pic_img.Width / 2,
                    e.Y + btn.Location.Y + pan_Container.Location.Y -
                    pic_img.Height / 2);
                pic_img.Location = NewP;
            }
        }
 
        private void btn_MouseUp(object sender, MouseEventArgs e)
        {
            if (Manage && e.Button == MouseButtons.Left)
            {
                GZLockMouse.Unlock();
                if (CellTo != null)
                {
                    if (ButtonBak != null) ButtonStatusChange(ButtonBak, false);
 
                    #region 同步缓存数据表
 
                    DataRow drFrom;
                    DataRow drTo;
                    var RowIndexFrom = CellFrom.RowIndex;
                    var OrderIDFrom = CellFrom.ColIndex;
                    var RowIndexTo = CellTo.RowIndex;
                    var OrderIDTo = CellTo.ColIndex;
 
                    var where1 = string.Format("{0}='{1}' AND {2}='{3}'",
                        sys_UserCustomAction.RowIndex, RowIndexFrom,
                        sys_UserCustomAction.OrderID, OrderIDFrom);
                    var where2 = string.Format("{0}='{1}' AND {2}='{3}'",
                        sys_UserCustomAction.RowIndex, RowIndexTo,
                        sys_UserCustomAction.OrderID, OrderIDTo);
                    var drs1 = _dtCustomAction.Select(where1);
                    var drs2 = _dtCustomAction.Select(where2);
                    if (drs1.Length > 0)
                    {
                        drFrom = drs1[0];
                        drFrom[sys_UserCustomAction.RowIndex] = RowIndexTo;
                        drFrom[sys_UserCustomAction.OrderID] = OrderIDTo;
                    }
 
                    if (drs2.Length > 0)
                    {
                        drTo = drs2[0];
                        drTo[sys_UserCustomAction.RowIndex] = RowIndexFrom;
                        drTo[sys_UserCustomAction.OrderID] = OrderIDFrom;
                    }
 
                    #endregion
 
                    (CellFrom.ContainControl as SimpleButton).BorderStyle =
                        BorderStyleBak;
                    Cursor = Cursors.Default;
                    pic_img.Visible = false;
 
                    var Emp = lstEmptyCell.Contains(CellTo);
                    if (Emp)
                    {
                        lstEmptyCell.Remove(CellTo);
                        lstEmptyCell.Add(CellFrom);
                    }
 
                    var col = CellFrom.ContainControl;
                    CellFrom.ContainControl = CellTo.ContainControl;
                    CellTo.ContainControl = col;
 
                    CellFrom = null;
                    CellTo = null;
                    ButtonBak = null;
                }
            }
        }
 
        private void ButtonStatusChange(SimpleButton btn, bool Large)
        {
            if (Large)
            {
                btn.Width += AddWidth;
                btn.Height += AddWidth;
                btn.Location = new Point(btn.Location.X - AddWidth / 2,
                    btn.Location.Y - AddWidth / 2);
            }
            else
            {
                btn.Width = BTN_WIDTH;
                btn.Height = BTN_HEIGHT;
                btn.Location = new Point(btn.Location.X + AddWidth / 2,
                    btn.Location.Y + AddWidth / 2);
            }
        }
 
        private SimpleButton CreateEmptyButton()
        {
            var btn = new SimpleButton();
            btn.ButtonStyle = BorderStyles.NoBorder;
            btn.Text = "+";
            btn.Width = BTN_WIDTH;
            btn.Height = BTN_HEIGHT;
            btn.Enabled = false;
            return btn;
        }
 
        public override MenuStrip GetModuleMenu()
        {
            return menuStrip1;
        }
 
        private void GetMouseRect()
        {
            var HandFirst = _ButtonTable[0][0].ContainControl.Handle.ToInt32();
            var r1 = new GZLockMouse.Rect();
            GZLockMouse.GetWindowRect(HandFirst, ref r1);
 
            var HandLast =
                _ButtonTable[_ButtonTable.RowCount - 1][
                        _ButtonTable.CellCount - 1].ContainControl.Handle
                    .ToInt32();
            var r2 = new GZLockMouse.Rect();
            GZLockMouse.GetWindowRect(HandLast, ref r2);
 
            var rt = new GZLockMouse.Rect();
            rt.Top = r1.Top + BTN_MARGIN;
            rt.Left = r1.Left + BTN_MARGIN;
            rt.Bottom = r2.Bottom - BTN_MARGIN;
            rt.Right = r2.Right - BTN_MARGIN;
            _MouseRect = rt;
        }
 
        /*
         *
         * 思路:
         * 1.usp_GetUserCustomActionMenu 取快捷菜单
         * 2.初始化模块主菜单(this.MainMenuStrip),主窗体自动加载菜单
         * 3.Get Button, 找到菜单,绑定事件
         */
 
        private void InitialButton()
        {
            RemoveButton();
 
            _dtCustomAction = _bllUserCustomAction.GetCurrentCustomAction();
 
            foreach (DataRow dr in _dtCustomAction.Rows)
            {
                var FormFullName =
                    ConvertEx.ToString(dr[sys_UserCustomAction.FormFullName]);
                var MenuName =
                    ConvertEx.ToString(dr[sys_UserCustomAction.MenuName]);
                var BtnName =
                    ConvertEx.ToString(dr[sys_UserCustomAction.ButtonName]);
 
                var RowIndex =
                    ConvertEx.ToInt(dr[sys_UserCustomAction.RowIndex]);
                var OrderID = ConvertEx.ToInt(dr[sys_UserCustomAction.OrderID]);
 
                //获取按钮实例
                var ctl =
                    (MdiTools.MainForm as IMdiForm)
                    .GetModuleMainFormButtonByMenuName(FormFullName, MenuName);
                if (ctl == null || ctl.Enabled == false)
                {
                    (MdiTools.MainForm as IMdiForm).RemoveFunction(BTN_PREFIX +
                        MenuName);
                    continue; //无权限:按钮不存在或不可用,不加载此按钮。
                }
 
                var Refbtn = ctl as SimpleButton;
                var btn = new SimpleButton();
                btn.Image = Refbtn.Image;
                btn.Text = Refbtn.Text;
                btn.Appearance.Options.UseTextOptions = true;
                btn.Appearance.TextOptions.WordWrap = WordWrap.Wrap;
                btn.ImageLocation = ImageLocation.MiddleLeft;
                btn.Cursor = Cursors.Hand;
                btn.ButtonStyle = BorderStyles.HotFlat;
                btn.Size = new Size(BTN_WIDTH, BTN_HEIGHT);
                btn.Margin = new Padding(5, 5, 0, 0);
                var propertyInfo =
                    typeof(SimpleButton).GetProperty("Events",
                        BindingFlags.Instance | BindingFlags.NonPublic);
                var eventHandlerList =
                    (EventHandlerList)propertyInfo.GetValue(Refbtn, null);
                var fieldInfo = typeof(Control).GetField("EventClick",
                    BindingFlags.Static | BindingFlags.NonPublic);
                var d = eventHandlerList[fieldInfo.GetValue(null)];
                if (d != null)
                    foreach (var temp in d.GetInvocationList())
                        btn.Click += (EventHandler)temp;
 
                btn.MouseDown += btn_MouseDown;
                btn.MouseMove += btn_MouseMove;
                btn.MouseUp += btn_MouseUp;
 
                _ButtonTable[RowIndex][OrderID].ContainControl = btn;
                pan_Container.Controls.Add(btn);
 
                //按钮对象字典
                _ShortCutButtons[MenuName] = btn;
            }
        }
 
        public override void LoadCustomFunction()
        {
            InitialButton();
        }
 
        private void LoadMenuItems(ToolStripMenuItem menu)
        {
            var dt = _bllUserCustomAction.GetMenuItems();
            ToolStripItem item;
            foreach (DataRow R in dt.Rows)
            {
                item = menu.DropDownItems.Add(R["MenuCaption"].ToStringEx());
                item.Name = BTN_PREFIX + R["MenuName"].ToStringEx();
                item.Tag = new MenuItemTag(MenuType.DataForm,
                    (int)Interfaces.ModuleID.UserCustom,
                    AuthorityCategory.NONE);
                item.Click += OnMenuItem_Click;
            }
        }
 
        private void OnMenuItem_Click(object sender, EventArgs e)
        {
            var MenuName = (sender as ToolStripMenuItem).Name;
            MenuName = MenuName.Replace(BTN_PREFIX, "");
            if (_ShortCutButtons.ContainsKey(MenuName))
                _ShortCutButtons[MenuName].PerformClick();
        }
 
        private void RecInitialButton()
        {
            RemoveButton();
 
            _dtCustomAction = _bllUserCustomAction.GetCurrentCustomAction();
 
            foreach (DataRow dr in _dtCustomAction.Rows)
            {
                var FormFullName =
                    ConvertEx.ToString(dr[sys_UserCustomAction.FormFullName]);
                var MenuName =
                    ConvertEx.ToString(dr[sys_UserCustomAction.MenuName]);
                var BtnName =
                    ConvertEx.ToString(dr[sys_UserCustomAction.ButtonName]);
 
                var SizeW = ConvertEx.ToInt(dr[sys_UserCustomAction.SizeW]);
                var SizeH = ConvertEx.ToInt(dr[sys_UserCustomAction.SizeH]);
                var RowIndex =
                    ConvertEx.ToInt(dr[sys_UserCustomAction.RowIndex]);
                var OrderID = ConvertEx.ToInt(dr[sys_UserCustomAction.OrderID]);
 
                var col =
                    (MdiTools.MainForm as IMdiForm)
                    .GetModuleMainFormButtonByMenuName(FormFullName, MenuName);
 
                if (col == null) continue;
 
                var Refbtn = col as SimpleButton;
                var btn = new SimpleButton();
 
                btn.Appearance.Options.UseTextOptions = true;
                btn.Appearance.TextOptions.WordWrap = WordWrap.Wrap;
                btn.ImageLocation = ImageLocation.MiddleLeft;
                btn.Cursor = Cursors.Hand;
                btn.ButtonStyle = BorderStyles.HotFlat;
                btn.Size = new Size(BTN_WIDTH, BTN_HEIGHT);
                btn.Tag = new[] { FormFullName, MenuName };
                btn.Image = Refbtn.Image;
                btn.Text = Refbtn.Text;
                btn.Margin = new Padding(5, 5, 0, 0);
 
                var propertyInfo =
                    typeof(SimpleButton).GetProperty("Events",
                        BindingFlags.Instance | BindingFlags.NonPublic);
                var eventHandlerList =
                    (EventHandlerList)propertyInfo.GetValue(Refbtn, null);
                var fieldInfo = typeof(Control).GetField("EventClick",
                    BindingFlags.Static | BindingFlags.NonPublic);
                var d = eventHandlerList[fieldInfo.GetValue(null)];
                if (d != null)
                    foreach (var temp in d.GetInvocationList())
                        btn.Click += (EventHandler)temp;
 
                btn.MouseDown += btn_MouseDown;
                btn.MouseMove += btn_MouseMove;
                btn.MouseUp += btn_MouseUp;
 
                _ButtonTable[RowIndex][OrderID].ContainControl = btn;
 
                pan_Container.Controls.Add(btn);
            }
        }
 
        private void RemoveButton()
        {
            for (var i = 0; i < _ButtonTable.RowCount; i++)
            for (var j = 0; j < _ButtonTable.CellCount; j++)
                _ButtonTable[i][j].ContainControl = null;
 
            pan_Container.Controls.Clear();
        }
 
        public override void SetSecurity(object securityInfo)
        {
            btn_Cancel.Visible = false;
        }
 
        public override void UpdateButtonLocation()
        {
            InitialButton();
        }
 
        private void 管理调整位置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btn_Manage_Click(btn_Manage, new EventArgs());
        }
 
        private void 取消ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btn_Cancel_Click(btn_Cancel, new EventArgs());
        }
 
        private void 重置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Msg.AskQuestion("重置操作将删除我的常用功能,确定要操作吗?"))
                //重置数据
                if (_bllUserCustomAction.ResetCustomActions(Loginer.CurrentUser
                        .Account))
                    //刷新界面
                    btn_Cancel_Click(btn_Cancel, new EventArgs());
        }
    }
}