0

I am trying to make a re-sizable square box with my mouse but if I drag my mouse fast it's very noticeable that it's offset from where it should be eg, https://i.stack.imgur.com/5Lnev.jpg (Red is where it should be, black is where it is.)

using System.Drawing;
using System.Windows.Forms;
using System;
using System.Drawing.Drawing2D;

namespace WindowsFormsApp6
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
            TransparencyKey = Color.SaddleBrown;
            canvas.MouseDown += new MouseEventHandler(canvas_MouseDown);
            canvas.MouseUp += new MouseEventHandler(canvas_MouseUp);
            canvas.MouseMove += new MouseEventHandler(canvas_MouseMove);
            Load += new EventHandler(Form1_Load);
            //typeof(Panel).InvokeMember("DoubleBuffered", BindingFlags.SetProperty| BindingFlags.Instance | BindingFlags.NonPublic, null,canvas, new object[] { true });

        }
        private Bitmap m_OriginalImage = null;
        private int X0, Y0, X1, Y1;
        private bool SelectingArea = false;
        private Bitmap SelectedImage = null;
        private Graphics SelectedGraphics = null;

        private void Form1_Load(object sender, EventArgs e)
        {
            m_OriginalImage = new Bitmap(canvas.Image);
            KeyPreview = true;
        }

        private void canvas_MouseDown(object sender, MouseEventArgs e)
        {
            SelectingArea = true;
            X0 = e.X;
            Y0 = e.Y;

            SelectedImage = new Bitmap(m_OriginalImage);
            SelectedGraphics = Graphics.FromImage(SelectedImage);
            canvas.Image = SelectedImage;
        }


        private void canvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (!SelectingArea) return;

            X1 = e.X;
            Y1 = e.Y;

            SelectedGraphics.DrawImage(m_OriginalImage, 0, 0);

            using (Pen select_pen = new Pen(Color.Red))
            {
                select_pen.DashStyle = DashStyle.Dash;
                Rectangle rect = MakeRectangle(X0, Y0, X1, Y1);
                //ControlPaint.DrawReversibleFrame(rect,this.BackColor, FrameStyle.Dashed);
                SelectedGraphics.DrawRectangle(select_pen, rect);
            }
            canvas.Refresh();
            }
        private void canvas_MouseUp(object sender, MouseEventArgs e)
        {
            if (!SelectingArea) return;

            SelectingArea = false;
            SelectedImage = null;
            SelectedGraphics = null;
            canvas.Image = m_OriginalImage;
            canvas.Refresh();

            Rectangle rect = MakeRectangle(X0, Y0, X1, Y1);
            if ((rect.Width > 0) && (rect.Height > 0))
            {
                MessageBox.Show(rect.ToString());
            }
        }
        private Rectangle MakeRectangle(int x0, int y0, int x1, int y1)
        {
            return new Rectangle(
                Math.Min(x0, x1),
                Math.Min(y0, y1),
                Math.Abs(x0 - x1),
                Math.Abs(y0 - y1));
        }
    }
}

I have tried everything I could think from timers to threads.

Any suggestions to keep it at 60 fps would be appreciated greatly.

Hello World
  • 168
  • 6
  • how about drawing in `OnPaint` method? you're drawing in move event and call refresh every time. – Lei Yang Jul 07 '17 at 03:09
  • I do not quite understand how that event works and how to use it. Would it be difficult to introduce into the current code if I do try and learn it? – Hello World Jul 07 '17 at 03:50
  • almost all code can reuse. it's worth learning. see [msdn Can not draw rectangle smoothly using GDI+ with mouse moving](https://social.msdn.microsoft.com/Forums/vstudio/en-US/23423d22-08cb-4f7a-96bd-232786916702/can-not-draw-rectangle-smoothly-using-gdi-with-mouse-moving?forum=netfxbcl) and [How to draw rectangle on MouseDown/Move c#](https://stackoverflow.com/questions/4060446/how-to-draw-rectangle-on-mousedown-move-c-sharp) – Lei Yang Jul 07 '17 at 04:31
  • I just got the rectangle drawing inside of it but sadly it's still looking like 30fps – Hello World Jul 07 '17 at 04:32
  • paste your latest code then – Lei Yang Jul 07 '17 at 04:33
  • https://pastebin.com/9dbBZHmN It's not a final product for the paint event but it does still lag behind the cursor. – Hello World Jul 07 '17 at 04:34
  • you'd better try out the samples in those links i just provided. your latest code is in the wrong way. (you should **only** draw in paint event) – Lei Yang Jul 07 '17 at 04:37
  • Well it's much faster now after forgetting to remove the drawing code inside the move event. Thanks – Hello World Jul 07 '17 at 04:43

0 Answers0