0

Like in Visual Studio, let's say the ToolBox, it has a blue draggable WindowBar like this:

ToolBox

or like this:

VerticalGrip

Is there a DLL to get one, or an easy way to make it?

Yakov .P
  • 88
  • 8
  • You may want to look [here](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.visualstyles.visualstyleelement.rebar.gripper?view=netframework-4.7.2) – TaW Sep 30 '18 at 19:13
  • OP asks for an 'easy way' to create a grip on a control. VisualStyleRenderer is included in .NET just for these adornments. Question is 100% on topic! – TaW Oct 01 '18 at 04:17

2 Answers2

3

To render some control to look like some system element, like a a grip, you can use a suitable VisualStyleRenderer

As you can see there is a huge number! - Here is how you would add a VisualStyleElement.Rebar.Gripper to a Panel:

private void panel1_Paint(object sender, PaintEventArgs e)
{
    // any other drawing before..
    DrawVisualStyleElementRebarGripper1(e);
}

Here is a typical implementation of the method to call:

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

Result:

enter image description here

Make sure to call the rendering method after any other paint action so it won't get painted over

Note that are two of them: GripperVertical and Gripper; on my system (W10) they look the same but on other systems they may not!

If you actually want a custom grip style you could paint it with a suitable hatchpattern brush; that would look the same across all systems, which may be what you want. But it would also mean that it will not always integrate with the rest of windows; this solution will always use the style of the current machine.

Update:

If you want to allow dragging the control you can use Vanethrane's answer for the basic functionality. For better UX also make sure to consider these points:

  • Use all three events, MouseDown, -Move and -Up.
  • Change the Cursor from Default to Hand and SizeAll
  • Test if you are in the grip area
  • Before moving bring the control to the top of the z-order with BringToFront to avoid passing under any other control
  • In the MouseDown store the current position twice; once for moving and once for restoring in case the final location is invalid
  • often you want to use a grid to control the final position and...
  • ..often you want to have the control align itself 'magnetically' with the closest neighbour. Use MouseUp to change the final position accordingly..

I suggest to bundle all the functionality into a DraggableControl class.

TaW
  • 53,122
  • 8
  • 69
  • 111
0

super easy. here:

create the desired control, name it grip. put these in the mouse down and mouse move methods respectively

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

note, that this will move a single control. to move the whole form you need to implement this on the form itself

Technivorous
  • 1,682
  • 2
  • 16
  • 22
  • Thanks though I Didn't want to know how to drag it. I wanted to know how to make a grip itself (the design) – Yakov .P Sep 30 '18 at 18:43