/// <summary>Загрузить параметры отображения формы</summary> public void LoadFormOptions(Form form, FormOptionsSetType setType = FormOptionsSetType.All) { if (AppConfig.HasProp("UserOptionsInDB")) { FormOptions.LoadFromXML(form, userFormOptions, setType); } if (AppConfig.HasProp("UserOptionsInFile")) { FormOptions.LoadFromFile(form, null, setType); } }
// загрузить параметры из формы internal void GetFromForm(Form form) { string mode = form.StartPosition == FormStartPosition.CenterParent ? "Parent" : "Default"; FormOptions opt = Forms.FirstOrDefault(x => x.Name == form.Name && x.Mode == mode); if (opt == null) { opt = new FormOptions() { Name = form.Name, Mode = mode }; Forms.Add(opt); } if (form.WindowState == FormWindowState.Normal) { opt.Top = form.Top; opt.Left = form.Left; opt.Width = form.Width; opt.Height = form.Height; } opt.Grids.Clear(); form.ForControls((c) => { if (c is DataGridView) { opt.Grids.Add(new GridOptions((DataGridView)c)); } }); opt.Splits.Clear(); form.ForControls((c) => { opt.Splits.Add(new SplitOptions() { Name = c.Name, Distance = ((SplitContainer)c).SplitterDistance, Panel1Collapsed = ((SplitContainer)c).Panel1Collapsed, Panel2Collapsed = ((SplitContainer)c).Panel2Collapsed }); }, typeof(SplitContainer)); if (form is IDataForm) { opt.ControlValues.Clear(); opt.ControlValues = ((IDataForm)form).PrepareUiParamsProperties(opt.ControlValues); } }
/// <summary>Сохранить параметры отображения формы</summary> public void SaveFormOptions(Form form) { if (AppConfig.HasProp("UserOptionsInDB")) { userFormOptions = FormOptions.GetInXML(form, userFormOptions); Conn?.ExecCommand(@" if exists(select 1 from dm.tFormOptions where code = @user and appcode = @appcode) update dm.tFormOptions set options = @opt where code = @user and appcode = @appcode else insert dm.tFormOptions(code, appcode, options) values (@user, @appcode, @opt)", new Dictionary <string, object>() { { "user", username }, { "appcode", appcode }, { "opt", userFormOptions } }); } if (AppConfig.HasProp("UserOptionsInFile")) { FormOptions.SaveToFile(form); } }
// установить параметры для формы internal void SetToForm(Form form, FormOptionsSetType setType = FormOptionsSetType.All) { string mode = form.StartPosition == FormStartPosition.CenterParent ? "Parent" : "Default"; FormOptions opt = Forms.FirstOrDefault(x => x.Name == form.Name && x.Mode == mode); if (opt == null) { return; } if (setType == FormOptionsSetType.All || setType == FormOptionsSetType.Size) { if (opt.Width >= form.MinimumSize.Width) { form.Width = opt.Width; } //else // form.Width = form.MinimumSize.Width > 0 ? form.MinimumSize.Width : 100; if (opt.Height >= form.MinimumSize.Height) { form.Height = opt.Height; } //else // form.Height = form.MinimumSize.Height > 0 ? form.MinimumSize.Height : 100; if (AppConfig.HasProp("RestoreFormPos")) { form.Top = opt.Top; form.Left = opt.Left; } } if (setType == FormOptionsSetType.All) { foreach (var item in opt.Splits) { Control c = form.GetControl(item.Name); if (c is SplitContainer && item.Distance > 0) { var sc = (SplitContainer)c; if ((sc.Orientation == Orientation.Horizontal && item.Distance < form.Height) || (sc.Orientation == Orientation.Vertical && item.Distance < form.Width)) { sc.SplitterDistance = item.Distance; } sc.Panel1Collapsed = item.Panel1Collapsed; sc.Panel2Collapsed = item.Panel2Collapsed; } } } if (setType == FormOptionsSetType.All || setType == FormOptionsSetType.Grid) { foreach (var item in opt.Grids) { Control c = form.GetControl(item.Name); if (c is DataGridView) { DataGridView g = ((DataGridView)c); foreach (DataGridViewColumn col in g.Columns) { ColumnOptions copt = item.Columns.FirstOrDefault(x => x.Name == col.Name); if (copt != null) { col.Width = copt.Width; if (copt.Pos >= 0 && copt.Pos < g.Columns.Count) { col.DisplayIndex = copt.Pos; } else { col.DisplayIndex = g.Columns.Count - 1; } col.Visible = copt.Visible; } } var colCheck = g.Columns[GridOptions.checkColumnName]; if (colCheck != null) { colCheck.DisplayIndex = 0; } } } } if (setType == FormOptionsSetType.All && form is IDataForm) { ((IDataForm)form).SetUiParamsProperties(opt.ControlValues); } }