【问题标题】:Why some controls has both BackgroundImage and Image property?为什么有些控件同时具有 BackgroundImage 和 Image 属性?
【发布时间】:2011-01-08 16:39:12
【问题描述】:

为什么像ButtonPictureBox 这样的控件同时具有BackgroundImageImage 属性?

为什么在按钮的情况下两者都需要? 或者为什么图片框中的背景图像应该可用?

【问题讨论】:

  • 你了解两者的区别吗?
  • @matt:是的,为什么在按钮的情况下两者都需要?
  • @matt: 或者为什么图片框中的背景图片应该可用?

标签: .net winforms controls


【解决方案1】:

BackgroundImage 继承自 Control

Image,在Button的情况下继承自ButtonBase


一般来说,BackgroundImage 是:

表示要在控件背景中显示的图像的 Image。

Image 将是控件上的那个。


为什么需要这两个?

  • 因为您希望在控件周围显示背景图像的位置可能有边距。
  • 因为有时您可能会在Image 上使用透明胶片,并希望背景图片能透出来。

【讨论】:

    【解决方案2】:

    这主要是因为它无论如何都在那里。它继承自 Control 类,无需额外工作即可使其正常工作。事实上,需要额外的工作来隐藏它,这是不支持背景图像的本机 Windows 控件所必需的。喜欢 TreeView:

    public class TreeView : Control
    {
        [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
        public override Image BackgroundImage
        {
           // etc..
        }
    // etc..
    }
    

    注意 override 关键字和在属性网格和编辑器中隐藏它所需的属性。额外的工作来阻止它工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多