An easy way for a user to resize a control?

3 posts, 0 answers
  1. NOAH
    NOAH avatar
    2 posts
    Member since:
    Jan 2014

    Posted 13 Jan 2014 Link to this post

    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. NOAH
    NOAH avatar
    2 posts
    Member since:
    Jan 2014

    Posted 14 Jan 2014 Link to this post

    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. 
  3. UI for WinForms is Visual Studio 2017 Ready
  4. George
    Admin
    George avatar
    500 posts

    Posted 15 Jan 2014 Link to this post

    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 >>
Back to Top