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

2021-02-08 05:16

阅读:489

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

技术图片

  /// 
  /// 标题栏控件管理
  /// 
  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;
    }

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

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

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

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

    /// 
    /// 关闭按钮Rect
    /// 
    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;
      }
    }

    /// 
    /// 最大按钮Rect
    /// 
    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;
      }
    }

    /// 
    /// 最小化按钮Rect
    /// 
    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;
      }
    }

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

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

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

    /// 
    /// 
    /// 
    /// 
    /// 
    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;
      }
    }

    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    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;
        }
      }

    }

    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    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;
      }

    }

    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    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();
    }

    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    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();
      }
    }

    /// 
    /// 重新绘制窗体控件指定rect
    /// 
    /// 
    private void Invalidate(Rectangle rect)
    {
      this._owner.Invalidate(rect);
    }

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

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

    /// 
    /// 
    /// 
    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


评论


亲,登录后才可以留言!