-
Notifications
You must be signed in to change notification settings - Fork 191
Open
Description
In funcs that're called when a draw is needed, you're declaring multiple objects when it's actually better to declare and assign the objects in the constructor and avoid creating/destroying objects in the actual draw/paint methods
Below is taken from DarkUI/Forms/DarkForm.cs
var g = e.Graphics;
using (var p = new Pen(Colors.DarkBorder))
{
var modRect = new Rectangle(ClientRectangle.Location, new Size(ClientRectangle.Width - 1, ClientRectangle.Height - 1));
g.DrawRectangle(p, modRect);
}
Rather than creating a new series of objects (eg; NEW pen => NEW var modRect = blah, NEW size) you can declare these objects in the constructor and only update the values if/when needed-
public partial Form {
clientModRect = new Rect(blah);
clientBorderPen = new Pen(blah);
}
public eventClientSizeChanged() { // or borderColorChanged, whatever update triggers needed
//update the clientModRect, clientBorderPen, and anything else that needs changing
}
public Draw(PaintEventArgs e) { // negating as many obj creations as possible to reduce mem usage and GC calls
base.OnPaintBackground(e);
if (!flatBorder) return;
e.Graphics.DrawRectangle(clientBorderPen, clientModRect);
} // end draw
This is entirely a performance-oriented suggestion/issue, and if it's not something y'all care about feel free to close and ignore :)
Metadata
Metadata
Assignees
Labels
No labels