【问题标题】:Winforms - How do I create a custom windows border and close/minimise buttons? [closed]Winforms - 如何创建自定义窗口边框和关闭/最小化按钮? [关闭]
【发布时间】:2008-11-20 16:33:04
【问题描述】:

我希望能够创建一个黑色自定义窗口(带有边框和控件),就像作为表达式混合、Twirl 或 Adob​​e Lightroom 的一部分提供的那样。

如何创建自绘窗口?

【问题讨论】:

标签: c# windows winforms ownerdrawn


【解决方案1】:

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

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

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

【讨论】:

  • 老实说一个非常公平的方法
【解决方案2】:

我的任务是使活动窗口比其他应用程序的非活动窗口更引人注目、更明亮。应用有许多打开的窗口,一些是模态的,一些是非模态的——还有 MDI 父窗口。

您可以使用非边框之类的东西 - 客户区域内的框架。这里是代码sn-p,是基类的一部分(可以直接在表单中使用):

    #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

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

希望对某人有所帮助。

【讨论】:

  • 在哪里可以找到这种类型的WindowFrameType??
【解决方案3】:
猜你喜欢
  • 1970-01-01
  • 2013-04-17
  • 2011-04-26
  • 2012-11-01
  • 2012-12-16
  • 1970-01-01
  • 2022-11-11
  • 2022-11-11
  • 1970-01-01
相关资源
最近更新 更多