C# 如何设计或以某种方式获得自定义窗口条和夹点

C# 如何设计或以某种方式获得自定义窗口条和夹点,c#,visual-studio,winforms,material-design,visualdesigner,C#,Visual Studio,Winforms,Material Design,Visualdesigner,就像Visual Studio中的工具箱一样,它有一个蓝色的可拖动窗口条,如下所示: 或者像这样: 是否有一个DLL来获取一个,或者有一个简单的方法来实现它?超级简单。在这里: 创建所需控件,将其命名为grip。将这些分别放在鼠标下和鼠标移动方法中 private Point Mouselocation; private void grip_MouseDown(object sender, MouseEventArgs e) { if (e.Button

就像Visual Studio中的工具箱一样,它有一个蓝色的可拖动窗口条,如下所示:

或者像这样:

是否有一个DLL来获取一个,或者有一个简单的方法来实现它?

超级简单。在这里:

创建所需控件,将其命名为grip。将这些分别放在鼠标下和鼠标移动方法中

 private Point Mouselocation;


    private void grip_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            Mouselocation = e.Location;
        }
    }

    private void grip_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            grip.Left = e.X + grip.Left - Mouselocation.X;
            grip.Top = e.Y + grip.Top - Mouselocation.Y;
        }
    }
请注意,这将移动单个控件。要移动整个表单,您需要在表单本身上实现这一点。在这里:

创建所需控件,将其命名为grip。将这些分别放在鼠标下和鼠标移动方法中

 private Point Mouselocation;


    private void grip_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            Mouselocation = e.Location;
        }
    }

    private void grip_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            grip.Left = e.X + grip.Left - Mouselocation.X;
            grip.Top = e.Y + grip.Top - Mouselocation.Y;
        }
    }

请注意,这将移动单个控件。要移动整个表单,您需要在表单本身上实现此操作

要使某些控件看起来像某个系统元素,如夹点,您可以使用合适的

正如你所看到的,这是一个巨大的数字下面是如何将添加到
面板中的方法:

private void panel1_Paint(object sender, PaintEventArgs e)
{
    // any other drawing before..
    DrawVisualStyleElementRebarGripper1(e);
}
下面是要调用的方法的典型实现:

public void DrawVisualStyleElementRebarGripper1(PaintEventArgs e)
{
    if (VisualStyleRenderer.IsElementDefined(
        VisualStyleElement.Rebar.Gripper.Normal))
    {
        VisualStyleRenderer renderer =
                new VisualStyleRenderer(VisualStyleElement.Rebar.GripperVertical.Normal);
        Rectangle rectangle1 = new Rectangle(0, 0, 
                                            20,  (int)e.Graphics.VisibleClipBounds.Height);
        renderer.DrawBackground(e.Graphics, rectangle1);
    }
    //else
    //    e.Graphics.DrawString("This element is not defined in the current visual style.",
    //            this.Font, Brushes.Black, new Point(10, 10));
}
结果:

确保在任何其他绘制操作之后调用渲染方法,这样它就不会被绘制

请注意,它们中有两个:
gripvertical
Gripper
;在我的系统(W10)上,它们看起来相同,但在其他系统上可能不同

如果您真的想要自定义夹点样式,可以使用合适的hatchpattern笔刷进行绘制;这在所有系统中看起来都是一样的,这可能是您想要的。但这也意味着它不会总是与windows的其余部分集成;此解决方案将始终使用当前机器的样式

更新:

如果要允许拖动控件,可以使用凡士兰的答案来实现基本功能。对于更好的用户体验,也要考虑以下几点:

  • 使用所有三个事件,
    MouseDown、-Move和-Up
  • 光标
    默认
    更改为
    手动
    SizeAll
  • 测试您是否处于夹持区域
  • 移动前,使用
    BringToFront
    将控件移动到z顺序的顶部,以避免在任何其他控件下通过
  • 鼠标向下
    中,将当前位置存储两次;一次用于移动,一次用于恢复,如果最终位置无效
  • 通常,您希望使用栅格来控制最终位置和
  • ..通常,您希望控件与最近的相邻控件对齐。使用
    MouseUp
    相应地更改最终位置

我建议将所有功能捆绑到一个
DraggableControl
类中

要使一些控件看起来像某个系统元素,像一个夹点,可以使用合适的

正如你所看到的,这是一个巨大的数字下面是如何将添加到
面板中的方法:

private void panel1_Paint(object sender, PaintEventArgs e)
{
    // any other drawing before..
    DrawVisualStyleElementRebarGripper1(e);
}
下面是要调用的方法的典型实现:

public void DrawVisualStyleElementRebarGripper1(PaintEventArgs e)
{
    if (VisualStyleRenderer.IsElementDefined(
        VisualStyleElement.Rebar.Gripper.Normal))
    {
        VisualStyleRenderer renderer =
                new VisualStyleRenderer(VisualStyleElement.Rebar.GripperVertical.Normal);
        Rectangle rectangle1 = new Rectangle(0, 0, 
                                            20,  (int)e.Graphics.VisibleClipBounds.Height);
        renderer.DrawBackground(e.Graphics, rectangle1);
    }
    //else
    //    e.Graphics.DrawString("This element is not defined in the current visual style.",
    //            this.Font, Brushes.Black, new Point(10, 10));
}
结果:

确保在
任何其他绘制操作之后调用渲染方法,这样它就不会被绘制

请注意,它们中有两个:
gripvertical
Gripper
;在我的系统(W10)上,它们看起来相同,但在其他系统上可能不同

如果您真的想要自定义夹点样式,可以使用合适的hatchpattern笔刷进行绘制;这在所有系统中看起来都是一样的,这可能是您想要的。但这也意味着它不会总是与windows的其余部分集成;此解决方案将始终使用当前机器的样式

更新:

如果要允许拖动控件,可以使用凡士兰的答案来实现基本功能。对于更好的用户体验,也要考虑以下几点:

  • 使用所有三个事件,
    MouseDown、-Move和-Up
  • 光标
    默认
    更改为
    手动
    SizeAll
  • 测试您是否处于夹持区域
  • 移动前,使用
    BringToFront
    将控件移动到z顺序的顶部,以避免在任何其他控件下通过
  • 鼠标向下
    中,将当前位置存储两次;一次用于移动,一次用于恢复,如果最终位置无效
  • 通常,您希望使用栅格来控制最终位置和
  • ..通常,您希望控件与最近的相邻控件对齐。使用
    MouseUp
    相应地更改最终位置

我建议将所有功能捆绑到一个
DraggableControl
类中

谢谢,尽管我不想知道如何拖动它。虽然我不想知道如何拖动它,但我想知道如何制作把手本身(设计)。我想知道如何制作一个夹点本身(设计),你可能想看看,OP需要一种“简单的方法”来在控件上创建夹点。VisualStyleRenderer包含在.NET中,仅用于这些装饰。问题是100%的主题!您可能需要寻找一种“简单方法”来在控件上创建夹点。VisualStyleRenderer包含在.NET中,仅用于这些装饰。问题是100%的主题!谢谢,很有魅力:)注意我添加的更新!美好的顺便说一句,我意识到,如果你只是制作一个带有夹点的画框,然后用凡士兰的答案让它拖拉起来,可能会更容易。是的,这只是关于外观。你可以在同一个控件中组合这两个答案来获得外观和行为……是的,我做了。它很有效,谢谢。谢谢,就像一个符咒:)注意我添加的更新!美好的顺便说一下,我意识到可能