lundi 28 décembre 2015

c# if bool not working

I have an interesting issue here. I'm trying to make a simple rubber band selection in C# and I have everything working except for a weird bug if I don't make a selection.

Expected result: MouseDown/MouseUp without moving mouse and no selection should be made after moving the mouse.

Actual result: MouseDown/MouseUp without moving mouse, then when I mouse the mouse, the selection is drawn as I move my mouse. If I click, the selection disappears. Then if I click again and mouse the mouse, the new selection is continuously redrawn.

I added a label to my form to check the state of the bool. When I do this, everything works as expected. However, once I comment out the label1.Text line, it's no longer working as expected. I've tried adding multiple bools to resolve said issue but no avail.

So why is the bool not being recognized unless I set the label text? P.S. I'm a newbie to C# so I welcome any criticism. I pulled this code from http://ift.tt/1OpUEns

Here is my current code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RubberBand
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private Bitmap m_Orig = null;
        private int X0, X1, Y0, Y1;
        private bool sArea, isDown, hasMoved = false;
        private Bitmap sImg = null;
        private Graphics sGraph = null;

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

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 27)
            {
                if (!sArea) return;
                sArea = false;
                isDown = false;
                hasMoved = false;

                sImg = null;
                sGraph = null;
                pictureBox1.Image = m_Orig;
                pictureBox1.Refresh();
            }
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            sArea = true;
            isDown = true;
            //label1.Text = isDown.ToString();
            X0 = e.X;
            Y0 = e.Y;

            sImg = new Bitmap(m_Orig);
            sGraph = Graphics.FromImage(sImg);
            pictureBox1.Image = sImg;
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (!sArea) return;
            hasMoved = true;
            //label1.Text = isDown.ToString();

            if (isDown)
            {
                X1 = e.X;
                Y1 = e.Y;

                sGraph.DrawImage(m_Orig, 0, 0);

                using (Pen select_pen = new Pen(Color.Red))
                {
                    select_pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
                    Rectangle rect = MakeRectangle(X0, Y0, X1, Y1);
                    sGraph.DrawRectangle(select_pen, rect);
                }

                pictureBox1.Refresh();
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (!sArea) return;
            if (!hasMoved) return;

            sArea = false;
            hasMoved = false;
            isDown = false;
            //label1.Text = isDown.ToString();
            sImg = null;
            sGraph = null;
            pictureBox1.Image = m_Orig;
            pictureBox1.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));
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire