C#自定义控件事件和委托问题,求高手指教
我正在做一个自定义的listbox控件,我的代码大致如下程序代码:
.... public class ListItem { public ListItem() { } public ListItem(string songname, int songlenght) { this.songname = songname; this.songlenght = songlenght; } private string songname; private int songlenght; private int alpha = 0; private bool focus; private EMouseState mouseState; private Rectangle bounds; public Rectangle Bounds { get { return bounds; } set { bounds = value; } } public int Alpha { get { return alpha; } set { alpha = value; } } public EMouseState MouseState { get { return mouseState; } set { mouseState = value; } } public bool Focus { get { return focus; } set { focus = value; } } public string SongName { get { return songname; } set { songname = value; } } public int SongLenght { get { return songlenght; } set { songlenght = value; } } } .... private List<ListItem> items = new List<ListItem>(); public List<ListItem> Items { get { return items; } } ... protected override void OnPaint(PaintEventArgs e) { //base.OnPaint(e); TextFormatFlags SNFlags = TextFormatFlags.VerticalCenter; TextFormatFlags SLFlags = TextFormatFlags.VerticalCenter | TextFormatFlags.Right; Graphics g = e.Graphics; g.SmoothingMode = SmoothingMode.AntiAlias; //g.TextRenderingHint = TextRenderingHint.AntiAlias; g.InterpolationMode = InterpolationMode.High; Rectangle rectItem = new Rectangle(0, 1, Width, itemSize.Height); if (Items != null && Items.Count() > 0) foreach (ListItem li in Items) { string songname = li.SongName; int songlenght = li.SongLenght; //画背景 using (SolidBrush sb = new SolidBrush(Color.FromArgb(li.Alpha, this.Parent.BackColor))) { g.FillRectangle(sb, rectItem); } //画文字 TextRenderer.DrawText(g, songname, Font, rectItem, ForeColor, SNFlags); TextRenderer.DrawText(g, songlenght.ToString(), Font, rectItem, ForeColor, SLFlags); li.Bounds = new Rectangle(rectItem.Location, rectItem.Size); rectItem.Y = rectItem.Bottom + 1; } } ... protected override void OnMouseMove(MouseEventArgs e) { // Point mouse = e.Location; foreach (ListItem li in Items) { if (li.Bounds.Contains(mouse)) { li.Alpha = 200; this.Invalidate(li.Bounds); } } base.OnMouseMove(e); } ... protected override void OnMouseLeave(EventArgs e) { ... }
当鼠标划过和选中的时候重新绘制项的背景。控件外面有个timer,但是鼠标划过的时候那个tiemer就卡住了。界面其他的控件就不绘制了。有黑色背景。我知道要用事件和委托来做,但是不知道具体要怎么做。
求高手为指点迷津,谢谢