123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Drawing;
- using System.Windows.Forms;
- using SAGA.DotNetUtils.Configration;
- namespace SAGA.DotNetUtils.WinForms
- {
- public class BaseForm : FormDataProvide
- {
- protected bool IsNeedEscClose = true;
- protected bool IsSaveFormData = true;
- public BaseForm()
- {
- base.ShowInTaskbar = false;
- base.FormBorderStyle = FormBorderStyle.FixedSingle;
- base.Icon = AppBaseInfo.AppIcon;
- this.IsSaveFormData = true;
- }
- protected void AbandonExtendComboxDropDownWidth()
- {
- foreach (Control control in base.FormControls)
- {
- if (control is ComboBox)
- {
- ((ComboBox) control).DropDown -= new EventHandler(this.ComboBox_DropDown);
- }
- }
- }
- protected void AutoExtendComboxDropDownWidth()
- {
- foreach (Control control in base.FormControls)
- {
- if (control is ComboBox)
- {
- ((ComboBox) control).DropDown += new EventHandler(this.ComboBox_DropDown);
- }
- }
- }
- private void ComboBox_DropDown(object sender, EventArgs e)
- {
- ComboBox box = sender as ComboBox;
- if (box != null)
- {
- int dropDownWidth = box.DropDownWidth;
- using (Graphics graphics = box.CreateGraphics())
- {
- int num2 = (box.Items.Count > box.MaxDropDownItems) ? (SystemInformation.VerticalScrollBarWidth + 5) : 0;
- Font font = box.Font;
- foreach (object obj2 in box.Items)
- {
- string text = obj2.ToString();
- int num3 = (int) Math.Round((double) (graphics.MeasureString(text, font).Width + num2), 0);
- if (dropDownWidth < num3)
- {
- dropDownWidth = num3;
- }
- }
- if (box.DropDownWidth != dropDownWidth)
- {
- box.DropDownWidth = dropDownWidth;
- }
- }
- }
- }
- private void Control_KeyDown(object sender, KeyEventArgs e)
- {
- if (this.IsNeedEscClose && (e.KeyCode == Keys.Escape))
- {
- base.Close();
- }
- }
- private void DealKeyDown(Control ctrl)
- {
- if (ctrl.Controls.Count != 0)
- {
- foreach (Control control in ctrl.Controls)
- {
- this.DealKeyDown(control);
- }
- }
- ctrl.KeyDown += new KeyEventHandler(this.Control_KeyDown);
- }
- protected virtual void InitializeData()
- {
- }
- protected override void OnClosed(EventArgs e)
- {
- if (this.IsSaveFormData)
- {
- this.SaveFormData();
- }
- base.OnClosed(e);
- }
- protected override void OnLoad(EventArgs e)
- {
- base.OnLoad(e);
- this.InitializeData();
- this.AutoExtendComboxDropDownWidth();
- Dictionary<string, object> dictionary = this.LoadConfigFrom();
- if (dictionary != null)
- {
- if (dictionary.ContainsKey("FormOldLocation"))
- {
- int[] numArray = dictionary["FormOldLocation"] as int[];
- Point pt = new Point(numArray[0], numArray[1]);
- IntPtr mainWindowHandle = Process.GetCurrentProcess().MainWindowHandle;
- if ((mainWindowHandle != IntPtr.Zero) && Screen.FromHandle(mainWindowHandle).Bounds.Contains(pt))
- {
- base.Location = pt;
- }
- }
- if (dictionary.ContainsKey("FormSize") && (base.FormBorderStyle != FormBorderStyle.FixedSingle))
- {
- int[] numArray2 = dictionary["FormSize"] as int[];
- if (numArray2 != null)
- {
- base.Size = new Size(numArray2[0], numArray2[1]);
- }
- }
- }
- foreach (Control control in this.FormControls)
- {
- this.DealKeyDown(control);
- }
- base.KeyDown += new KeyEventHandler(this.Control_KeyDown);
- }
- }
- }
|