Adorners for C # Windows Forms

I have a canvas (control panel) in my WinForms application, where users can drag things like text fields, labels, etc. But I want it easier for them to orient objects. I read about it, and apparently Alonners is the way to go? But apparently this is only for WPF. WPF, unfortunately, is not an option for me.

What I'm trying to accomplish is to create lines that appear every time a user drags an object around on the canvas ... How they work in the Designer Windows Forms view.

I would appreciate any help.

Thank.

+3
source share
4 answers

Thank you all for your answers.


. . "", ...

Label l = (Label)sender;
foreach (Control control in Canvas.Controls)
{
    if (l.Location.X > control.Location.X + control.Size.Width && l.Location.X < control.Location.X + control.Size.Width + 5)
        l.Location = new Point(control.Location.X + control.Size.Width + 5, l.Location.Y);
    else if (l.Location.X < control.Location.X - l.Size.Width && l.Location.X > control.Location.X - l.Size.Width - 5)
        l.Location = new Point(control.Location.X - l.Size.Width - 5, l.Location.Y);
    else if (l.Location.Y > control.Location.Y + control.Size.Height && l.Location.Y < control.Location.Y + control.Size.Height + 5)
        l.Location = new Point(l.Location.X, control.Location.Y + control.Size.Height + 5);
    else if (l.Location.Y < control.Location.Y - control.Size.Height && l.Location.Y > control.Location.Y - control.Size.Height - 5)
        l.Location = new Point(l.Location.X, l.Location.Y - 5);

    this.Update();
}

Control_MouseMove, , , , .

, 5 , , .

+3

:

- :

left = (left/10)*10;
top = (top/10)*10;

. Form Desginer, .

+2

. DrawReversibleLine MSDN. , .

bool AllowResize;
bool DoTracking;  

private void MyControl_MouseDown(object sender, MouseEventArgs e)
{
    if (AllowResize)
    {
        DoTracking = true;
                ControlPaint.DrawReversibleFrame(new Rectangle(this.PointToScreen(new Point(1,1)),
        this.Size), Color.DarkGray, FrameStyle.Thick);
    }
}

, , , , . , .. , MyControl_MouseUp ControlPaint.DrawReversibleFrame(...) . , . - , . , .

, , , InitializeComponents();

//  To reduce redraw flicker
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
+1

, , (GroupBox, Panel ..)?

If so, I think DisplayRectangle is what you need. You change it to the rectangle that you want other controls to snap to. For example, I have a GroupBox control, and I set DisplayRectangle as follows:

public override Rectangle DisplayRectangle
{
    get
    {
        return Rectangle.FromLTRB(base.DisplayRectangle.Left,
            base.DisplayRectangle.Top + Font.Height + 4,
            base.DisplayRectangle.Right,
            base.DisplayRectangle.Bottom);
    }
}

Now, any control that I place as a child will be bound to this rectangle when I drag it to the edges.

NTN!

+1
source

Source: https://habr.com/ru/post/1732423/


All Articles