节省宝贵的屏幕空间隐藏很少使用的winform控件
当我们布局窗体控件时,会发现有些控件很少使用到但却又必不可少,比如一些系统的参数输入项。遇到这种情况时,我们通常会将这些很少使用的参数单独放置在一个窗体中
进行配置。在其它窗体中需要使用这些参数时,再调用。如果系统参数很少,而且只在某个窗体中需要使用到,这时我们可以将参数放置在当前窗体中进行配置,比如使用TextBox
控件让客户进行输入,为了不让客户经常看到这个控件,我们也可以使用一些自动伸缩的控件,但是这种做法不会减小整个窗体的空间,下面就介绍一种能够减少窗体的空间的做
法。代码如下:(http://www.codeproject.com/Articles/10091/Save-valuable-screen-space-by-hiding-seldom-used-o)
using System; using System.Drawing; using System.Windows.Forms; namespace ShrinkAreaTest { public partial class Form1 : Form { ShrinkArea shrinkArea_horizon; ShrinkArea shrinkArea_vertical; public Form1() { InitializeComponent(); shrinkArea_horizon = new ShrinkArea(this, Left, Right, false); Rectangle rect_grpHorizon = shrinkArea_horizon.GetFormRelativeBounds(grpHorizon); shrinkArea_horizon = new ShrinkArea(this, rect_grpHorizon.Left, rect_grpHorizon.Right, false); shrinkArea_vertical = new ShrinkArea(this, Top, Bottom, true); Rectangle rect_grpVertical = shrinkArea_vertical.GetFormRelativeBounds(grpVertical); shrinkArea_vertical = new ShrinkArea(this, rect_grpVertical.Top, rect_grpVertical.Bottom, true); shrinkArea_vertical.Expanded = false; } private void btnVertical_Click(object sender, EventArgs e) { shrinkArea_vertical.Toggle(); } private void btnHorizon_Click(object sender, EventArgs e) { shrinkArea_horizon.Toggle(); } } }
ShrinkArea如下:
using System; using System.Windows.Forms; using System.Collections; using System.Drawing; namespace ShrinkAreaTest { public class ShrinkArea { Form form; bool expanded = true; bool vertical; int areaSize; Hashtable areaControls = new Hashtable(); Hashtable outsideControls = new Hashtable(); public Rectangle GetFormRelativeBounds(Control control) { Control current = control; Form form = control.FindForm(); int x = 0; int y = 0; while (current != control.FindForm()) { x += current.Location.X; y += current.Location.Y; current = current.Parent; } return new Rectangle(x, y, control.Bounds.Width, control.Bounds.Height); } public ShrinkArea(Form form, int areaBegin, int areaEnd,bool vertical) { this.vertical = vertical; this.form = form; this.areaSize = areaEnd - areaBegin; foreach (Control control in form.Controls) { if (areaBegin <= (vertical ? control.Location.Y : control.Location.X) && (vertical ? control.Location.Y : control.Location.X) < areaEnd) { areaControls.Add(control,null); } if ((vertical ? control.Location.Y : control.Location.X) >= areaEnd) { outsideControls.Add(control,null); } } } public void Toggle() { int diff = expanded ? -areaSize : areaSize; form.SuspendLayout(); foreach (Control control in form.Controls) { if (areaControls.Contains(control)) { control.Visible = !expanded; } if (outsideControls.Contains(control)) { if (!vertical) control.Top += diff; else control.Left += diff; } } form.Size = new Size(form.Size.Width + (vertical ? 0 : diff), form.Size.Height + (vertical ? diff : 0)); form.ResumeLayout(); expanded = !expanded; } public bool Expanded { get { return expanded; } set { if (expanded == value) { return; } Toggle(); } } } }