关于表单中的矩形绘制 [英] Regarding drawing rectangles in a form

查看:106
本文介绍了关于表单中的矩形绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


 公共  void  KaliedoScope()
       {
           center.X =  200 ;
           center.Y =  200 ;
           FirstColor = RandomColor();
           SecondColor = RandomColor();
           TrinketSize = RandomTrinketSize(MaxSize);
           offSet = RandomOffset(TrinketSize);
           TrinketQuad1 =  RectangleF(center +  SizeF(offSet,offSet), RectangleF(center +  SizeF(-offSet,offSet), RectangleF(center +  SizeF(-offSet,-offSet), RectangleF(center +  SizeF(offSet,-offSet), SolidBrush(FirstColor);
           SolidBrush secondbrush =  SolidBrush(SecondColor);
           图形g =  .CreateGraphics();
           g.FillRectangle(firstbrush,TrinketQuad1);
           g.FillRectangle(firstbrush,TrinketQuad2);
           g.FillRectangle(firstbrush,TrinketQuad3);
           g.FillRectangle(firstbrush,TrinketQuad4);
           firstbrush.Dispose();
           secondbrush.Dispose();

       } 


为什么此功能无法在表单上创建图形?
没有错误! :

Paint是一种哪种好玩的方法? (DataGridViewImageCell.Paint(...)) [在mdi子窗体之间画线 [在面板上捕获图形 [
其次,该代码不应编译,除非您使用的是尚未发布的扩展名,或者center 不是Point-在.NET 3.0中没有SizeF和Point的operator +

您确定输入到TrinketQuads的值正确吗?你检查了吗?



public void KaliedoScope()
       {
           center.X = 200;
           center.Y = 200;
           FirstColor = RandomColor();
           SecondColor = RandomColor();
           TrinketSize = RandomTrinketSize(MaxSize);
           offSet = RandomOffset(TrinketSize);
           TrinketQuad1 = new RectangleF(center + new SizeF(offSet, offSet), new SizeF(TrinketSize, TrinketSize));
           TrinketQuad2 = new RectangleF(center + new SizeF(-offSet, offSet), new SizeF(TrinketSize, TrinketSize));
           TrinketQuad3 = new RectangleF(center + new SizeF(-offSet, -offSet), new SizeF(TrinketSize, TrinketSize));
           TrinketQuad4 = new RectangleF(center + new SizeF(offSet, -offSet), new SizeF(TrinketSize, TrinketSize));

           SolidBrush firstbrush = new SolidBrush(FirstColor);
           SolidBrush secondbrush = new SolidBrush(SecondColor);
           Graphics g = this.CreateGraphics();
           g.FillRectangle(firstbrush, TrinketQuad1);
           g.FillRectangle(firstbrush, TrinketQuad2);
           g.FillRectangle(firstbrush, TrinketQuad3);
           g.FillRectangle(firstbrush, TrinketQuad4);
           firstbrush.Dispose();
           secondbrush.Dispose();

       }


why this function is not able to create graphics on a form?
there are no errors!!!!!

If addition to what Griff says, here is how to draw using Paint, OnPaint and Invalidate:

What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^].

As Griff pointed out, if you do your rendering not in the handler of the event Paint and not in the overridden method OnPaint your drawing will disappear. So, practically creating an instance of Graphics and drawing using it is useless. You need to use the instance passed through event arguments parameter.

—SA


First off, don''t do it that way - if you create a Graphics context, you MUST dispose of it. They are a scarce system resource, and you will run out of them a long time before the garbage collector gets called to free up memory.
Either enclose them in a using block:

using(Graphics g = this.CreateGraphics())
   {
   g.FillRectangle(firstbrush, TrinketQuad1);
   g.FillRectangle(firstbrush, TrinketQuad2);
   g.FillRectangle(firstbrush, TrinketQuad3);
   g.FillRectangle(firstbrush, TrinketQuad4);
   }

Or add a Dispose reference to your other two.
The better way to do this is in the Paint event, when a Graphics object will be given to you as part of the PaintEventArgs - this has the advantages that you do not need to dispose of it, and the drawing will be "permanent" (it won''t vanish if your form is re-sized, or minimized).

Secondly, That code shouldn''t compile, unless you are using some extensions you haven''t posted or center is not a Point - there is not operator+ for a SizeF and a Point in .NET 3.0

Are you sure that the values going into your TrinketQuads are correct? Have you checked?


这篇关于关于表单中的矩形绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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