如何调用使用PaintEventArgs和坐标变量的方法 [英] How to call a method that uses PaintEventArgs and coordinates variables

查看:786
本文介绍了如何调用使用PaintEventArgs和坐标变量的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近显然开始在C#中进行编程,并且试图创建一个简单的WinForms应用,该应用可以获取鼠标坐标并根据坐标缩放矩形.
我面临的问题是我不知道如何调用使用更多参数的方法(在这种情况下为x,y和PaintEventArgs).或者我确实知道,但是我不知道如何使用PaintEvent.

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.
The issue I am facing is that i dont know how to call a method that uses more arguments (in this case is x, y and PaintEventArgs). Or I do know, but I don't 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绘制矩形.我尝试了Google搜索并向好友寻求帮助,但到目前为止还没有运气.
那么如何用坐标和PaintEventArgs调用DrawRect()?

I'm trying to call the DrawRect function to draw the Rectangle with width and height according to the mouse coordinates. I tried Googling and asking buddies for help but so far no luck.
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);
            }
        }
    }
}

这篇关于如何调用使用PaintEventArgs和坐标变量的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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