This is a migrated thread and some comments may be shown as answers.

An easy way for a user to resize a control?

2 Answers 104 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
NOAH
Top achievements
Rank 1
NOAH asked on 13 Jan 2014, 06:41 PM
I am working on a Winform program, and I can't believe there isn't an easier way to accomplish what I'm trying. 

Basically, I want to display an image that will always be 640x480.  Then, I want to have another control on top of it that is nothing more than a selection area that the user can change its size & location inside that 640x480 area, and it will report back its pixel size & x/y location. 

Short of drawing all of the lines by hand and adding grips by hand, I can't seem to find an easy way to do this.  Am I overlooking something? 

2 Answers, 1 is accepted

Sort by
0
NOAH
Top achievements
Rank 1
answered on 14 Jan 2014, 03:00 PM
Apparently what I'm looking for is referred to by Microsoft as a Rubber Band Rectangle...  I still have not found a simple solution to use a control for this. 
0
George
Telerik team
answered on 15 Jan 2014, 09:19 AM
Hi Noah,

Thank you for contacting us.

We do not have such a control in our suite so far. We do have a feature request for image editor control which seems to be related. If you find it useful, feel free to vote for it here: http://www.telerik.com/support/pits.aspx#/details/Issue=15947.

However, you can easily implement a custom control which can support the Rubber Band Selection. I have made a small example of such control. Here is the code for it:
[Browsable(true)]
public class RubberBandSelectionControl : RadControl
{
    private RubberBandSelectionElement rubberBandSelectionElement;
 
    protected override void CreateChildItems(RadElement parent)
    {
        this.rubberBandSelectionElement = new RubberBandSelectionElement();
        parent.Children.Add(this.rubberBandSelectionElement);
    }
 
    protected override Size DefaultSize
    {
        get
        {
            return new Size(200, 300);
        }
    }
}
 
public class RubberBandSelectionElement : LightVisualElement
{
    private bool isMouseDown;
    private Point startingPoint;
    private Point currentPoint;
 
    protected override void OnMouseDown(MouseEventArgs e)
    {
        base.OnMouseDown(e);
 
        if (e.Button == MouseButtons.Left)
        {
            this.isMouseDown = true;
            this.startingPoint = Control.MousePosition;  
        }
    }
 
    protected override void OnMouseUp(MouseEventArgs e)
    {
        base.OnMouseUp(e);
 
        this.isMouseDown = false;
        this.currentPoint = Point.Empty;
    }
 
    protected override void OnMouseMove(MouseEventArgs e)
    {
        base.OnMouseMove(e);
        if (this.isMouseDown)
        {
            if (this.currentPoint != Point.Empty)
            {
                this.DrawRectangle(this.startingPoint, this.currentPoint);
            }
 
            this.currentPoint = Control.MousePosition;
 
            this.DrawRectangle(this.startingPoint, this.currentPoint);
        }
    }
 
    private void DrawRectangle(Point startingPoint, Point endingPoint)
    {
        Rectangle rect = new Rectangle();
 
        if (startingPoint.X < endingPoint.X)
        {
            rect.X = startingPoint.X;
            rect.Width = endingPoint.X - startingPoint.X;
        }
        else
        {
            rect.X = endingPoint.X;
            rect.Width = startingPoint.X - endingPoint.X;
        }
        if (startingPoint.Y < endingPoint.Y)
        {
            rect.Y = startingPoint.Y;
            rect.Height = endingPoint.Y - startingPoint.Y;
        }
        else
        {
            rect.Y = endingPoint.Y;
            rect.Height = startingPoint.Y - endingPoint.Y;
        }
 
        ControlPaint.DrawReversibleFrame(rect, Color.Black, FrameStyle.Dashed);
    }
}

You can drag it on a form, setup its BackgroudImage property and start making rectangles. 

I hope this helps.

Regards,
George
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
General Discussions
Asked by
NOAH
Top achievements
Rank 1
Answers by
NOAH
Top achievements
Rank 1
George
Telerik team
Share this question
or