Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

CSharp MouseMove can't give Real Time Mouse Location in Window Form

$
0
0

Dear All,

I am looking for to get the comparisonal result of mouse move events for single and double click events.

Here is my draft and some working code for your review

namespace CSharp
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// Is range?
        /// </summary>
        private bool flag = false;
        /// <summary>
        /// Is in the range?
        /// </summary>
        private bool isInRangeX = false, isInRangeY = false;
        bool isInRangeofBox = false;

        private PictureBox pictureBox1;
        /// <summary>
        /// Allows Single click or not?
        /// </summary>
        private bool isallowsingleclick = true;
        bool bDoubleClick  = false;
        Image img;
        Graphics g;
        int xLoc, yLoc;
        static Rectangle region = new Rectangle();
        bool bMouseDown = true, bDrawn = true;


        public Form1()
        {
            InitializeComponent();
            string strImg = "Jellyfish.jpg";
            img = Image.FromFile(strImg);
            pictureBox1.Image = img;
        }

        [STAThread]
        public static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
        
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            //isInRange = e.Location.X <= 300 && e.Location.Y <= 200;
            //isInRange = e.Location.X <= region.Location.X + 200 && e.Location.Y <= region.Location.X + 100;
            isInRangeX = region.Location.X < e.Location.X && e.Location.X <= region.Location.X + 200;
            isInRangeY = region.Location.Y < e.Location.Y && e.Location.Y <= region.Location.Y + 200;
            Debug.WriteLine(isInRangeX);
            Debug.WriteLine(isInRangeY);
        }

        private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            //if ((isInRange || isInRangeofBox) && flag)
            if (isInRangeX && isInRangeY && flag)
            {
                //MessageBox.Show("You have double click the range, and in the range the double click returns to work!");
                Debug.WriteLine("You have double click the range, and in the range the double click returns to work!");
                isallowsingleclick = true;
                bMouseDown = false;
                //bDoubleClick = true;
            }
            else
            {
                //MessageBox.Show("You have double click out of the range, and in the range the double click fails to work!");
                Debug.WriteLine("You have double click out of the range, and in the range the double click fails to work!");
                isallowsingleclick = false;
                bMouseDown = true;
            }
        }

        private void InitializeComponent()
        {
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // pictureBox1
            // 
            this.pictureBox1.Location = new System.Drawing.Point(12, 12);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(600, 450);
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            this.pictureBox1.DoubleClick += new System.EventHandler(this.pictureBox1_DoubleClick);
            this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
            this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
            this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
            this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
            // 
            // Form1
            // 
            this.ClientSize = new System.Drawing.Size(627, 478);
            this.Controls.Add(this.pictureBox1);
            this.Name = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);

        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
           
            if (bMouseDown)
            {
                xLoc = e.X;
                yLoc = e.Y;

                region.Location = new Point(e.X, e.Y);
                region.Size = new Size(200, 100);
              
                Debug.WriteLine("Single Click!");
            }
           else
           {
               if (bDrawn && isInRangeX && isInRangeY && isallowsingleclick)
                {
                    flag = true;
                    bMouseDown = false;
                }
            }
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            Pen p = new Pen(Color.Green);
            p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;

            //if (bMouseDown)
            {
                e.Graphics.DrawRectangle(p, region);
                pictureBox1.Refresh();
                bDrawn = true;
                if (isInRangeX)
                {
                    //bMouseDown = false;
                }
              
            }
                  
        }
     
    }
}

I would like to have

1. single click on image

2. then rectangle came out so far ok

3. i want to have double click right only the rectangle area but mouse move is not updated real time

4. now outside and inside area got double click right for the time being and most of time.

The issume seem like mouse move event and i hope some one will advise me more.

Thanks and best regards


Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>