Winforms: A draggable transparent rectangle

后端 未结 3 813
误落风尘
误落风尘 2020-12-30 16:59

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 \'sele

相关标签:
3条回答
  • 2020-12-30 17:49

    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 :)

    0 讨论(0)
  • 2020-12-30 17:50

    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
    
    0 讨论(0)
  • 2020-12-30 18:00

    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;
            }
        }
    
    0 讨论(0)
提交回复
热议问题