0

就像在 Visual Studio 中,假设是 ToolBox,它有一个蓝色的可拖动 WindowBar,如下所示:

工具箱

或像这样:

垂直握把

是否有一个 DLL 来获得一个,或者一种简单的方法来制作它?

4

2 回答 2

2

要使某些控件看起来像某些系统元素,例如握把,您可以使用合适的VisualStyleRenderer

正如你所看到的,有一个巨大的数字!- 以下是如何将VisualStyleElement.Rebar.Gripper添加到Panel

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

结果:

在此处输入图像描述

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

请注意,其中有两个GripperVerticalGripper; 在我的系统(W10)上它们看起来一样,但在其他系统上它们可能不一样!

如果你真的想要一个自定义的握把样式,你可以用合适的填充图案画笔来绘制它;在所有系统中看起来都一样,这可能是您想要的。但这也意味着它不会总是与其他窗口集成;此解决方案将始终使用当前机器的样式。

更新:

如果您想允许拖动控件,您可以使用 Vanethrane 的基本功能答案。为了获得更好的用户体验,还请务必考虑以下几点:

  • 使用所有三个事件,MouseDown, -Move and -Up
  • Cursor将from更改DefaultHandandSizeAll
  • 测试你是否在握把区域
  • 在移动之前将控件带到z 顺序的顶部,BringToFront避免在任何其他控件下通过
  • MouseDown存储当前位置两次;一次移动,一次恢复,以防最终位置无效
  • 通常您想使用网格来控制最终位置,并且...
  • ..通常您希望控件与最近的邻居“磁性”对齐。用于MouseUp相应地更改最终位置..

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

于 2018-09-30T19:29:16.103 回答
0

超级容易。这里:

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

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

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

于 2018-09-30T18:36:05.553 回答