1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84:
| public class frListView : ListView { private HorizontalAlignment _columnTextAlign = HorizontalAlignment.Center; private HorizontalAlignment _itemTextAlign = HorizontalAlignment.Center;
public frListView() { this.OwnerDraw = true; }
protected override void OnDrawColumnHeader(DrawListViewColumnHeaderEventArgs e) { using (StringFormat sf = new StringFormat()) { switch (_columnTextAlign) { case HorizontalAlignment.Left: sf.Alignment = StringAlignment.Near; break; case HorizontalAlignment.Center: sf.Alignment = StringAlignment.Center; break; case HorizontalAlignment.Right: sf.Alignment = StringAlignment.Far; break; } e.DrawBackground(); SizeF sSize = e.Graphics.MeasureString(e.Header.Text, e.Font); Rectangle rect = e.Bounds; int sY = (int)((rect.Height / 2f) - (sSize.Height / 2f) - 1) * -1; rect.Inflate(0, sY); e.Graphics.DrawString(e.Header.Text, e.Font, Brushes.Black, rect, sf); } base.OnDrawColumnHeader(e); }
protected override void OnDrawSubItem(DrawListViewSubItemEventArgs e) { using (StringFormat sf = new StringFormat()) { switch (_itemTextAlign) { case HorizontalAlignment.Left: sf.Alignment = StringAlignment.Near; break; case HorizontalAlignment.Center: sf.Alignment = StringAlignment.Center; break; case HorizontalAlignment.Right: sf.Alignment = StringAlignment.Far; break; } e.DrawBackground(); SizeF sSize = e.Graphics.MeasureString(e.SubItem.Text, e.SubItem.Font); Rectangle rect = e.Bounds; int sY = (int)((rect.Height / 2f) - (sSize.Height / 2f)) * -1; rect.Inflate(0, sY); e.Graphics.DrawString(e.SubItem.Text, e.SubItem.Font, Brushes.Black, rect, sf); } base.OnDrawSubItem(e); }
[Browsable(true)] [Description("Richtet den Text in den Spalten aus.")] [Category("ColumnTextAlign")] public HorizontalAlignment ColumnTextAlign { get { return _columnTextAlign; } set { _columnTextAlign = value; } }
[Browsable(true)] [Description("Richtet den Text in den Zellen aus.")] [Category("ItemTextAlign")] public HorizontalAlignment ItemTextAlign { get { return _itemTextAlign; } set { _itemTextAlign = value; } } } |