3

Morning,

I'm just about ready to claw my own eyes out at this point. I'm building a basic image editor using Windows forms on .NET 3.5, and what I need is a 'select tool'. This tool needs to appear when a button is clicked and will be a fixed size, it needs to be a drag and dropable rectangle with a transparent center.

The purpose of this is to act almost like a 'Picture frame' in that the user can drag and drop the rectangle over a portion of the image and hit another button to snapshot whatever is inside the rectangle at that point. (Please note: I don't want a rubber band rectangle, it has to be a fixed size, draggable across the form and transparent).

I've spent a couple of days scouring the internet and this site looking for possible solutions, none of which have been any use. I have managed to make a control draggable - but this poses problems with transparency. Below is the code which makes a control draggable, but I'm not sure this is the right path to take.

 class ControlMover
{
    public enum Direction
    {
        Any,
        Horizontal,
        Vertical
    }

    public static void Init(Control control)
    {
        Init(control, Direction.Any);
    }

    public static void Init(Control control, Direction direction)
    {
        Init(control, control, direction);
    }

    public static void Init(Control control, Control container, Direction direction)
    {
        EditorForm.blnSelectArea = true;
        bool Dragging = false;
        Point DragStart = Point.Empty;
        control.MouseDown += delegate(object sender, MouseEventArgs e)
        {
            Dragging = true;
            DragStart = new Point(e.X, e.Y);
            control.Capture = true;
        };
        control.MouseUp += delegate(object sender, MouseEventArgs e)
        {
            Dragging = false;
            control.Capture = false;
        };
        control.MouseMove += delegate(object sender, MouseEventArgs e)
        {
            if (Dragging)
            {
                if (direction != Direction.Vertical)
                    container.Left = Math.Max(0, e.X + container.Left - DragStart.X);
                if (direction != Direction.Horizontal)
                    container.Top = Math.Max(0, e.Y + container.Top - DragStart.Y);

                control.Invalidate();

            }
        };
    }
}  

Can anyone point me in the right direction or make a suggestion as to where to look.

Many Thanks

Adam H
  • 1,159
  • 3
  • 15
  • 27

3 Answers3

6

I actualy made an aplication for screen capturing that worked the way you describe, to make it draggable i use Mouse events. To make it transparent i simply made another Form control with semi-transparent png Image as background image.

public partial class Photo : Form
    {
        public delegate void ScreenShotReadyDelegate(Bitmap g);
        public event ScreenShotReadyDelegate ScreenShotReady;

        bool Moving = false;
        Point oldLoc = new Point();

        public Photo()
        {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.None;
            this.DoubleBuffered = true;
            this.SetStyle(ControlStyles.ResizeRedraw, true);
        } 

        private void Photo_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.BackgroundImage = null;
            this.Invalidate();
            Rectangle bounds = this.Bounds;
            using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
            {
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
                }
                ScreenShotReady(bitmap);
            }
            this.BackgroundImage = Properties.Resources.rect;
            this.Invalidate();
        }

        private void Photo_MouseDown(object sender, MouseEventArgs e)
        {
            this.Moving = true;
            this.oldLoc = MousePosition;
        }

        private void Photo_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.Moving)
            {
                Point vector = new Point(MousePosition.X - this.oldLoc.X, MousePosition.Y - this.oldLoc.Y);
                this.Location = new Point(this.Location.X + vector.X, this.Location.Y + vector.Y);
                this.oldLoc = MousePosition;
            }
        }

        private void Photo_MouseUp(object sender, MouseEventArgs e)
        {
            this.Moving = false;
        }
    }
Milan Halada
  • 1,943
  • 18
  • 28
2

You should be able to after reading this Painting On Top Of Child Controls

I have used ControlPaint.DrawReversibleFrame(_FrameRect, Color.Black, FrameStyle.Dashed); myself before and it works fine :)

radbyx
  • 9,352
  • 21
  • 84
  • 127
  • I appreciate your answer but this is a non draggable rubber band rectangle. I'm looking for a rectangle I can set to be a fixed size and then click within and drag around the page. Cheers for the effort. – Adam H Feb 21 '12 at 09:23
  • 1
    What if you first draw it, and then when you in your delegates detect some draging, you first clean the old rectangle and then draw a new one? hmm do you want to drag the rectangle or the page? I don't know what you mean by page – radbyx Feb 21 '12 at 09:28
  • The accepted answer supplied a solution, thanks for your input anyway! :) – Adam H Feb 21 '12 at 09:35
  • 3
    It's cool i'm just glad if I could be at any help with the short time I had. :) – radbyx Feb 21 '12 at 09:41
0

You would be better off to make a custom control which draws an image, and overlays the rectangle on top. Then, you could handle mouse events to 'move' the rectangle.

Some pseudocode to get you started:

class cropcontrol

onpaint
  paint image
  paint rectangle using rect location

onmousemove
  if mouse down
    update rect location
clearpath
  • 916
  • 8
  • 24