c# winform 窗体最小化和最大化控件简单封装(SkinsDLL)。

后续再继续新增功能
直接上代码:
namespace
SkinsDll { public partial class bgTopControl : UserControl { public bool SetMove = true;//是否可移动 private bool capture = false; private bool ismax = false; private Point position; /// <summary> /// 记录该控件最顶级的父类(Form) /// </summary> private Form _ParentForm = null; /// <summary> /// 设置窗体 /// </summary> public Form ParentFm { get { return _ParentForm; } set { _ParentForm = value; } } private bool _IsGC = false; /// <summary> /// 是否进行垃圾回收 /// </summary> public bool IsGC { get { return _IsGC; } set { _IsGC = value; } } public bgTopControl() { InitializeComponent(); SetCtrlMove(this); } private void SetCtrlMove(Control Pctrl) { foreach (object var in Pctrl.Controls) { ((Control)var).DoubleClick += new EventHandler(ctl_DoubleClick); ((Control)var).Click += new EventHandler(ctl_Click); } } /// <summary> /// 继承基类的单击事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ctl_Click(object sender, EventArgs e) { base.OnClick(e); } private void ctl_DoubleClick(object sender, EventArgs e) { base.OnDoubleClick(e); } private void bgTopControl_MouseUp(object sender, MouseEventArgs e) { if (SetMove) { capture = false; } } private void bgTopControl_MouseMove(object sender, MouseEventArgs e) { if (SetMove) { try { if (e.Button == MouseButtons.Right) return; if (capture && _ParentForm != null) { this._ParentForm.Location = new Point(e.X + this._ParentForm.Left - position.X, e.Y + this._ParentForm.Top - position.Y); } } catch { } } } private void bgTopControl_MouseDown(object sender, MouseEventArgs e) { if (SetMove) { if (e.Button != MouseButtons.Left) return; position = new Point(e.X, e.Y); capture = true; } } private void bgtoppanel_DoubleClick(object sender, EventArgs e) { if (_ParentForm == null) { return; } if (ismax == false) { _ParentForm.WindowState = FormWindowState.Maximized; ismax = true; } else { _ParentForm.WindowState = FormWindowState.Normal; ismax = false; } this.Width = _ParentForm.Width - 10; //this.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top & System.Windows.Forms.AnchorStyles.Right & System.Windows.Forms.AnchorStyles.Left))); } private void minpicbox_MouseMove(object sender, MouseEventArgs e) { minpicbox.BackgroundImage = global::SkinsDll.Properties.Resources.MainMinMove; } private void minpicbox_MouseLeave(object sender, EventArgs e) { minpicbox.BackgroundImage = global::SkinsDll.Properties.Resources.MainMin; } private void closepicbox_MouseMove(object sender, MouseEventArgs e) { closepicbox.BackgroundImage = global::SkinsDll.Properties.Resources.MainExitMove; } private void closepicbox_MouseLeave(object sender, EventArgs e) { closepicbox.BackgroundImage = global::SkinsDll.Properties.Resources.MainExit; } private void bgtoppanel_MouseDown(object sender, MouseEventArgs e) { if (SetMove) { if (e.Button != MouseButtons.Left) return; position = new Point(e.X, e.Y); capture = true; } } private void bgtoppanel_MouseMove(object sender, MouseEventArgs e) { if (SetMove) { try { if (e.Button == MouseButtons.Right) return; if (capture && _ParentForm != null) { this._ParentForm.Location = new Point(e.X + this._ParentForm.Left - position.X, e.Y + this._ParentForm.Top - position.Y); } } catch { } } } private void bgtoppanel_MouseUp(object sender, MouseEventArgs e) { if (SetMove) { capture = false; } } private void closepicbox_Click(object sender, EventArgs e) { if (_ParentForm != null && _IsGC) { System.GC.Collect(); } _ParentForm.Close(); } private void minpicbox_Click(object sender, EventArgs e) { if (_ParentForm != null) { _ParentForm.WindowState = FormWindowState.Minimized; } } } }

最后的效果图:

 

 

这样以后就可以直接用这个控件了,来回双击该控件,可以切换最大化到原始大小。

 

posted @ 2017-07-01 19:38  longdb  阅读(745)  评论(0)    收藏  举报