就像在 Visual Studio 中,假设是 ToolBox,它有一个蓝色的可拖动 WindowBar,如下所示:
或像这样:
是否有一个 DLL 来获得一个,或者一种简单的方法来制作它?
要使某些控件看起来像某些系统元素,例如握把,您可以使用合适的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));
}
结果:
确保在任何其他绘制操作之后调用渲染方法,这样它就不会被覆盖
请注意,其中有两个:GripperVertical
和Gripper
; 在我的系统(W10)上它们看起来一样,但在其他系统上它们可能不一样!
如果你真的想要一个自定义的握把样式,你可以用合适的填充图案画笔来绘制它;在所有系统中看起来都一样,这可能是您想要的。但这也意味着它不会总是与其他窗口集成;此解决方案将始终使用当前机器的样式。
更新:
如果您想允许拖动控件,您可以使用 Vanethrane 的基本功能答案。为了获得更好的用户体验,还请务必考虑以下几点:
MouseDown, -Move and -Up
。Cursor
将from更改Default
为Hand
andSizeAll
BringToFront
避免在任何其他控件下通过MouseDown
存储当前位置两次;一次移动,一次恢复,以防最终位置无效MouseUp
相应地更改最终位置..我建议将所有功能捆绑到一个DraggableControl
类中。
超级容易。这里:
创建所需的控件,将其命名为夹点。将这些分别放在鼠标按下和鼠标移动方法中
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;
}
}
请注意,这将移动单个控件。要移动整个表单,您需要在表单本身上实现它