如何使用Paint事件在鼠标坐标处绘制形状 [英] How to use the Paint event to draw shapes at mouse coordinates

查看:101
本文介绍了如何使用Paint事件在鼠标坐标处绘制形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我显然开始在C#中进行编程,并试图创建一个简单的WinForms应用,该应用获取鼠标坐标并根据坐标缩放矩形.

I recently started programming in C# obviously and was trying to do a simple WinForms app that takes mouse coordinates and scales a Rectangle according to the coordinates.

我面临的问题是我不知道如何调用使用更多参数的方法(在这种情况下为xyPaintEventArgs).或者,我确实知道如何使用PaintEvent.

The issue I am facing is that I don't know how to call a method that uses more arguments (in this case is x, y and PaintEventArgs). Or, I do know what to do with the PaintEvent.

这是整个代码,因为它很短而且很简单:

Here is the whole code, since its pretty short and rather simple:

using System;
using System.Drawing;
using System.Windows.Forms;

public partial class Form1 : Form
{
    public void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        int x = e.X; 
        int y = e.Y;
        String data = (x.ToString() + " " + y.ToString());
        DrawRect(Something, x, y);
    }

    PaintEventArgs pEventArgs;
    private void Form1_Paint(object sender, PaintEventArgs e)
    {

    }

    public void DrawRect(PaintEventArgs e, int rey, int rex)
    {
        Graphics gr = e.Graphics;
        Pen pen = new Pen(Color.Azure, 4);
        Rectangle rect = new Rectangle(0, 0, rex, rey);
        gr.DrawRectangle(pen, rect);
    }
}

我正在尝试调用DrawRect()方法,以根据鼠标坐标用widthheight绘制矩形.

I'm trying to call the DrawRect() method to draw the Rectangle with width and height according to the mouse coordinates.

那我怎么用坐标和PaintEventArgs调用DrawRect()?

So how can I call the DrawRect() with coordinates and PaintEventArgs?

推荐答案

PaintEventArgs允许您访问Graphics对象,您需要该对象来绘制对象.

The PaintEventArgs allows you to access to the Graphics object, you need that one to draw something.

如果您不想使用PaintEventArgs,我建议您调用FormCreateGraphics()方法,它将允许您绘制矩形.

If you don't want to use the PaintEventArgs, i suggest that you call the CreateGraphics() method of your Form, and it will allow you to draw the rectangle.

为了提高性能,我建议您使用using(...){}键盘,以便处理Graphics对象和Pen对象.

To improve performance, i suggest that you use the using(...){ } keywork in order to dispose the Graphics object and the Pen object.

您需要包括System.Drawing才能使用GraphicsPen.

You need to include System.Drawing in order to use Graphics and Pen.

您的代码将如下所示:

using System.Drawing;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        Point _coordinates;

        public Form1()
        {
            this._coordinates = new Point();
            this.InitializeComponent();
        }

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

        public void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            this._coordinates = new Point(e.X, e.Y);
            this.Invalidate();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            // Don't draw on first Paint event
            if(this._coordinates.X != 0 && this._coordinates.Y != 0)
            {
                this.DrawRect(e);
            }
        }

        public void DrawRect(PaintEventArgs e)
        {
            using (Pen pen = new Pen(Color.Azure, 4))
            {
                Rectangle rect = new Rectangle(0, 0, this._coordinates.X, this._coordinates.Y);
                e.Graphics.DrawRectangle(pen, rect);
            }
        }
    }
}

这篇关于如何使用Paint事件在鼠标坐标处绘制形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆