winform 根据鼠标移动画线

 

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

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

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        //MyPoint1, MyPoint2表示鼠标按下和弹起时鼠标的坐标位置
        public Point MyPoint1, MyPoint2;
        public int MyFlag = 0;

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            //当鼠标弹起时,设置MyFlag = 0,表示不能画线
            this.MyFlag = 0;
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            this.Text = "X=" + e.X.ToString() + ",Y=" + e.Y.ToString();
            Graphics g = this.CreateGraphics();
            Pen MyPen = new Pen(Color.Black);

            //MyFlag=0表示鼠标弹起,不能进行画线
            //当鼠标按下时,设置MyFlag=1表示可以画线
            if (this.MyFlag == 0)
                return;

            //鼠标移动,每次变换时,MyPoint2都记录下鼠标的位置,以便进行鼠标移动画线
            this.MyPoint2.X = e.X;
            this.MyPoint2.Y = e.Y;
            g.DrawLine(MyPen, MyPoint1.X, MyPoint1.Y, MyPoint2.X, MyPoint2.Y);

            //当画完一条线后(很短的,可以当做一个小点看待),将MyPoint1的坐标重置为此时鼠标的位置
            MyPoint1.X = e.X;
            MyPoint1.Y = e.Y;
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            //鼠标第一次按下时,设置鼠标坐标为第一个点的坐标
            this.MyFlag = 1;
            this.MyPoint1.X = e.X;
            this.MyPoint1.Y = e.Y;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.MyPoint1.X = 0;
            this.MyPoint1.Y = 0;

            this.MyPoint2.X = 90;
            this.MyPoint2.Y = 90; 
            Graphics g = this.CreateGraphics();
            float a = 10;
            Pen MyPen = new Pen(Color.Black,a);
            g.DrawLine(MyPen, MyPoint1.X, MyPoint1.Y, MyPoint2.X, MyPoint2.Y);
            g.DrawLine(MyPen,MyPoint2.X , MyPoint1.Y,MyPoint1.X , MyPoint2.Y);


            g.DrawLine(MyPen, MyPoint2.X, MyPoint2.Y, MyPoint2.X, MyPoint1.Y);
            g.DrawLine(MyPen, MyPoint1.X, MyPoint2.Y, MyPoint1.X, MyPoint1.Y);


        }


    }
}


 

 

在Windows Form应用程序中,你可以使用.NET Framework的GDI+库来通过鼠标事件来绘制矩形。下面是一个简单的步骤说明: 1. **事件处理**:首先,你需要在Form上设置一个鼠标按下、移动和释放的事件处理器。可以使用MouseDown、MouseMove和MouseUp事件。 ```csharp private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { // 开始绘制 } private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { // 移动鼠标时更新矩形位置 } private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { // 鼠标抬起结束绘制 } ``` 2. **初始状态**:在MouseDown事件中,记录下鼠标按下时的位置和形状信息。例如,创建一个Point用于存储初始点和一个Rectangle表示初始矩形。 3. **绘制过程**:在MouseMove事件中,每次鼠标移动,更新Rectangle的位置和大小。通常会在PictureBox或其他容器控件中画线,因为它们支持绘操作。 4. **结束绘制**:当鼠标在PictureBox上抬起(MouseUp)时,根据鼠标的最后位置停止画线,并可能清空绘图区,如果需要的话。 5. **实际绘制**:使用Graphics对象的DrawRectangle方法来绘制矩形,它接受Pen对象(颜色和宽度)、Rectangle对象以及填充模式作为参数。 示例代码片段(简化版): ```csharp private Point startPoint; private Rectangle rect; private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { startPoint = e.Location; rect = new Rectangle(startPoint, Size.Empty); } private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { rect.Width = e.X - startPoint.X; rect.Height = e.Y - startPoint.Y; pictureBox1.Invalidate(); } } private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { pictureBox1.Invalidate(); startPoint = Point.Empty; // 清除起点 } ``` 记得要在pictureBox1_Paint事件中处理绘制: ```csharp private void pictureBox1_Paint(object sender, PaintEventArgs e) { using (var pen = new Pen(Color.Black)) { e.Graphics.DrawRectangle(pen, rect); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值