在新表格上绘制图形

时间:2015-03-14 04:47:19

标签: c# graphics drawing

我在绘制图形方面遇到了问题,请原谅我,因为我对它完全陌生。我现在拥有的是一个名为FormDraw的表单。在FormDraw中,我有一个按钮。按钮的作用是

private void button1_Click(object sender, EventArgs e)
{
    using (Form form = new Form())
    {
        form.Text = "About Us";
        System.Drawing.Graphics graphics = this.CreateGraphics();
        System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(100, 100, 200, 200);
        graphics.DrawEllipse(System.Drawing.Pens.Black, rectangle);
        graphics.DrawRectangle(System.Drawing.Pens.Red, rectangle);
        // form.Controls.Add(...);

        form.ShowDialog();
    }
}

我想要实现的是在新表单上绘制图形,但是在单击它在旧表单(FormDraw)上创建的按钮时。知道我做错了什么吗?

3 个答案:

答案 0 :(得分:3)

这里的主要问题似乎是你没有研究Winforms API中的绘图是如何工作的,所以你试图在不理解它的情况下使用API​​。 :(

您发布的代码将尝试仅绘制一次新创建的表单对象。这个绘图根本不可能工作,因为表单初始化的其余部分还没有完成,但是即使某些东西确实绘制到屏幕上,它会立即丢失,因为新的东西后来被绘制到屏幕上(就像表格一样)当它最终显示时本身。)

Winforms与大多数主流GUI API(包括Mac OS,Java的SWT,AWT,Swing等,当然还有本机Windows API)一样,是“即时模式”API。也就是说,您的代码应该按需绘制,并绘制当时需要呈现的内容。 API不记得您绘制的任何内容;任何时候发生某些事情(对你的数据或屏幕本身,例如窗口重叠变化)会使你已经绘制的内容无效,你必须再次绘制它。

绘制Winforms控件(包括表单)的唯一合适位置是在实际处理程序中或通过覆盖Paint处理OnPaint()事件。如果你想执行一次绘图语句,那么你必须绘制一个位图对象(有效地缓存它们),然后在Paint事件期间自己绘制位图。

在你的问题中没有足够的背景能够真正理解你正在尝试做什么。但是您发布的代码可以修复为(我认为)您期望的工作,通过修改它看起来像这样:

private void button1_Click(object sender, EventArgs e) {
    using (Form form = new Form())
    {
        form.Text = "About Us";
        form.Paint += (sender, e) =>
        {
            System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(100, 100, 200, 200);
            e.Graphics.DrawEllipse(System.Drawing.Pens.Black, rectangle);
            e.Graphics.DrawRectangle(System.Drawing.Pens.Red, rectangle);
        }

        // form.Controls.Add(...);

        form.ShowDialog();
    }
}

传递给PaintEventArgs事件处理程序的Paint类包含{em> Graphics实例的Graphics属性在Paint事件期间绘制时使用。

上面为新表单的Paint事件订阅了一个匿名事件处理程序方法。在该处理程序中,使用通过传递给处理程序的Graphics提供给您的PaintEventArgs实例完成所需的绘图。

(请注意,上面只修复了Paint处理。当然,您需要正确初始化表单对象,例如设置其宽度和高度,实际添加所需的控件等。)

答案 1 :(得分:0)

我没有使用我在问题中发布的上述方法,而是设法使用

Form form2 = new Form();
form2.Show();

var graphics = form2.CreateGraphics();
var rectangle = new System.Drawing.Rectangle(100, 100, 200, 200);

graphics.DrawEllipse(System.Drawing.Pens.Black, rectangle);
graphics.DrawRectangle(System.Drawing.Pens.Red, rectangle);

然而,我会对绘图课程做更多的研究。谢谢Peter Duniho

答案 2 :(得分:0)

您可以使用“PictureBox”在新表单上显示创建的图像。

private void Button1_Click(object sender, EventArgs e)
    {
        // create Form instance
        Form form = new Form
        {
            Text = "About Us",
            Width = 440,
            Height = 460,
        };

        // create bmp image
        Bitmap bmp = new Bitmap(400, 400);
        // draw on bmp image
        using (Graphics graphics = Graphics.FromImage(bmp))
        {
            graphics.Clear(Color.Transparent);
            Rectangle rectangle = new Rectangle(100, 100, 200, 200);
            graphics.DrawEllipse(Pens.Black, rectangle);
            graphics.DrawRectangle(Pens.Red, rectangle);

        }
        // create PictureBox instance
        PictureBox pictureBox = new PictureBox
        {
            Image = bmp,
            //BorderStyle = BorderStyle.FixedSingle,
            //Dock = DockStyle.Fill,
            /// To center
            Size = new Size(400,400),
            Location = new Point(10,10)
        };
        // add pictureBox control to form
        form.Controls.Add(pictureBox);
        // show form in dialog box
        form.ShowDialog();
    }