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

Posted tlmbem

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c#美化窗体----------WinForm控件开发系列相关的知识,希望对你有一定的参考价值。

技术图片

  /// <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控件开发系列的主要内容,如果未能解决你的问题,请参考以下文章

C# winform 界面美化技巧(扁平化设计) (转)

winform窗体如何美化界面

vs2010用 IrisSkin2.dll皮肤控件美化WINFORM窗体 不显示皮肤也不报错 这个是啥原因呢

winform 自已做皮肤

WinForm 窗体属性 窗体美化

实现WinForm窗体的美化(借助第三方控件)