123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- 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
- {
- /// <summary>
- /// 无边框窗体母窗体
- /// </summary>
- public partial class BaseMotherForm : Form
- {
- public BaseMotherForm()
- {
- InitializeComponent();
- SetStyles();
- this.Load += BaseMotherForm_Load;
- }
- void BaseMotherForm_Load(object sender, EventArgs e)
- {
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
- }
- void BaseMotherForm_DoubleClick(object sender, EventArgs e)
- {
- SetWindowState();
- }
- #region 减少闪烁
- private void SetStyles()
- {
- try
- {
- SetStyle(
- ControlStyles.UserPaint |
- ControlStyles.AllPaintingInWmPaint |
- ControlStyles.OptimizedDoubleBuffer |
- ControlStyles.ResizeRedraw |
- ControlStyles.DoubleBuffer, true);
- //强制分配样式重新应用到控件上
- UpdateStyles();
- base.AutoScaleMode = AutoScaleMode.None;
- }
- catch { }
- }
- #endregion
- private void BaseMotherForm_MouseDown(object sender, MouseEventArgs e)
- {
- if(e.Clicks==1)
- FormMove();
- }
- /// <summary>
- /// 移动窗体
- /// </summary>
- public void FormMove()
- {
- try
- {
- if (FormMobile)
- {
- Win32.FormMobile(this.Handle);
- }
- }
- catch { }
- }
- #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;
- /// <summary>
- /// 窗体是否可以移动
- /// </summary>
- [DescriptionAttribute("窗体是否可以移动"), CategoryAttribute("自定义窗体属性"), DefaultValueAttribute(typeof(bool), "true")]
- public bool FormMobile
- {
- get { return _FormMobile; }
- set
- {
- if (_FormMobile != value)
- {
- _FormMobile = value;
- }
- }
- }
- private bool _isMaximized = true;
- /// <summary>
- /// 窗体是是允许最大化
- /// </summary>
- [Category("自定义窗体属性")]
- [Description("窗体是是允许最大化")]
- [DefaultValue(typeof(bool), "true")]
- public bool IsMaximized
- {
- get { return _isMaximized; }
- set { _isMaximized = value;}
- }
- private bool _isReion = false;
- /// <summary>
- /// 窗体是是否圆角
- /// </summary>
- [Category("自定义窗体属性")]
- [Description("窗体是是否圆角")]
- [DefaultValue(typeof(bool), "false")]
- public bool IsReion
- {
- get { return _isReion; }
- set { _isReion = value; }
- }
- bool _isFullScreen = false;
- /// <summary>
- /// 窗体WindowState为FormWindowState.Maximized值时,是否可以全屏显示
- /// </summary>
- [Category("自定义窗体属性")]
- [Description("窗体WindowState为FormWindowState.Maximized值时,是否可以全屏显示")]
- [DefaultValue(typeof(bool), "false")]
- public bool IsFullScreen
- {
- get { return _isFullScreen; }
- set { _isFullScreen = value; }
- }
- bool _isUserControlsSize = false;
- /// <summary>
- /// 窗体是否允许用户拖动控件大小
- /// </summary>
- [Category("自定义窗体属性")]
- [Description("窗体是否允许用户拖动控件大小")]
- [DefaultValue(typeof(bool), "false")]
- public bool IsUserControlsSize
- {
- get { return _isUserControlsSize; }
- set { _isUserControlsSize = value; }
- }
- #endregion
- //控件首次创建时被调用
- protected override void OnCreateControl()
- {
- try
- {
- base.OnCreateControl();
- if (IsReion)
- {
- SetReion();//需要圆角时开启
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show("控件首次创建时:" + ex.Message + ex.Source);
- }
- }
- //圆角
- private void SetReion()
- {
- try
- {
- 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;
- }
- }
- catch { }
- }
- //改变窗体大小时
- protected override void OnSizeChanged(EventArgs e)
- {
- try
- {
- 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);
- //}
- }
- catch { }
- }
- /// <summary>
- /// 设置窗体最大化和还原状态
- /// </summary>
- public void SetWindowState()
- {
- try
- {
- if (IsMaximized)
- {
- if (this.MaximizeBox && this.MinimizeBox)
- {
- if (this.WindowState == FormWindowState.Normal)
- {
- this.WindowState = FormWindowState.Maximized;
- }
- else
- {
- this.WindowState = FormWindowState.Normal;
- }
- }
- }
- }
- catch { }
- }
- #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)
- {
- try
- {
- 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:
- try
- {
- base.WndProc(ref m);
- }
- catch { }
- break;
- }
- }
- catch {
- try
- {
- base.WndProc(ref m);
- }
- catch { }
- }
- }
- else
- {
- try
- {
- base.WndProc(ref m);
- }
- catch { }
- }
- }
- #endregion
- protected override void CreateHandle()
- {
- if (!IsHandleCreated)
- {
- try
- {
- base.CreateHandle();
- }
- catch { }
- finally
- {
- if (!IsHandleCreated)
- {
- try
- {
- base.RecreateHandle();
- }
- catch { }
- }
- }
- }
- }
- }
- }
|