0

如何在鼠标单击事件中调用具有系统参数的方法:

public void DrawStringRectangleF(PaintEventArgs e)
{

    // Create string to draw.
    String drawString = "Sample Text";

    // Create font and brush.
    Font drawFont = new Font("Arial", 16);
    SolidBrush drawBrush = new SolidBrush(Color.Black);

    // Create rectangle for drawing. 
    float x = 150.0F;
    float y = 150.0F;
    float width = 200.0F;
    float height = 50.0F;
    RectangleF drawRect = new RectangleF(x, y, width, height);

    // Draw rectangle to screen.
    Pen blackPen = new Pen(Color.Black);
    e.Graphics.DrawRectangle(blackPen, x, y, width, height);

    // Draw string to screen.
    e.Graphics.DrawString(drawString, drawFont, drawBrush, drawRect);
}

我想在这个事件中调用这个方法:

private void button1_Click(object sender, EventArgs e)
        {
            // calling DrawStringRectangleF(PaintEventArgs e) here
        }

如果参数是变量,调用这些方法的时候只要提一下值就可以很容易地调用方法,但是这种情况下怎么调用呢?!

4

2 回答 2

3

为了满足现有的方法签名,您可以封装Graphics您希望用来在PaintEventArgs类的实例中绘制矩形的实例,如下所示

using (var controlGraphics = CreateGraphics())
{
    var paintEventArgs = new PaintEventArgs(controlGraphics, ClientRectangle);
    DrawStringRectangleF(paintEventArgs);
}

Control.CreateGraphics方法将生成一个Graphics实例,您可以使用该实例在底层控件(或窗体)上进行绘制。

请理解,Graphics每次控件无效时,使用该对象绘制的所有内容都将被删除,因此您必须不断地重新绘制图形。

您可以考虑调整方法签名,以便它接受一个实例,Graphics而不是这应该是一个更清洁的解决方案。

于 2013-10-20T18:29:57.603 回答
1

这是一个使用 Paint() 事件和自定义类的 List 来存储有关应绘制的信息的示例:

public partial class Form1 : Form
{

    public class TextData
    {
        public string Text;
        public string FontName;
        public int FontSize;
        public RectangleF TextRectF;
        public Color TextColor;
    }

    private List<TextData> Texts = new List<TextData>();

    public Form1()
    {
        InitializeComponent();
        this.Paint += new PaintEventHandler(Form1_Paint);

        TextData td = new TextData();
        td.Text = "Example";
        td.FontName = "Courier";
        td.FontSize = 24;
        td.TextRectF = new RectangleF(50.0F, 50.0F, 175.0F, 75.0F);
        td.TextColor = Color.Red;
        Texts.Add(td);
    }

    void Form1_Paint(object sender, PaintEventArgs e)
    {
        foreach (TextData td in Texts)
        {
            using (Pen drawPen = new Pen(td.TextColor))
            {
                e.Graphics.DrawRectangle(drawPen, Rectangle.Round(td.TextRectF));
            }
            using (Font drawFont = new Font(td.FontName, td.FontSize))
            {
                using (SolidBrush drawBrush = new SolidBrush(td.TextColor))
                {
                    e.Graphics.DrawString(td.Text, drawFont, drawBrush, td.TextRectF);            
                }
            }
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        TextData td = new TextData();
        td.Text = "Sample Text";
        td.FontName = "Arial";
        td.FontSize = 16;
        td.TextRectF = new RectangleF(150.0F, 150.0F, 200.0F, 50.0F);
        td.TextColor = Color.Black;
        Texts.Add(td);
        this.Refresh();
    }

}
于 2013-10-20T20:03:03.927 回答