【问题标题】:How can I test the mouse right button drag?如何测试鼠标右键拖动?
【发布时间】:2019-08-20 15:43:52
【问题描述】:

这个测试鼠标右键点击。 但是现在我想测试当我单击并按住鼠标右键并拖动鼠标时。

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 Mouse_Test
{
    public partial class Form1 : Form
    {
        private int numClicks = 0;

        public Form1()
        {
            InitializeComponent();

            label1.Text = "0";
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Right)
            {
                numClicks++;
                label1.Text = numClicks.ToString();
            }
        }
    }
}

例如,只有在按住鼠标右键并拖动鼠标时才能在 form1 上画一条线。

【问题讨论】:

标签: c# winforms


【解决方案1】:

这是我想要用于测试的有效解决方案。似乎当按住鼠标右键并拖动鼠标时,它几乎根本不工作而不是绘图。但是,如果我按住鼠标左键并拖动鼠标,它的绘制很流畅。所以问题出在鼠标右键上。

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

namespace Mouse_Test
{
    public partial class Form1 : Form
    {
        ArrayList listOfPoints;
        bool PencilDown;

        public Form1()
        {
            InitializeComponent();

            listOfPoints = new ArrayList();
            PencilDown = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Left || e.Button == MouseButtons.Right)
            {
                Point p = new Point(e.X, e.Y);
                listOfPoints.Add(p);
                PencilDown = true;
                this.statusStrip1.Items[0].Text = "MouseDown";
            }
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            Graphics g = this.CreateGraphics();
            Point points = new Point(e.X, e.Y);
            Pen pencil = new Pen(Color.White);

            if (e.Button == MouseButtons.Left)
            {
                pencil = new Pen(Color.BlueViolet);
            }

            if(e.Button == MouseButtons.Right)
            {
                pencil = new Pen(Color.Red);
            }

            if(PencilDown)
            {
                this.statusStrip1.Items[0].Text = "MouseMove";
                if(listOfPoints.Count > 1)
                {
                    g.DrawLine(pencil, (Point)listOfPoints[listOfPoints.Count - 1], points);
                }
                listOfPoints.Add(points);
            }
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            PencilDown = false;
            this.statusStrip1.Items[0].Text = "MouseUp";
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {

        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多