Winforms 在性能和内存使用泄漏方面,这样做是正确的方法还是更好的方法?
好的,我在winform的顶部有一个按钮,在表单构造函数中我有GraphicsWinforms 在性能和内存使用泄漏方面,这样做是正确的方法还是更好的方法?,winforms,c#-4.0,button,memory-leaks,gdi,Winforms,C# 4.0,Button,Memory Leaks,Gdi,好的,我在winform的顶部有一个按钮,在表单构造函数中我有Graphicsobj=this.CreateGraphics() 当触发鼠标输入事件时 MainFormGraphicsHandle.DrawRectangle( new Pen(Color.CornflowerBlue, 2.0f), this.MdPlayerButton.Location.X - 2, this.MdPlayerButton.Location.Y - 2, this.MdPlayerButton.Size.Wi
obj=this.CreateGraphics()代码>
当触发鼠标输入事件时
MainFormGraphicsHandle.DrawRectangle( new Pen(Color.CornflowerBlue, 2.0f),
this.MdPlayerButton.Location.X - 2, this.MdPlayerButton.Location.Y - 2,
this.MdPlayerButton.Size.Width + 4, this.MdPlayerButton.Size.Height + 4);
当鼠标离开时被触发
MainFormGraphicsHandle.DrawRectangle(新笔(此为背景色,2.0f),
这个.MdPlayerButton.Location.X-2,这个.MdPlayerButton.Location.Y-2,
this.MdPlayerButton.Size.Width+4,this.MdPlayerButton.Size.Height+
4);
在矩形上绘制矩形是否会导致内存泄漏或其他问题,或者突出显示按钮是一种良好的做法
我希望能够调整我的表单大小,所以使用就绪图像并在它们之间交换是不可取的
谢谢大家! WinForms使用GDI进行图形处理,GDI被称为即时模式。这意味着您可以绘制一个或十亿个矩形;这将只占用保存生成位图所需的内存
为了提高效率,您可能希望手动处理您创建的笔,但如果不这样做,.NET最终将为您清理这些笔:
using (var pen = new Pen(...))
DrawRectangle(pen, ...);