从表单的on_paint调用组件的图形 [英] calling the graphics of a component from the on_paint of the form

查看:119
本文介绍了从表单的on_paint调用组件的图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个图形组件来执行带有世界坐标的绘图,这是一个矩形,其中带有World(-1000,-1000,1000,1000)指令,我声明了x和y轴的范围,并带有Line(- 1000,-1000,1000,1000)我可以绘制对角线.

要调用图形函数,我必须将其传递给PaintEventArgs ^参数.

在第一个版本中,我传递给每个函数这些组件中的参数来自其自己的on_paint事件处理程序,一切正常.

之后,我试图更改grafics的结构,从表单的onpaint处理程序中调用所有图形(我已经做到了在cbuilder中).要做到这一点,我必须将组件的PaintEventArgs ^参数传递给组件的每个绘图函数,而不是表单的onpaint处理程序之一.

I试图将其自己的PaintEventArgs ^参数附加到每个组件,该参数在其InitializeComponent()中实例化了,但是没有用,并且图形是随机执行的.

我的uns创建此参数的成功尝试是:

I created a graphic component to perform drawing with world coordinates, that is a rectangle where  with a World(-1000,-1000,1000,1000) instruction i declare the range of x and y axis and with a Line(-1000,-1000,1000,1000) i can draw the diagonal.

To call the graphic functions I had to pass them a PaintEventArgs^ argument.

In the first version I passed to each of these components the argument from its own on_paint event handler and everything worked fine.

After,  I tried to change the structure of my grafics, calling all the drawing from the onpaint handler of the form (I had done that in cbuilder).

To do that I had to pass to each drawing function of the components the PaintEventArgs^ argument of the component and not the one of the onpaint handler of the form.

I tried to attach to every component its own PaintEventArgs^ argument, instantiated in its InitializeComponent(), but that didn't work, and graphics performed randomly.

My unsuccessful trial of creating this argument was this:

 

System :: Windows :: Forms :: PaintEventArgs ^ The_e;

System::Windows::Forms::PaintEventArgs^ The_e;



无效的InitializeComponent()

{Graphics ^ graphics = CreateGraphics();



void InitializeComponent()

{Graphics^ graphics=CreateGraphics();

矩形RcDraw;

RcDraw.Width = -> Width;

RcDraw.Width =this->Width;

RcDraw.Height = -> Height;

RcDraw.Height =this->Height;

RcDraw.Location = -> Location;

RcDraw.Location =this->Location;

The_e = gcnew System :: Windows :: Forms :: PaintEventArgs(graphics,RcDraw);

The_e=gcnew System::Windows::Forms::PaintEventArgs( graphics, RcDraw);

推荐答案

PictureBox组件排序符合条件.它具有Image属性,该属性附加了Image类类型的持久对象.您可以使用Graphics :: FromImage()对其进行涂写,然后调用PB的Invalidate()方法对其进行绘制.但是,这不是Windows窗体所鼓励的绘画风格.为了使各种技巧发挥作用,例如双缓冲,您应该始终在Paint事件中进行绘制.

如果由于某些原因而代价昂贵,则可以始终绘制Bitmap并仅在Paint中绘制DrawImage()即可.事件.与画布几乎没有区别.无论您做什么,都*绝*会存储Graphics或PaintEventArgs对象.他们的hDC迟早会被保证是错误的.
The PictureBox component sorta qualifies.  It has an Image property that attaches a persistent object of the Image class type.  You could scribble in it with Graphics::FromImage(), then call the PB's Invalidate() method to get it painted.  This is however not a style of painting that is at all encouraged by Windows Forms.  To make various tricks work, like double-buffering, you should always draw in the Paint event.

If that is expensive for some reason, you can always draw in a Bitmap and just DrawImage() it in the Paint event.  Which is pretty indistinguishable from a Canvas.  Whatever you do, *never* store a Graphics or PaintEventArgs object.  Their hDC is guaranteed to be incorrect sooner or later.


这篇关于从表单的on_paint调用组件的图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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