首页 新闻 会员 周边

关于Listbox重绘,出现显示不全问题

0
悬赏园豆:50 [待解决问题]

实现的控件效果,类似QQ好友列表,选中放大,经过变色

 

选中非倒数第二项后,选中项与倒数第二项显示不全或不显示

拜托各位GGJJ帮帮忙!!完全不知道该怎么办T^T

这是控件的完整代码

public class FriendsListBox : ListBox
    {
        private int _MouseIndex = -1;

        public FriendsListBox()
        {
            this.DrawMode = DrawMode.OwnerDrawVariable;
            this.MeasureItem += new MeasureItemEventHandler(FriendsListBox_MeasureItem);
            this.DrawItem += new DrawItemEventHandler(FriendsListBox_DrawItem);
            this.MouseMove += new MouseEventHandler(FriendsListBox_MouseMove);
            this.Leave += new EventHandler(FriendsListBox_Leave);
            //this.SelectedIndexChanged += new EventHandler(FriendsListBox_SelectedIndexChanged);
        }

        //private void FriendsListBox_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    for (int i = 0; i < this.Items.Count; i++)
        //    {
        //        if (i != this.SelectedIndex)
        //            this.RefreshItem(i);
        //    }
        //}

        private void FriendsListBox_Leave(object sender, EventArgs e)
        {
            if (_MouseIndex > -1)
            {
                _MouseIndex = -1;
                this.Invalidate();
            }
        }

        private void FriendsListBox_MouseMove(object sender, MouseEventArgs e)
        {
            int index = IndexFromPoint(e.Location);
            
            if (index != _MouseIndex)
            {
                _MouseIndex = index;
                this.Invalidate();
            }
        }

        private int IndexFromPoint(Point point)
        {
            int si = this.SelectedIndex;
            int index = -1;
            if (si == -1 || point.Y < 20 * si)
                index = point.Y / 20;
            else if (point.Y > 20 * si + 50)
                index = (point.Y - (20 * si + 50)) / 20 + si + 1;
            if (index > this.Items.Count - 1)
                index = this.Items.Count - 1;
            return index;
        }

        private void FriendsListBox_DrawItem(object sender, DrawItemEventArgs e)
        {
            Graphics g = e.Graphics;
            int deta = 0;
            e.DrawBackground();
            if (e.Index >= 0 && e.Index < this.Items.Count)
            {
                Friend friend = (Friend)this.Items[e.Index];

                int w = 18, h = 18;
                Rectangle rect;
                if (e.Index > -1)
                {
                    if (this.SelectedIndex != -1 && e.Index > this.SelectedIndex)
                        deta = 30;
                    if (e.Index == _MouseIndex && (e.State & DrawItemState.Selected) != DrawItemState.Selected)
                        g.FillRectangle(Brushes.LightPink, e.Bounds.X, e.Bounds.Y + deta, this.Width, h + 2);
                    else if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
                    {
                        w = 45;
                        h = 45;
                        g.FillRectangle(Brushes.LightSkyBlue, e.Bounds.X, e.Bounds.Y, this.Width, h + 5);
                    }
                }
                rect = new Rectangle(e.Bounds.X, e.Bounds.Y + deta + 1, w, h);
                g.DrawImage(friend.Profile, rect);
                g.DrawString(friend.UserName, Font, Brushes.Black, e.Bounds.X + friend.Profile.Width + 5, e.Bounds.Y + deta);

            }
        }

        private void FriendsListBox_MeasureItem(object sender, MeasureItemEventArgs e)
        {
            if (e.Index >= 0 && e.Index < this.Items.Count)
            {
                if (e.Index != this.SelectedIndex)
                    e.ItemHeight = 20;
                else
                    e.ItemHeight = 50;
                //if (FriendsListbox.SelectedIndex >= 0)
                //    FriendsListbox.Height = (FriendsListbox.Items.Count - 1) * 20 + 50;
            }
        }
    }

这是Friend的代码,用于放在ListBox.Items里的

public class Friend
    {
        private Image profile;

        public Image Profile
        {
            get { return profile; }
            set { profile = value; }
        }

        private string username;

        public string UserName
        {
            get { return username; }
            set { username = value; }
        }
    }
我很想听的主页 我很想听 | 初学一级 | 园豆:152
提问于:2012-09-22 23:49
< >
分享
所有回答(1)
0

可以尝试加大ListBox的高度试试的,或者在Listbox中的数据每行预留一定的空间(加一个空白记录的)

Learning hard | 园豆:362 (菜鸟二级) | 2012-09-24 00:21
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册