WinForms의 사용자 정의 목록보기?
문제
ListView에 일부 문자열을 그릴 수 있습니까?
나는 onpaint 이벤트를 무시했지만 아무런 변화도 보이지 않습니다. Custom ListView에서 일부 코드를 확인했지만 사람들이 P/Invoke 등을 사용하는 것처럼 보입니다. 왜?
버튼 컨트롤과 같은 다른 winform과 같이 목록이 사용자 정의 할 수 없습니까?
나는 격렬하게 사용자 정의하지 않고 표준 그림을 마친 후에 더 페인트를 칠할 것입니다.
해결책
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));
}
}
다른 팁
You can't just override the OnPaint()
method. That method doesn't do anything in a ListView. Similarly, OwnerDrawn
lets you custom draw each cell, but doesn't let you paint over the control as a whole.
Use an ObjectListView (an open source wrapper around .NET WinForms ListView) and use its Overlay feature. That lets you effortlessly do something like this:
This was produced by this code:
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;
Set the OwnerDraw property to true.
You can then handle the DrawItem, DrawSubItem, and DrawColumnHeader events to draw on specific elements of the ListView.