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

  • 问题

  • 实现的控件效果,类似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的代码
    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; }
            }
        }


    • 已编辑 loong1 2012年9月22日 15:57
    2012年9月22日 15:43

答案

全部回复

  • Loong,你好

    我用你的代码重现了这个问题,我将尽量在明天找到解决办法。如果这期间你有什么发现,也请分享在这里,谢谢!


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2012年9月24日 13:43
    版主
  • 我仔细看过你的代码,没有找到解决办法,你提个connect吧 https://connect.microsoft.com/visualstudio

    完了,麻烦把link贴在这里,谢谢!

     

    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2012年9月25日 9:49
    版主
  • 谢谢啦,我也没弄明白哪里有问题,不过我现在改重写OnPaint了,反而更方便=。=包括闪烁啊什么的都木有了。。
    2012年9月26日 7:48