标题:C#自定义控件事件和委托问题,求高手指教
只看楼主
chuchur
Rank: 1
等 级:新手上路
帖 子:6
专家分:7
注 册:2011-4-22
 问题点数:0 回复次数:0 
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就卡住了。界面其他的控件就不绘制了。有黑色背景。我知道要用事件和委托来做,但是不知道具体要怎么做。
求高手为指点迷津,谢谢
2013-04-02 14:22



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-402859-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.615455 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved