⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 实现用鼠标在屏幕上写字
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
namespace MousePicture
{
    public partial class Form1 : Form
    {
        private System.Drawing.Point startPoint;
        private System.Drawing.Bitmap bitmap;
        private bool CanMove = false;

        public Form1()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            bitmap = new Bitmap(this.pictureBox2.Width, this.pictureBox2.Height);
            this.pictureBox2.Image = bitmap;

        }

        private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
        {
            this.CanMove = true;
            this.startPoint = new Point(e.X, e.Y);

        }

        private void pictureBox2_MouseUp(object sender, MouseEventArgs e)
        {
            this.pictureBox2.Image = this.bitmap;
            using (Graphics graphics = Graphics.FromImage(this.pictureBox2.Image))
            {
                graphics.DrawLine(new Pen(Color.Red, 3), this.startPoint, new Point(e.X, e.Y));
            }
            this.CanMove = false;

        }
        private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.CanMove == true)
            {
                this.pictureBox2.Image = (Bitmap)this.bitmap.Clone();
                using (Graphics graphics = Graphics.FromImage(this.pictureBox2.Image))
                {
                    graphics.Clear(Color.Transparent);//清除
                    graphics.DrawLine(new Pen(Color.Red, 3), this.startPoint, new Point(e.X, e.Y));//重绘
                    graphics.DrawImage(this.bitmap, this.pictureBox2.Location.X, this.pictureBox2.Location.Y, this.pictureBox2.Width, this.pictureBox2.Height);
                }
            }
        }
    }
}

        

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -