关于表单中的矩形绘制 [英] Regarding drawing rectangles in a form
问题描述
公共 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 usingPaint
,OnPaint
andInvalidate
:
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 eventPaint
and not in the overridden methodOnPaint
your drawing will disappear. So, practically creating an instance ofGraphics
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 orcenter
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屋!