【问题标题】:Custom ListView in Winforms?Winforms中的自定义ListView?
【发布时间】:2010-12-08 22:46:38
【问题描述】:

是否可以在列表视图上绘制一些字符串?

我重写了 OnPaint 事件,但没有看到任何变化。我在自定义列表视图上查看了一些代码,但似乎人们正在使用 p/invoke 等。为什么?

list 不是像其他 winform 一样可定制吗,比如 Button 控件?

我不会大肆定制,只是在完成标准绘画之后再画一些。

【问题讨论】:

    标签: c# .net winforms listview gdi+


    【解决方案1】:

    您不能只覆盖OnPaint() 方法。该方法在 ListView 中没有任何作用。同样,OwnerDrawn 允许您自定义绘制每个单元格,但不允许您在整个控件上进行绘制。

    使用ObjectListView(.NET WinForms ListView 的开源包装器)并使用它的Overlay feature。这让你可以毫不费力地做这样的事情:

    这是由这段代码产生的:

    this.olv1.OverlayText.Alignment = ContentAlignment.BottomRight;
    this.olv1.OverlayText.Text = "Trial version";
    this.olv1.OverlayText.BackColor = Color.White;
    this.olv1.OverlayText.BorderWidth = 2.0f;
    this.olv1.OverlayText.BorderColor = Color.RoyalBlue;
    this.olv1.OverlayText.TextColor = Color.DarkBlue;
    

    【讨论】:

      【解决方案2】:
       class MyCustomlistView : ListView
          {
              public MyCustomlistView()
                  : base()
              {
                  SetStyle(ControlStyles.UserPaint, true);
              }
              protected override void OnPaint(PaintEventArgs e)
              {
                  base.OnPaint(e);
                  e.Graphics.DrawString("This is a custom string", new Font(FontFamily.GenericSerif, 10, FontStyle.Bold), Brushes.Black, new PointF(0, 50));
              }
      
          }
      

      【讨论】:

      • 这不适用于 ListView。它只是让 ListView 什么都不画。
      【解决方案3】:

      OwnerDraw 属性设置为true。

      然后,您可以处理 DrawItemDrawSubItemDrawColumnHeader 事件来绘制 ListView 的特定元素。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-06-18
        • 2011-12-18
        • 2013-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多