C#鼠标事件

程序在微软官网复制测试

鼠标相关事件介绍链接(https://docs.microsoft.com/zh-cn/dotnet/framework/winforms/mouse-events-in-windows-forms)

C#鼠标事件_第1张图片

获取鼠标点击位置可分为两种(容器内相对位置、相对于整个软件的位置)

 

1、容器内的相对位置

这个事件中的e:private void pictureBox1_MouseMove(object sender, MouseEventArgs e)

在使用X/Y坐标时,e.X 和 e.Y

 

根据原文试一下第一个MouseDown

示例的鼠标点击处理链接(https://docs.microsoft.com/zh-cn/dotnet/api/system.windows.forms.control.mouseup?view=netframework-4.8)

1、新建windows窗体应用程序

2、删除Form1.cs这个文件

3、复制微软示例程序并覆盖替换Program.cs,之后就可以直接运行测试

 

当鼠标指针位于控件上并按下鼠标键时发生。

public event System.Windows.Forms.MouseEventHandler MouseDown;

1、添加事件

this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseDown);

2、实现事件处理内容,使用函数输入的“e“来读取相关参数(获取的X,Y是相对于panel1的,Button则是鼠标点了什么,MouseButtons.XButton1/2为鼠标的拓展按钮)

        private void panel1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) 
        {
            // Update the mouse path with the mouse information,获取指针位置
            Point mouseDownLocation = new Point(e.X, e.Y);

            string eventString = null;
            // 确定鼠标是点击哪个触发的
            switch (e.Button) {
                case MouseButtons.Left:
                    eventString = "L";
                    break;
                case MouseButtons.Right:
                    eventString = "R";
                    break;
                case MouseButtons.Middle:
                    eventString = "M";
                    break;
                case MouseButtons.XButton1:
                    eventString = "X1";
                    break;
                case MouseButtons.XButton2:
                    eventString = "X2";
                    break;
                case MouseButtons.None:
                default:
                    break;
            }

            if (eventString != null) 
            {
                // 显示string、字体、显示形式(这里加粗)、位置、文本布局
                mousePath.AddString(eventString, FontFamily.GenericSerif, (int)FontStyle.Bold, fontSize, mouseDownLocation, StringFormat.GenericDefault);
            }
            else 
            {
                mousePath.AddLine(mouseDownLocation,mouseDownLocation);
            }
            panel1.Focus();
            panel1.Invalidate();
        }

 

其他一些咯,别在意

// 保存图片
pictureBox1.Image.Save("F:/documents/visual studio 2015/Projects/绘图/cc.jpg");
        // 拖动面板时,内置内容大小跟随改变
        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {
            groupBox1.Width = splitContainer1.Panel1.Width;
            groupBox1.Height = Convert.ToInt32(splitContainer1.Panel1.Height * 0.5);
            groupBox2.Width = splitContainer1.Panel1.Width;
            groupBox2.Height = Convert.ToInt32(splitContainer1.Panel1.Height * 0.5);
        }
// 打开图片显示        
        private void 打开文件_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.InitialDirectory = "G:/下载/恐怖图片.jpeg";
            openFileDialog.Filter = "Bitmap files (*.bmp)|*.bmp|Jpeg files (*.jpg)|*.jpg|All valid files (*.bmp/*.jpg)|*.bmp/*.jpg| All Files|*.*";
            openFileDialog.FilterIndex = 2;
            openFileDialog.RestoreDirectory = true;

            if (DialogResult.OK == openFileDialog.ShowDialog())
            {
                m_Bitmap = (Bitmap)Bitmap.FromFile(openFileDialog.FileName, false);
                this.AutoScroll = true;
                this.AutoScrollMinSize = new Size((int)(m_Bitmap.Width * Zoom), (int)(m_Bitmap.Height * Zoom));
                this.Invalidate();

                pictureBox1.Width = m_Bitmap.Width;
                pictureBox1.Height = m_Bitmap.Height;
                pictureBox1.Image = m_Bitmap;
                groupBox3.Width = pictureBox1.Width;
                groupBox3.Height = pictureBox1.Height;

                //Graphics graphics = groupBox3.CreateGraphics();
                graphics = Graphics.FromImage(m_Bitmap);
            }
        }
        // 按下鼠标后,移动的时候绘图
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if(key == Key.move)
            {
                // Update the mouse path with the mouse information
                Point mouseDownLocation = new Point(e.X, e.Y);

                string eventString = null;
                switch (e.Button)
                {
                    case MouseButtons.Left:
                        eventString = "L";
                        break;
                    case MouseButtons.Right:
                        eventString = "R";
                        break;
                    case MouseButtons.Middle:
                        eventString = "M";
                        break;
                    case MouseButtons.XButton1:
                        eventString = "X1";
                        break;
                    case MouseButtons.XButton2:
                        eventString = "X2";
                        break;
                    case MouseButtons.None:
                    default:
                        break;
                }

                if (eventString != null)
                {
                    mousePath.AddString(eventString, FontFamily.GenericSerif, (int)FontStyle.Bold, 20, mouseDownLocation, StringFormat.GenericDefault);

                    // 添加到图片的里面
                    if (lastPoint.X != -1 || lastPoint.Y != -1)
                        Graphics.FromImage(pictureBox1.Image).DrawLine(pen, lastPoint, mouseDownLocation);
                    lastPoint = mouseDownLocation;

                    listPoint.Add(mouseDownLocation);
                }
                else
                {
                    //mousePath.AddLine(mouseDownLocation, mouseDownLocation);
                }
            }
            
            // Cursor.Position是相对于当前运行软件左上角开始计算,而事件中e则是相对于触发事件的pictureBox1左上角 
            //mousePosition.Text = "X|Y:" + Convert.ToString(Cursor.Position.X) + "," + Convert.ToString(Cursor.Position.Y);
            mousePosition.Text = "X|Y:" + Convert.ToString(e.X) + "," + Convert.ToString(e.Y);
            pictureBox1.Focus();
            pictureBox1.Invalidate();
        }
        // 可以重绘,但当前的鼠标点击处理事件没有响应,上一个鼠标位置事件也没有响应
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e) {
            // Update the mouse path with the mouse information
            Point mouseDownLocation = new Point(e.X, e.Y);
            
            switch (key)
            {
                case Key.line:
                    if (lastPoint.X != -1)
                    {
                        Graphics.FromImage(pictureBox1.Image).DrawLine(pen, lastPoint, mouseDownLocation);
                        lastPoint.X = -1;
                    }
                    else
                    {
                        lastPoint.X = e.X;
                        lastPoint.Y = e.Y;
                    }
                    break;
                case Key.arc:
                    if (lastPoint.X != -1)
                    {
                        double db = Math.Sqrt(Math.Pow(e.X - lastPoint.X, 2) + Math.Pow(e.Y - lastPoint.Y, 2));
                        int size = Convert.ToInt32(db);
                        Point point = new Point(lastPoint.X - size, lastPoint.Y - size);
                        Graphics.FromImage(pictureBox1.Image).DrawArc(pen, new Rectangle(point, new Size(size * 2, size * 2)), 0, 360);
                        lastPoint.X = -1;
                    }
                    else
                    {
                        lastPoint.X = e.X;
                        lastPoint.Y = e.Y;
                    }
                    break;
                case Key.fillEllepse:
                    if (lastPoint.X != -1)
                    {
                        double db = Math.Sqrt(Math.Pow(e.X - lastPoint.X, 2) + Math.Pow(e.Y - lastPoint.Y, 2));
                        int size = Convert.ToInt32(db);
                        Point point = new Point(lastPoint.X - size, lastPoint.Y - size);
                        Brush brush = new SolidBrush(Color.Red);
                        Graphics.FromImage(pictureBox1.Image).FillEllipse(brush, new Rectangle(point, new Size(size * 2, size * 2)));
                        lastPoint.X = -1;
                    }
                    else
                    {
                        lastPoint.X = e.X;
                        lastPoint.Y = e.Y;
                    }
                    break;
                case Key.ellepse:
                    if (lastPoint.X != -1)
                    {
                        double db = Math.Sqrt(Math.Pow(e.X - lastPoint.X, 2) + Math.Pow(e.Y - lastPoint.Y, 2));
                        int size = Convert.ToInt32(db);
                        Point point = new Point(lastPoint.X - size, lastPoint.Y - size);
                        Graphics.FromImage(pictureBox1.Image).DrawArc(pen, new Rectangle(point, new Size(size * 2, size * 2)), 0, 360);
                        lastPoint.X = -1;
                    }
                    else
                    {
                        lastPoint.X = e.X;
                        lastPoint.Y = e.Y;
                    }
                    break;
                case Key.rectangle:
                    if (lastPoint.X != -1)
                    {
                        Graphics.FromImage(pictureBox1.Image).DrawRectangle(pen, lastPoint.X, lastPoint.Y, e.X - lastPoint.X, e.Y - lastPoint.Y);
                        lastPoint.X = -1;
                    }
                    else
                    {
                        lastPoint.X = e.X;
                        lastPoint.Y = e.Y;
                    }
                    break;
                case Key.pie:
                    break;
                case Key.move:
                    lastPoint.X = e.X;
                    lastPoint.Y = e.Y;
                    break;
                case Key.text:
                    Graphics.FromImage(pictureBox1.Image).DrawString(CharsInput.Str, new Font("宋体", 12), Brushes.Black, new PointF(e.X, e.Y));
                    break;

                default:
                    break;
            }
        }

 

你可能感兴趣的:(C#)