图形绘画OnPaint()方法的问题 [英] problem with Graphic paint OnPaint() Method

查看:116
本文介绍了图形绘画OnPaint()方法的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么此代码与Button_Click(在表单上)正确执行,
但是当它与Form_load一起执行时却没有响应???

why this code with Button_Click(On Form) excecute correctly,
But when it with Form_load Excecuted it not response???

private void button1_Click(object sender, EventArgs e)
       {
           String drawString = "Sample Text";

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

           // Create point for upper-left corner of drawing.
           Point drawPoint = new Point(20, 20);

           // Draw string to screen.
           Graphics p= pictureBox1.CreateGraphics();

           Graphics k = this.CreateGraphics();
           p.DrawString(drawString, drawFont, drawBrush, drawPoint);


       }



-------------------------------------------------- ----------------------



------------------------------------------------------------------------

private Form_Load(object sender, EventArgs e)       //not response
       {
           String drawString = "Sample Text";

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

           // Create point for upper-left corner of drawing.
           Point drawPoint = new Point(20, 20);

           // Draw string to screen.
           Graphics p= pictureBox1.CreateGraphics();

           Graphics k = this.CreateGraphics();
           p.DrawString(drawString, drawFont, drawBrush, drawPoint);


       }

推荐答案

OnPain()方法仅在Duke Nukem API中找到.
(对不起,我开玩笑了.)

认真地讲,为什么不显示一个Label控件而不是浏览所有这些手动绘图内容呢?

如果您代替处理表单的Shown事件,它可能会按预期呈现.我相信在渲染表单后会触发此事件.
The OnPain() method is only found in the Duke Nukem API.

(Sorry I couldn''t resist making the joke.)

Seriously, though, why don''t you just display a Label control instead of going through all that manual drawing stuff?

Maybe it will render as expected if you handle the form''s Shown event instead. I believe this event is fired after the form has been rendered.


根据经验,请勿使用CreateGraphics.
而是改写OnPaint或处理Paint事件,并使用事件arguments参数中的Graphics实例.
(我认为,您已经得到了此建议,为什么不遵循它.)

您是否认为按一下按钮即可正常工作?我对此表示怀疑.在运行时,请尝试使图片无效.只需在其顶部放置另一个窗口,然后将表格再次置于顶部即可.您在按钮单击上所做的所有渲染都将消失.如果这是您所期望的,那么没问题,这是正确的:).另一个问题是您不处置Graphics的实例.您应该已经使用过using语句来创建该对象,因此在using块结束时,它将自动调用IDisposable.Dispose.但这没关系,因为您真正需要的是OnPaint甚至是Paint上的处理程序.如果要在按钮单击上进行一些更改,只需更改Paint事件内部渲染中使用的数据,然后调用要绘制的控件的Control.Invalidate.将Invalidate与参数(有几个重载)一起使用可使只是您想要的部分图片无效.

并且,当然,如果不可能对事件FormLoad进行处理.从某种意义上说,这是一个假事件.如果您处理它,它将从表单构造函数中调用,因此我从未使用过,只需直接从构造函数中调用我的设置方法即可.

该表单当时甚至没有显示,您希望得到什么渲染?!

-SA
As a rule of thumb, do not use CreateGraphics.
Instead, override OnPaint or handle Paint event and use the Graphics instance from the event arguments parameter.
(I think, you already got this advise, why not following it.)

Do you think you drawing on button click work correctly? I doubt it. During run time, try to invalidate the picture. Just put another windows on top of it, then bring you form on top again. All you rendering made on the button click will disappear. If this is what you expected then all right, it''s correct :) . Another problem is you don''t dispose the instance of Graphics. You should have used using statement to create this object, so at the end of using block it would call IDisposable.Dispose automatically. It does not matter though, because what you really need is OnPaint or even handler on Paint. If you want some change on the button click, just change the data used in rendering inside Paint event and call Control.Invalidate of the control you''re painting on. Use Invalidate with parameter (there are couple of overloads) to invalidate just part of the picture is you want.

And, of course, if can not possible work on event FormLoad. In a way, this a fake event. if you handle it, it will be called from the form constructor, so I never used to, just call my set-up method from the constructor directly.

The form is not even shown at that time, what rendering would you expect?!

—SA


根据 ^ ],Form.Load事件:在第一次显示表单之前发生.
这意味着在触发Form_Load时,尚未生成该表单的任何Graphic元素.没有任何东西可以获取图形或进行绘制等.还没有视觉元素.
According to MSDN[^], the Form.Load event: Occurs before a form is displayed for the first time.
This means that at the time that Form_Load is fired, none of the Graphic elements of the form have been generated yet. There is nothing to get the Graphics of, or paint on, etc. There are no visual elements yet.


这篇关于图形绘画OnPaint()方法的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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