using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Text; using System.Windows.Forms; using System.Reflection; using System.Drawing.Drawing2D; using LYFZ.WinAPI; namespace LYFZ.ComponentLibrary { /// /// 无边框窗体母窗体 /// public partial class BaseMotherForm : Form { public BaseMotherForm() { InitializeComponent(); SetStyles(); } void BaseMotherForm_DoubleClick(object sender, EventArgs e) { SetWindowState(); } #region 减少闪烁 private void SetStyles() { SetStyle( ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.DoubleBuffer, true); //强制分配样式重新应用到控件上 UpdateStyles(); base.AutoScaleMode = AutoScaleMode.None; } #endregion private void BaseMotherForm_MouseDown(object sender, MouseEventArgs e) { if(e.Clicks==1) FormMove(); } /// /// 移动窗体 /// public void FormMove() { if (FormMobile) { Win32.FormMobile(this.Handle); } } #region 变量属性 //不显示FormBorderStyle属性 [Browsable(false)] [EditorBrowsable(EditorBrowsableState.Never)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public new FormBorderStyle FormBorderStyle { get { return base.FormBorderStyle; } set { base.FormBorderStyle = FormBorderStyle.None; } } private bool _FormMobile = true; /// /// 窗体是否可以移动 /// [DescriptionAttribute("窗体是否可以移动"), CategoryAttribute("自定义窗体属性"), DefaultValueAttribute(typeof(bool), "true")] public bool FormMobile { get { return _FormMobile; } set { if (_FormMobile != value) { _FormMobile = value; } } } private bool _isMaximized = true; /// /// 窗体是是允许最大化 /// [Category("自定义窗体属性")] [Description("窗体是是允许最大化")] [DefaultValue(typeof(bool), "true")] public bool IsMaximized { get { return _isMaximized; } set { _isMaximized = value;} } private bool _isReion = false; /// /// 窗体是是否圆角 /// [Category("自定义窗体属性")] [Description("窗体是是否圆角")] [DefaultValue(typeof(bool), "false")] public bool IsReion { get { return _isReion; } set { _isReion = value; } } bool _isFullScreen = false; /// /// 窗体WindowState为FormWindowState.Maximized值时,是否可以全屏显示 /// [Category("自定义窗体属性")] [Description("窗体WindowState为FormWindowState.Maximized值时,是否可以全屏显示")] [DefaultValue(typeof(bool), "false")] public bool IsFullScreen { get { return _isFullScreen; } set { _isFullScreen = value; } } bool _isUserControlsSize = false; /// /// 窗体是否允许用户拖动控件大小 /// [Category("自定义窗体属性")] [Description("窗体是否允许用户拖动控件大小")] [DefaultValue(typeof(bool), "false")] public bool IsUserControlsSize { get { return _isUserControlsSize; } set { _isUserControlsSize = value; } } #endregion //控件首次创建时被调用 protected override void OnCreateControl() { base.OnCreateControl(); if (IsReion) { SetReion();//需要圆角时开启 } } //圆角 private void SetReion() { using (GraphicsPath path = GraphicsPathHelper.CreatePath( new Rectangle(Point.Empty, base.Size), 6, RoundStyle.All, true)) { Region region = new Region(path); path.Widen(Pens.White); region.Union(path); this.Region = region; } } //改变窗体大小时 protected override void OnSizeChanged(EventArgs e) { base.OnSizeChanged(e); if (IsReion) { SetReion();//需要圆角时开启 } if (!this._isFullScreen) { this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height); } //else { // this.MaximumSize = new Size(0,0); //} } /// /// 设置窗体最大化和还原状态 /// public void SetWindowState() { if (IsMaximized) { if (this.MaximizeBox && this.MinimizeBox) { if (this.WindowState == FormWindowState.Normal) { this.WindowState = FormWindowState.Maximized; } else { this.WindowState = FormWindowState.Normal; } } } } #region 重写CreateParams protected override CreateParams CreateParams { get { try { const int WS_MINIMIZEBOX = 0x00020000; // Winuser.h中定义 CreateParams cp = base.CreateParams; cp.Style = cp.Style | WS_MINIMIZEBOX; // 允许最小化操作 return cp; } catch { return base.CreateParams; } } } #endregion #region 窗体可调大小 const int Guying_HTLEFT = 10; const int Guying_HTRIGHT = 11; const int Guying_HTTOP = 12; const int Guying_HTTOPLEFT = 13; const int Guying_HTTOPRIGHT = 14; const int Guying_HTBOTTOM = 15; const int Guying_HTBOTTOMLEFT = 0x10; const int Guying_HTBOTTOMRIGHT = 17; protected override void WndProc(ref Message m) { if (IsUserControlsSize) { switch (m.Msg) { case 0x0084: base.WndProc(ref m); Point vPoint = new Point((int)m.LParam & 0xFFFF, (int)m.LParam >> 16 & 0xFFFF); vPoint = PointToClient(vPoint); if (vPoint.X <= 5) if (vPoint.Y <= 5) m.Result = (IntPtr)Guying_HTTOPLEFT; else if (vPoint.Y >= ClientSize.Height - 5) m.Result = (IntPtr)Guying_HTBOTTOMLEFT; else m.Result = (IntPtr)Guying_HTLEFT; else if (vPoint.X >= ClientSize.Width - 5) if (vPoint.Y <= 5) m.Result = (IntPtr)Guying_HTTOPRIGHT; else if (vPoint.Y >= ClientSize.Height - 5) m.Result = (IntPtr)Guying_HTBOTTOMRIGHT; else m.Result = (IntPtr)Guying_HTRIGHT; else if (vPoint.Y <= 5) m.Result = (IntPtr)Guying_HTTOP; else if (vPoint.Y >= ClientSize.Height - 5) m.Result = (IntPtr)Guying_HTBOTTOM; break; case 0x0201: //鼠标左键按下的消息 m.Msg = 0x00A1; //更改消息为非客户区按下鼠标 m.LParam = IntPtr.Zero; //默认值 m.WParam = new IntPtr(2);//鼠标放在标题栏内 base.WndProc(ref m); break; default: base.WndProc(ref m); break; } } else { base.WndProc(ref m); } } #endregion } }