Winforms: A draggable transparent rectangle

c#, winforms

Solution

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

Problem

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

Original source