【问题标题】:Is doing this,is the right way with regard to performance and memory usage-leaks or is it a better way?这样做是关于性能和内存使用泄漏的正确方法还是更好的方法?
【发布时间】:2012-03-12 20:17:37
【问题描述】:

好的,我在 winform 顶部有一个按钮,在表单构造函数中我有 Graphics 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(new Pen(this.BackColor, 2.0f), this.MdPlayerButton.Location.X - 2,this.MdPlayerButton.Location.Y - 2, this.MdPlayerButton.Size.Width + 4, this.MdPlayerButton.Size.Height + 4);

在矩形上绘制矩形是否会导致内存泄漏或其他问题,或者突出显示按钮是否是一种好习惯?

我希望能够调整我的表单大小,因此不建议使用现成的图像并在它们之间进行交换。

谢谢!

【问题讨论】:

    标签: winforms c#-4.0 button memory-leaks gdi


    【解决方案1】:

    WinForms 将 GDI 用于图形,而 GDI 就是所谓的即时模式。这意味着您可以绘制一个或十亿个矩形;这只会占用与保存生成的位图一样多的内存。

    为了提高效率,您可能希望手动处理您创建的 Pen,但如果您不这样做,.NET 最终还是会为您清理它们:

    using (var pen = new Pen(...))
       DrawRectangle(pen, ...);
    

    【讨论】:

      猜你喜欢
      • 2011-01-13
      • 1970-01-01
      • 2012-02-05
      • 1970-01-01
      • 1970-01-01
      • 2017-05-16
      • 2021-07-06
      • 2014-01-03
      相关资源
      最近更新 更多