using DevExpress.XtraGrid.Views.BandedGrid; using DevExpress.XtraGrid.Views.Grid; using DevExpress.XtraTab; using Lskj.Control.ZKFinger; using Lskj.Util; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using WinFormsControl = System.Windows.Forms.Control; namespace Lskj.Control.Model { public static class StaticBandedControl { public static void ReleaseModuleReferences(WinFormsControl moduleRoot, XtraTabPage modulePage, string moduleCode) { List moduleControls = CollectModuleControls(moduleRoot, modulePage); ReleaseModuleReferences(moduleControls, modulePage, moduleCode); } internal static void ReleaseModuleReferences( IEnumerable moduleControls, XtraTabPage modulePage, string moduleCode) { try { List controls = new List(); if (moduleControls != null) foreach (WinFormsControl control in moduleControls) if (control != null && !ContainsControl(controls, control)) controls.Add(control); List bandedViews = CollectBandedGridViews(controls); List targetViews = CollectTargetGridViews(controls); foreach (BandedGridView view in bandedViews) BandedGridDragGrid.DisposeRegistrations( BandedGridViewDragGridDic, BandedTargetViewDragGridDic, view, null); foreach (GridView view in targetViews) BandedGridDragGrid.DisposeRegistrations( BandedGridViewDragGridDic, BandedTargetViewDragGridDic, null, view); if (ContainsBandedGridView(bandedViews, SourceDragBandedGridView)) SourceDragBandedGridView = null; RemoveConditionPanels(moduleCode, controls); RemoveProtectedReferences(controls, modulePage); } catch (Exception exception) { System.Diagnostics.Debug.WriteLine("释放旧 Banded 模块引用失败:" + exception); } } private static List CollectModuleControls( WinFormsControl moduleRoot, XtraTabPage modulePage) { List result = new List(); AddModuleControls(result, moduleRoot); if (!ReferenceEquals(moduleRoot, modulePage)) AddModuleControls(result, modulePage); return result; } private static void AddModuleControls( List target, WinFormsControl root) { if (root == null) return; foreach (WinFormsControl control in EnumerateControls(root)) if (!ContainsControl(target, control)) target.Add(control); } private static List CollectBandedGridViews( IEnumerable controls) { List result = new List(); if (controls == null) return result; foreach (WinFormsControl control in controls) { DevExpress.XtraGrid.GridControl grid = control as DevExpress.XtraGrid.GridControl; if (grid == null) continue; try { AddBandedGridView(result, grid.MainView as BandedGridView); foreach (DevExpress.XtraGrid.Views.Base.BaseView baseView in grid.ViewCollection) AddBandedGridView(result, baseView as BandedGridView); } catch (Exception exception) { System.Diagnostics.Debug.WriteLine("读取旧 Banded 表格视图失败:" + exception); } } return result; } private static void AddBandedGridView( List target, BandedGridView view) { if (view != null && !ContainsBandedGridView(target, view)) target.Add(view); } private static bool ContainsBandedGridView( IEnumerable gridViews, BandedGridView value) { if (gridViews == null || value == null) return false; foreach (BandedGridView gridView in gridViews) if (ReferenceEquals(gridView, value)) return true; return false; } private static List CollectTargetGridViews( IEnumerable controls) { List result = new List(); if (controls == null) return result; foreach (WinFormsControl control in controls) { DevExpress.XtraGrid.GridControl grid = control as DevExpress.XtraGrid.GridControl; if (grid == null) continue; try { AddGridView(result, grid.MainView as GridView); foreach (DevExpress.XtraGrid.Views.Base.BaseView baseView in grid.ViewCollection) AddGridView(result, baseView as GridView); } catch (Exception exception) { System.Diagnostics.Debug.WriteLine("读取旧 Banded 目标表格视图失败:" + exception); } } return result; } private static void AddGridView(List target, GridView view) { if (view != null && !ContainsGridView(target, view)) target.Add(view); } private static bool ContainsGridView( IEnumerable gridViews, GridView value) { if (gridViews == null || value == null) return false; foreach (GridView gridView in gridViews) if (ReferenceEquals(gridView, value)) return true; return false; } private static IEnumerable EnumerateControls(WinFormsControl root) { yield return root; foreach (WinFormsControl child in root.Controls) foreach (WinFormsControl nested in EnumerateControls(child)) yield return nested; } private static void RemoveConditionPanels( string moduleCode, IEnumerable moduleControls) { foreach (string key in ConditionsPanelDic.Keys.ToList()) { ModuleConditionsPanelEx panel; if (!ConditionsPanelDic.TryGetValue(key, out panel) || panel == null || panel.IsDisposed) { ConditionsPanelDic.Remove(key); continue; } if (string.Equals(key, moduleCode, StringComparison.OrdinalIgnoreCase) && IsConditionPanelOwnedByModule(panel, moduleControls)) ConditionsPanelDic.Remove(key); } } private static bool IsConditionPanelOwnedByModule( ModuleConditionsPanelEx panel, IEnumerable moduleControls) { if (panel == null) return false; if (ContainsControl(moduleControls, panel) || ContainsControl(moduleControls, panel.Owner)) return true; MyControl parentControl = panel.ParentControlObj; return parentControl != null && parentControl.MainGridEx != null && ContainsControl(moduleControls, parentControl.MainGridEx); } private static void RemoveProtectedReferences( IEnumerable moduleControls, XtraTabPage modulePage) { while (modulePage != null && DogVerifyModuleForms.Remove(modulePage)) { } foreach (XtraTabPage item in DogVerifyModuleForms.ToList()) if (item == null || item.IsDisposed || ContainsControl(moduleControls, item)) DogVerifyModuleForms.Remove(item); foreach (IForm item in DogVerifyNoPageForms.ToList()) if (item == null || item.SubForm == null || item.SubForm.IsDisposed || ContainsControl(moduleControls, item.SubForm)) DogVerifyNoPageForms.Remove(item); } private static bool ContainsControl( IEnumerable moduleControls, WinFormsControl value) { if (moduleControls == null || value == null) return false; foreach (WinFormsControl control in moduleControls) if (ReferenceEquals(control, value)) return true; return false; } /// /// 记录当前U盾权限窗口 /// public static List DogVerifyModuleForms = new List(); public static List DogVerifyNoPageForms = new List(); /// ///模块打开时的条件界面 /// public static Dictionary ConditionsPanelDic = new Dictionary(); /// /// 表格拖拽类,key值为源表,多表格 /// public static Dictionary> BandedGridViewDragGridDic = new Dictionary>(); /// /// 表格拖拽类,key值为目标表 /// public static Dictionary> BandedTargetViewDragGridDic = new Dictionary>(); /// /// 当前拖拽源表格 /// public static BandedGridView SourceDragBandedGridView; /// /// 指纹识别模块 /// public static FingerHelper fingerHelper; /// /// 进度条 /// public static FrmProgressBar frmProgressBar; } }