码迷,mamicode.com
首页 > Windows程序 > 详细

c#美化窗体----------WinForm控件开发系列

时间:2019-08-17 12:58:38      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:关闭   private   skin   manager   按钮   ted   orm   idt   opera   

技术图片

  /// <summary>
  /// 标题栏控件管理
  /// </summary>
  internal class ControlBoxManager : IDisposable
  {
    private SkinForm _owner;
    private ControlBoxState _closeBoxState;
    private ControlBoxState _minimizeBoxState;
    private ControlBoxState _maximizeBoxState;
    private bool _mouseDown;

    public ControlBoxManager(SkinForm owner)
    {
      this._owner = owner;
    }

    /// <summary>
    /// 是否显示按钮栏
    /// </summary>
    public bool ControlBoxVisibale
    {
      get { return this._owner.ControlBox; }
    }

    /// <summary>
    /// 是否显示关闭按钮
    /// </summary>
    public bool CloseBoxVisibale
    {
      get { return this._owner.ControlBox && this._owner.CloseBox; }
    }

    /// <summary>
    /// 是否显示最小化按钮
    /// </summary>
    public bool MinimizeBoxVisibale
    {
      get { return this._owner.ControlBox && this._owner.MinimizeBox; }
    }

    /// <summary>
    /// 是否显示最大化按钮
    /// </summary>
    public bool MaximizeBoxVisibale
    {
      get { return this._owner.ControlBox && this._owner.MaximizeBox; }
    }

    /// <summary>
    /// 关闭按钮Rect
    /// </summary>
    public Rectangle CloseBoxRect
    {
      get
      {
        if (this.CloseBoxVisibale)
        {
          int x = this._owner.Width - this._owner.CloseBoxSize.Width - this._owner.CloseBoxTopRight.X;
          return new Rectangle(x, this._owner.CloseBoxTopRight.Y, this._owner.CloseBoxSize.Width, this._owner.CloseBoxSize.Height);
        }
        return Rectangle.Empty;
      }
    }

    /// <summary>
    /// 最大按钮Rect
    /// </summary>
    public Rectangle MaximizeBoxRect
    {
      get
      {
        if (this.MaximizeBoxVisibale)
        {
          int x = this._owner.Width - this._owner.MaximizeBoxSize.Width - this._owner.MaximizeBoxTopRight.X;
          if (this.CloseBoxVisibale)
          {
            x = x - this._owner.CloseBoxSize.Width - this._owner.CloseBoxTopRight.X;
          }
          return new Rectangle(x, this._owner.MaximizeBoxTopRight.Y, this._owner.MaximizeBoxSize.Width, this._owner.MaximizeBoxSize.Height);
        }
        return Rectangle.Empty;
      }
    }

    /// <summary>
    /// 最小化按钮Rect
    /// </summary>
    public Rectangle MinimizeBoxRect
    {
      get
      {
        if (this.MinimizeBoxVisibale)
        {
          int x = this._owner.Width - this._owner.MinimizeBoxSize.Width - this._owner.MinimizeBoxTopRight.X;
          if (this.CloseBoxVisibale)
          {
            x = x - this._owner.CloseBoxSize.Width - this._owner.CloseBoxTopRight.X;
          }
          if (this.MaximizeBoxVisibale)
          {
            x = x - this._owner.MaximizeBoxSize.Width - this._owner.MaximizeBoxTopRight.X;
          }
          return new Rectangle(x, this._owner.MinimizeBoxTopRight.Y, this._owner.MinimizeBoxSize.Width, this._owner.MinimizeBoxSize.Height);
        }
        return Rectangle.Empty;
      }
    }

    /// <summary>
    /// 标题栏控件状态
    /// </summary>
    public ControlBoxState CloseBoxState
    {
      get { return this._closeBoxState; }
      protected set
      {
        if (this._closeBoxState != value)
        {
          this._closeBoxState = value;
          if (_owner != null)
          {
            this.Invalidate(this.CloseBoxRect);
          }
        }
      }
    }

    /// <summary>
    /// 最大按钮状态
    /// </summary>
    public ControlBoxState MaximizeBoxState
    {
      get { return this._maximizeBoxState; }
      protected set
      {
        if (this._maximizeBoxState != value)
        {
          this._maximizeBoxState = value;
          if (_owner != null)
          {
            this.Invalidate(this.MaximizeBoxRect);
          }
        }
      }
    }

    /// <summary>
    /// 最小化状态
    /// </summary>
    public ControlBoxState MinimizeBoxState
    {
      get { return this._minimizeBoxState; }
      protected set
      {
        if (this._minimizeBoxState != value)
        {
          this._minimizeBoxState = value;
          if (_owner != null)
          {
            this.Invalidate(this.MinimizeBoxRect);
          }
        }
      }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="mousePoint"></param>
    /// <param name="operate"></param>
    public void ProcessMouseOperate(Point mousePoint, ControlBoxBoxStatus operate)
    {
      if (!this._owner.ControlBox)
      {
        return;
      }

      Rectangle closeBoxRect = this.CloseBoxRect;
      Rectangle minimizeBoxRect = this.MinimizeBoxRect;
      Rectangle maximizeBoxRect = this.MaximizeBoxRect;

      bool closeBoxVisibale = this.ControlBoxVisibale;
      bool minimizeBoxVisibale = this.MinimizeBoxVisibale;
      bool maximizeBoxVisibale = this.MaximizeBoxVisibale;

      switch (operate)
      {
        case ControlBoxBoxStatus.Move:
          this.ProcessMouseMove(mousePoint, closeBoxRect, minimizeBoxRect, maximizeBoxRect, closeBoxVisibale, minimizeBoxVisibale, maximizeBoxVisibale);
          break;
        case ControlBoxBoxStatus.Down:
          this.ProcessMouseDown(mousePoint, closeBoxRect, minimizeBoxRect, maximizeBoxRect, closeBoxVisibale, minimizeBoxVisibale, maximizeBoxVisibale);
          break;
        case ControlBoxBoxStatus.Up:
          this.ProcessMouseUP(mousePoint, closeBoxRect, minimizeBoxRect, maximizeBoxRect, closeBoxVisibale, minimizeBoxVisibale, maximizeBoxVisibale);
          break;
        case ControlBoxBoxStatus.Leave:
          this.ProcessMouseLeave(closeBoxVisibale, minimizeBoxVisibale, maximizeBoxVisibale);
          break;
        case ControlBoxBoxStatus.Hover:
          break;
      }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="mousePoint"></param>
    /// <param name="closeBoxRect"></param>
    /// <param name="minimizeBoxRect"></param>
    /// <param name="maximizeBoxRect"></param>
    /// <param name="closeBoxVisibale"></param>
    /// <param name="minimizeBoxVisibale"></param>
    /// <param name="maximizeBoxVisibale"></param>
    private void ProcessMouseDown(Point mousePoint, Rectangle closeBoxRect, Rectangle minimizeBoxRect, Rectangle maximizeBoxRect, bool closeBoxVisibale, bool minimizeBoxVisibale, bool maximizeBoxVisibale)
    {
      this._mouseDown = true;

      if (closeBoxVisibale)
      {
        if (closeBoxRect.Contains(mousePoint))
        {
          this.CloseBoxState = ControlBoxState.Down;
          return;
        }
      }

      if (maximizeBoxVisibale)
      {
        if (maximizeBoxRect.Contains(mousePoint))
        {
          this.MaximizeBoxState = ControlBoxState.Down;
          return;
        }
      }

      if (minimizeBoxVisibale)
      {
        if (minimizeBoxRect.Contains(mousePoint))
        {
          this.MinimizeBoxState = ControlBoxState.Down;
          return;
        }
      }

    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="mousePoint"></param>
    /// <param name="closeBoxRect"></param>
    /// <param name="minimizeBoxRect"></param>
    /// <param name="maximizeBoxRect"></param>
    /// <param name="closeBoxVisibale"></param>
    /// <param name="minimizeBoxVisibale"></param>
    /// <param name="maximizeBoxVisibale"></param>
    private void ProcessMouseUP(Point mousePoint, Rectangle closeBoxRect, Rectangle minimizeBoxRect, Rectangle maximizeBoxRect, bool closeBoxVisibale, bool minimizeBoxVisibale, bool maximizeBoxVisibale)
    {
      this._mouseDown = false;

      if (closeBoxVisibale)
      {
        if (closeBoxRect.Contains(mousePoint))
        {
          if (this.CloseBoxState == ControlBoxState.Down)
          {
            this._owner.Close();
            this.CloseBoxState = ControlBoxState.Normal;
            return;
          }
        }
        this.CloseBoxState = ControlBoxState.Normal;
      }

      if (maximizeBoxVisibale)
      {
        if (maximizeBoxRect.Contains(mousePoint))
        {
          if (this.MaximizeBoxState == ControlBoxState.Down)
          {
            if (this._owner.WindowState == FormWindowState.Maximized)
            {
              this._owner.WindowState = FormWindowState.Normal;
            }
            else
            {
              this._owner.WindowState = FormWindowState.Maximized;
            }
            this.MaximizeBoxState = ControlBoxState.Normal;
            return;
          }
        }
        this.MaximizeBoxState = ControlBoxState.Normal;
      }

      if (minimizeBoxVisibale)
      {
        if (minimizeBoxRect.Contains(mousePoint))
        {
          if (this.MinimizeBoxState == ControlBoxState.Down)
          {
            this._owner.WindowState = FormWindowState.Minimized;
            this.MinimizeBoxState = ControlBoxState.Normal;
            return;
          }
        }
        this.MinimizeBoxState = ControlBoxState.Normal;
      }

    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="closeBoxVisibale"></param>
    /// <param name="minimizeBoxVisibale"></param>
    /// <param name="maximizeBoxVisibale"></param>
    private void ProcessMouseLeave(bool closeBoxVisibale, bool minimizeBoxVisibale, bool maximizeBoxVisibale)
    {
      if (closeBoxVisibale)
      {
        if (this.CloseBoxState == ControlBoxState.Down)
        {
          this.CloseBoxState = ControlBoxState.Up;
        }
        else
        {
          this.CloseBoxState = ControlBoxState.Normal;
        }
      }

      if (maximizeBoxVisibale)
      {
        if (this.MaximizeBoxState == ControlBoxState.Down)
        {
          this.MaximizeBoxState = ControlBoxState.Up;
        }
        else
        {
          this.MaximizeBoxState = ControlBoxState.Normal;
        }
      }

      if (minimizeBoxVisibale)
      {
        if (this.MinimizeBoxState == ControlBoxState.Down)
        {
          this.MinimizeBoxState = ControlBoxState.Up;
        }
        else
        {
          this.MinimizeBoxState = ControlBoxState.Normal;
        }
      }

      this.HideToolTip();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="mousePoint"></param>
    /// <param name="closeBoxRect"></param>
    /// <param name="minimizeBoxRect"></param>
    /// <param name="maximizeBoxRect"></param>
    /// <param name="closeBoxVisibale"></param>
    /// <param name="minimizeBoxVisibale"></param>
    /// <param name="maximizeBoxVisibale"></param>
    private void ProcessMouseMove(Point mousePoint, Rectangle closeBoxRect, Rectangle minimizeBoxRect, Rectangle maximizeBoxRect, bool closeBoxVisibale, bool minimizeBoxVisibale, bool maximizeBoxVisibale)
    {
      string text = string.Empty;
      bool hide = true;
      if (closeBoxVisibale)
      {
        if (closeBoxRect.Contains(mousePoint))
        {
          hide = false;
          if (!_mouseDown)
          {
            if (CloseBoxState != ControlBoxState.Enter)
            {
              text = "关闭";
            }
            CloseBoxState = ControlBoxState.Enter;
          }
          else
          {
            if (CloseBoxState == ControlBoxState.Up)
            {
              CloseBoxState = ControlBoxState.Down;
            }
          }
        }
        else
        {
          if (!_mouseDown)
          {
            CloseBoxState = ControlBoxState.Normal;
          }
          else
          {
            if (CloseBoxState == ControlBoxState.Down)
            {
              CloseBoxState = ControlBoxState.Up;
            }
          }
        }
      }

      if (maximizeBoxVisibale)
      {
        if (maximizeBoxRect.Contains(mousePoint))
        {
          hide = false;
          if (!_mouseDown)
          {
            if (MaximizeBoxState != ControlBoxState.Enter)
            {
              bool maximize =
                  this._owner.WindowState == FormWindowState.Maximized;
              text = maximize ? "还原" : "最大化";
            }
            MaximizeBoxState = ControlBoxState.Enter;
          }
          else
          {
            if (MaximizeBoxState == ControlBoxState.Up)
            {
              MaximizeBoxState = ControlBoxState.Down;
            }
          }
        }
        else
        {
          if (!_mouseDown)
          {
            MaximizeBoxState = ControlBoxState.Normal;
          }
          else
          {
            if (MaximizeBoxState == ControlBoxState.Down)
            {
              MaximizeBoxState = ControlBoxState.Up;
            }
          }
        }
      }

      if (minimizeBoxVisibale)
      {
        if (minimizeBoxRect.Contains(mousePoint))
        {
          hide = false;
          if (!_mouseDown)
          {
            if (MinimizeBoxState != ControlBoxState.Enter)
            {
              text = "最小化";
            }
            MinimizeBoxState = ControlBoxState.Enter;
          }
          else
          {
            if (MinimizeBoxState == ControlBoxState.Up)
            {
              MinimizeBoxState = ControlBoxState.Down;
            }
          }
        }
        else
        {
          if (!_mouseDown)
          {
            MinimizeBoxState = ControlBoxState.Normal;
          }
          else
          {
            if (MinimizeBoxState == ControlBoxState.Down)
            {
              MinimizeBoxState = ControlBoxState.Up;
            }
          }
        }
      }

      if (text != string.Empty)
      {
        HideToolTip();
        ShowTooTip(text);
      }

      if (hide)
      {
        HideToolTip();
      }
    }

    /// <summary>
    /// 重新绘制窗体控件指定rect
    /// </summary>
    /// <param name="rect"></param>
    private void Invalidate(Rectangle rect)
    {
      this._owner.Invalidate(rect);
    }

    /// <summary>
    /// 显示提示
    /// </summary>
    /// <param name="toolTipText"></param>
    private void ShowTooTip(string toolTipText)
    {
      if (this._owner != null)
      {
        this._owner.ToolTip.Active = true;
        this._owner.ToolTip.SetToolTip(this._owner, toolTipText);
      }
    }

    /// <summary>
    /// 隐藏提示
    /// </summary>
    private void HideToolTip()
    {
      if (this._owner != null)
      {
        this._owner.ToolTip.Active = false;
      }
    }

    /// <summary>
    /// 
    /// </summary>
    public void Dispose()
    {
      this._owner = null;
    }

  }

 源码下载地址:CSharp窗体美化.zip

c#美化窗体----------WinForm控件开发系列

标签:关闭   private   skin   manager   按钮   ted   orm   idt   opera   

原文地址:https://www.cnblogs.com/tlmbem/p/11258324.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!