#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());
|
}
|
}
|
}
|