2008-11-20 39 views
5

我希望能够创建一个黑色自定义窗口(带有边框和控件),就像作为表情混合,Twirl或Adobe Lightroom的一部分提供的那样。Winforms - 如何创建自定义窗口边框并关闭/最小化按钮?

是否有创建所有者绘制窗口的最佳实践方法?

平台:C#和WindowsForms(所有版本)

+0

这里是使用面板创建自定义窗体表单的文章 - [使用面板在C#中创建自定义Windows窗体](http://www.codeproject.com/Articles/1068043/Creating-Custom-Windows-Forms-in -Charrp-using-Pane) – 2016-02-01 07:56:39

+0

[WinForms应用中的自定义标题栏/ chrome]的可能重复(http://stackoverflow.com/questions/42460/custom-titlebars-chrome-in-a-winforms-app) – 2016-05-21 04:56:04

回答

4

如果自定义铬工具不能为您提供与您想要的外观和感觉,这种事情是很容易在C#中做自己。基本上,您可以创建一个无边界的窗体(FormBorderStyle = None),然后通过将控件放置在需要它们的位置(标题栏的标签,关闭和最小化的命令按钮等)和/或绘图来自己创建所有控件和边框直接使用Graphics对象在表单的表面上。

您还必须实现代码以允许表单被其“假”标题栏拖动(有关如何执行此操作的示例,请参见this answer)。您可能还必须实现自己的调整大小机制(如果需要可调整大小的表单)。

最后,尽管自定义表单代码可能有点笨拙,但您可以在单个表单上实现它,然后让应用程序中的所有其他表单继承此表单,这使其成为一种非常有用的自定义技术 - 整个应用程序的皮肤。

+0

老实说一个非常公平的方法 – 2017-03-28 16:46:27

2

我的任务是使活动窗口更明显,明亮 - 比其他人,应用程序的非活动窗口。应用程序有很多打开的窗口,一些模式,一些无模式 - 和MDI的父窗口。

你可以使用类似not-a-border的东西 - 客户区内的一个框架。这里是代码片段,基类的一部分(可以直接在一个形式使用):该类的

#region Кастомизированное поведение - рамки, активность и т.д. 
    private bool isCurrentlyActive = false; 
    private bool childControlsAreHandled = false; 
    private Pen activeWindowFramePen, inactiveWindowFramePen; 
    private Point[] framePoints; 

    private void AddControlPaintHandler(Control ctrl) 
    { 
     ctrl.Paint += DrawWindowFrame; 
     if (ctrl.Controls != null) 
     { 
      foreach (Control childControl in ctrl.Controls) 
      { 
       AddControlPaintHandler(childControl); 
      } 
     } 
    } 

    protected override void OnActivated(EventArgs e) 
    { 
     base.OnActivated(e); 
     if ((this.childControlsAreHandled == false) 
      && (WindowFrameType != Forms.WindowFrameType.NoFrame) 
      && (this.MdiParent == null)) 
     { 
      RecalculateWindowFramePoints(); 
      AddControlPaintHandler(this); 
      this.childControlsAreHandled = true; 
     } 

     this.isCurrentlyActive = true; 
     if (InactiveWindowOpacity < 1) 
     { 
      base.Opacity = 1; 
     } 
     base.Invalidate(true); 
    } 

    protected override void OnDeactivate(EventArgs e) 
    { 
     base.OnDeactivate(e); 
     this.isCurrentlyActive = false; 
     if (InactiveWindowOpacity < 1) 
     { 
      base.Opacity = InactiveWindowOpacity; 
     } 
     base.Invalidate(true); 
    } 

    protected override void OnResizeEnd(EventArgs e) 
    { 
     base.OnResizeEnd(e); 
     this.framePoints = null; 
     RecalculateWindowFramePoints(); 
     this.Invalidate(true); 
    } 

    private Pen ActivePen 
    { 
     get 
     { 
      if (this.isCurrentlyActive) 
      { 
       if (this.activeWindowFramePen == null) 
       { 
        this.activeWindowFramePen = new Pen(Color.FromArgb((int)(WindowFrameOpacity*255), WindowFrameActiveColor), WindowFrameSize * 2); 
       } 
       return this.activeWindowFramePen; 
      } 
      else 
      { 
       if (this.inactiveWindowFramePen == null) 
       { 
        this.inactiveWindowFramePen = new Pen(Color.FromArgb((int)(WindowFrameOpacity*255), WindowFrameInactiveColor), WindowFrameSize * 2); 
       } 
       return this.inactiveWindowFramePen; 
      } 
     } 
    } 

    private Point[] RecalculateWindowFramePoints() 
    { 
     if ((WindowFrameType == Forms.WindowFrameType.AllSides) 
      && (this.framePoints != null) 
      && (this.framePoints.Length != 5)) 
     { 
      this.framePoints = null; 
     } 
     if ((WindowFrameType == Forms.WindowFrameType.LeftLine) 
      && (this.framePoints != null) 
      && (this.framePoints.Length != 2)) 
     { 
      this.framePoints = null; 
     } 
     if (this.framePoints == null) 
     { 
      switch (WindowFrameType) 
      { 
       case Forms.WindowFrameType.AllSides: 
        this.framePoints = new Point[5] 
        { 
         new Point(this.ClientRectangle.X, this.ClientRectangle.Y), 
         new Point(this.ClientRectangle.X + this.ClientRectangle.Width, this.ClientRectangle.Y), 
         new Point(this.ClientRectangle.X + this.ClientRectangle.Width, this.ClientRectangle.Y + this.ClientRectangle.Height), 
         new Point(this.ClientRectangle.X, this.ClientRectangle.Y + this.ClientRectangle.Height), 
         new Point(this.ClientRectangle.X, this.ClientRectangle.Y) 
        }; 
        break; 
       case Forms.WindowFrameType.LeftLine: 
        this.framePoints = new Point[2] 
        { 
         new Point(this.ClientRectangle.X, this.ClientRectangle.Y), 
         new Point(this.ClientRectangle.X, this.ClientRectangle.Y + this.ClientRectangle.Height) 
        }; 
        break; 
      } 
     } 
     return this.framePoints; 
    } 

    private void DrawWindowFrame(object sender, PaintEventArgs e) 
    { 
     if (WindowFrameType == Forms.WindowFrameType.NoFrame) 
     { 
      return; 
     } 
     if ((this.framePoints == null) || (this.framePoints.Length == 0)) 
     { 
      return; 
     } 
     Control ctrl = (Control)(sender); 
     // пересчитаем точки в координатах контрола. 
     List<Point> pts = new List<Point>(); 
     foreach (var p in this.framePoints) 
     { 
      pts.Add(ctrl.PointToClient(this.PointToScreen(p))); 
     } 
     e.Graphics.DrawLines(ActivePen, pts.ToArray()); 
    } 

    public static int WindowFrameSize = 2; 
    public static WindowFrameType WindowFrameType = Forms.WindowFrameType.NoFrame; 
    public static Color WindowFrameActiveColor = Color.YellowGreen; 
    public static Color WindowFrameInactiveColor = SystemColors.ControlDark; 
    public static double InactiveWindowOpacity = 1.0; 
    public static double WindowFrameOpacity = 0.3; 
    #endregion 

静态字段从应用程序设置形式(类)初始化 - 因此,所有在形式该应用程序具有相同的行为。

希望对某人有帮助。

相关问题