如何设计或以某种方式获得自定义 WindowBar 和 grip
How to design or somehow get a custom WindowBar and grip
就像在 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;
}
}
请注意,这将移动单个控件。要移动整个表单,您需要在表单本身上实现它
要使某些控件看起来像某些系统元素,例如手柄,您可以使用合适的 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) 上它们看起来一样,但在其他系统上它们可能不一样!
如果你真的想要一个自定义的抓握样式,你可以用合适的阴影图案画笔来绘制它;这在所有系统中看起来都一样,这可能就是您想要的。但这也意味着它不会总是与 windows 的其余部分集成;此解决方案将始终使用当前机器的样式。
更新:
如果您想允许拖动控件,您可以使用 Vanethrane 的基本功能答案。为了获得更好的用户体验,请务必考虑以下几点:
- 使用所有三个事件,
MouseDown, -Move and -Up
。
- 将
Cursor
从 Default
更改为 Hand
和 SizeAll
- 测试你是否在抓握区域
- 在移动之前,将控件移至 z 顺序的顶部 并
BringToFront
以避免在任何其他控件下传递
- 在
MouseDown
中存储当前位置两次;一次用于移动,一次用于恢复,以防最终位置为无效
- 通常您想使用 网格 来控制最终位置并且...
- ..通常你想让控件对齐本身'magnetically'与最近的邻居。使用
MouseUp
相应地更改最终位置..
我建议将所有功能捆绑到一个 DraggableControl
class.
就像在 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;
}
}
请注意,这将移动单个控件。要移动整个表单,您需要在表单本身上实现它
要使某些控件看起来像某些系统元素,例如手柄,您可以使用合适的 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) 上它们看起来一样,但在其他系统上它们可能不一样!
如果你真的想要一个自定义的抓握样式,你可以用合适的阴影图案画笔来绘制它;这在所有系统中看起来都一样,这可能就是您想要的。但这也意味着它不会总是与 windows 的其余部分集成;此解决方案将始终使用当前机器的样式。
更新:
如果您想允许拖动控件,您可以使用 Vanethrane 的基本功能答案。为了获得更好的用户体验,请务必考虑以下几点:
- 使用所有三个事件,
MouseDown, -Move and -Up
。 - 将
Cursor
从Default
更改为Hand
和SizeAll
- 测试你是否在抓握区域
- 在移动之前,将控件移至 z 顺序的顶部 并
BringToFront
以避免在任何其他控件下传递 - 在
MouseDown
中存储当前位置两次;一次用于移动,一次用于恢复,以防最终位置为无效 - 通常您想使用 网格 来控制最终位置并且...
- ..通常你想让控件对齐本身'magnetically'与最近的邻居。使用
MouseUp
相应地更改最终位置..
我建议将所有功能捆绑到一个 DraggableControl
class.