WinForms-加载表单时如何使用PaintEventArgs运行函数? [英] WinForms - How do I run a function with PaintEventArgs when the form is loaded?

查看:36
本文介绍了WinForms-加载表单时如何使用PaintEventArgs运行函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解这些图形,并且在Graphics.FromImage文档中,以它为例:

I'm trying to understand the graphics, and in the Graphics.FromImage documentation, it has this as an example:

private void FromImageImage(PaintEventArgs e)
{

    // Create image.
    Image imageFile = Image.FromFile("SampImag.jpg");

    // Create graphics object for alteration.
    Graphics newGraphics = Graphics.FromImage(imageFile);

    // Alter image.
    newGraphics.FillRectangle(new SolidBrush(Color.Black), 100, 50, 100, 100);

    // Draw image to screen.
    e.Graphics.DrawImage(imageFile, new PointF(0.0F, 0.0F));

    // Dispose of graphics object.
    newGraphics.Dispose();
}

我是C#和Windows Forms的新手,正在努力了解所有这些如何结合在一起.该代码如何运行,例如在第一次加载表单时或在按下按钮时?

I'm new to C# and Windows Forms and am struggling to understand how this all fits together. How is this code run, say when the form first loads or when a button is pressed?

推荐答案

也许会有所帮助.我有一个示例,既可以绘制 Paint 事件,又可以绘制在现有 Image 的顶部.我创建了一个带有两个图片框的表单.每种情况一个. pictureBox1 具有用于 .Paint 事件的事件处理程序,而 pictureBox2 仅在按下按钮时绘制.

Maybe this will help. I have an example of both drawing on Paint events but also drawing on top of an existing Image. I created a form with two picture boxes. One for each case. pictureBox1 has an event handler for the .Paint event, while pictureBox2 is only drawn when a button is pressed.

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

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        pictureBox1.BackColor=Color.Black;
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        // The code below will draw on the surface of pictureBox1
        // It gets triggered automatically by Windows, or by
        // calling .Invalidate() or .Refresh() on the picture box.
        DrawGraphics(e.Graphics, pictureBox1.ClientRectangle);
    }

    private void toolStripButton1_Click(object sender, EventArgs e)
    {
        // The code below will draw on an existing image shown in pictureBox2
        var img = new Bitmap(pictureBox2.Image);
        var g = Graphics.FromImage(img);

        DrawGraphics(g, pictureBox2.ClientRectangle);
        pictureBox2.Image=img;
    }

    void DrawGraphics(Graphics g, Rectangle target)
    {
        // draw a red rectangle around the moon 
        g.DrawRectangle(Pens.Red, 130, 69, 8, 8);
    }
}

因此,当您启动该应用程序时,由于尚未按下该按钮,因此仅在左侧显示一个红色矩形.

So when you launch the application a red rectangle appears on the left only, because the button hasn't been pressed yet.

,当按下按钮时,红色矩形显示在 pictureBox2 中显示的图像上方.

and when the button is pressed, the red rectangle appears on top of the image displayed in pictureBox2.

没什么大不了的,但是它能完成任务.因此,根据您需要的操作模式(用户图形或图像批注),使用示例代码来了解如何进行操作.

Nothing dramatic, but it does the job. So depending on the mode of operation you need (user graphics, or image annotations) use the example code to understand how to do it.

这篇关于WinForms-加载表单时如何使用PaintEventArgs运行函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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