简体   繁体   中英

How to detect when holding the mouse down in the mousedown event and then draw pixels randomly around the current mouse cursor location?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;

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

            pictureBox1.Image = Properties.Resources._image;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

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

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            label1.Text = String.Format("X: {0}; Y: {1}", e.X, e.Y);
            Bitmap bmp = new Bitmap(pictureBox1.Image);
            bmp.SetPixel(e.X, e.Y, Color.Red);
            pictureBox1.Image = bmp;
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {

        }
    }
}

now when i'm moving the mouse around it's drawing pixels over the pictureBox1 image.

what i want to add is in the MouseDown event first to detect when holding down the mouse. then when the mouse is holding down draw many pixels randomly around the current mouse cursor location where it's holding down.

not to fill it around but to draw many pixels around like creating a block of pixels around the mouse cursor. and not rectangle shape just let's call it a "block" of pixels that will be created in randomly positions around the current mouse location.

when the mouse is up in the MouseUp event it will be called a block. block or call it area of pixels around the current mouse location in random locations but the random location have to be around the current mouse location and not that far from it.

lets decide the block area should be 20,20 around the mouse location something like that i mean that will be the area size around the mouse location to create the random locations of the pixels.

For testing the part of detecting mouse holding down i did this in the mousedown event:

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            heldDown= true;
            while(heldDown)
            {
                Thread.Sleep(500);
                progressBar1.Value = progressBar1.Value + 1;
            }
        }

the problem is that it keep adding percentages to the progressBar1 even if i leave the mouse and in the mouseup event i set the heldDown flag to false.

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            heldDown= false;
        }

You don't actually need MouseDown as you can start it from MouseMove , BUT you need to decorate the handler with async so that it will actually allow the MouseUp event to toggle your flag. You'll also use await Task.Delay() instead of Thread.Sleep() .

Looks something like:

private async void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        heldDown = true;
        while (heldDown)
        {
            // ... do something in here ...
            await Task.Delay(250);
        }
    }
}

You can get the current cursor position with Cursor.Position , which is in SCREEN coords, and see if it falls within the bounds of the PictureBox. Convert the PictureBox CLIENT coords to screen coords and see if the cursor is within:

private async void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        heldDown = true;
        while (heldDown)
        {
            Point pt = Cursor.Position;
            Rectangle rc = pictureBox1.RectangleToScreen(pictureBox1.ClientRectangle);
            if (rc.Contains(pt))
            {
                // ... do something ...
            }
            await Task.Delay(250);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM